Skip to content

Commit 4594194

Browse files
committed
Only copy headers once (in InitializeGlobal)
1 parent e8d239c commit 4594194

File tree

2 files changed

+10075
-4
lines changed

2 files changed

+10075
-4
lines changed

src/gsheets_copy.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,33 @@ namespace duckdb
9595

9696
// If writing, clear out the entire sheet first.
9797
// Do this here in the initialization so that it only happens once
98-
std::string response = delete_sheet_data(spreadsheet_id, token, encoded_sheet_name);
98+
std::string delete_response = delete_sheet_data(spreadsheet_id, token, encoded_sheet_name);
99+
100+
// Write out the headers to the file here in the Initialize so they are only written once
101+
// Create object ready to write to Google Sheet
102+
json sheet_data;
103+
104+
sheet_data["range"] = sheet_name;
105+
sheet_data["majorDimension"] = "ROWS";
106+
107+
vector<string> headers = bind_data.Cast<GSheetWriteBindData>().options.name_list;
108+
109+
vector<vector<string>> values;
110+
values.push_back(headers);
111+
sheet_data["values"] = values;
112+
113+
// Convert the JSON object to a string
114+
std::string request_body = sheet_data.dump();
115+
116+
// Make the API call to write data to the Google Sheet
117+
// Today, this is only append.
118+
std::string response = call_sheets_api(spreadsheet_id, token, encoded_sheet_name, HttpMethod::POST, request_body);
119+
120+
// Check for errors in the response
121+
json response_json = parseJson(response);
122+
if (response_json.contains("error")) {
123+
throw duckdb::IOException("Error writing to Google Sheet: " + response_json["error"]["message"].get<std::string>());
124+
}
99125

100126
return make_uniq<GSheetCopyGlobalState>(context, spreadsheet_id, token, encoded_sheet_name);
101127
}
@@ -121,11 +147,8 @@ namespace duckdb
121147

122148
sheet_data["range"] = sheet_name;
123149
sheet_data["majorDimension"] = "ROWS";
124-
125-
vector<string> headers = bind_data_p.Cast<GSheetWriteBindData>().options.name_list;
126150

127151
vector<vector<string>> values;
128-
values.push_back(headers);
129152

130153
for (idx_t r = 0; r < input.size(); r++)
131154
{

0 commit comments

Comments
 (0)