Skip to content

Commit f7d3cb8

Browse files
committed
Only copy headers once (in global init)
1 parent f51bf5b commit f7d3cb8

File tree

2 files changed

+10076
-5
lines changed

2 files changed

+10076
-5
lines changed

src/gsheets_copy.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,33 @@ namespace duckdb
6565

6666
// If writing, clear out the entire sheet first.
6767
// Do this here in the initialization so that it only happens once
68-
std::string response = delete_sheet_data(spreadsheet_id, token, encoded_sheet_name);
68+
std::string delete_response = delete_sheet_data(spreadsheet_id, token, encoded_sheet_name);
69+
70+
// Write out the headers to the file here in the Initialize so they are only written once
71+
// Create object ready to write to Google Sheet
72+
json sheet_data;
73+
74+
sheet_data["range"] = sheet_name;
75+
sheet_data["majorDimension"] = "ROWS";
76+
77+
vector<string> headers = bind_data.Cast<GSheetWriteBindData>().options.name_list;
78+
79+
vector<vector<string>> values;
80+
values.push_back(headers);
81+
sheet_data["values"] = values;
82+
83+
// Convert the JSON object to a string
84+
std::string request_body = sheet_data.dump();
85+
86+
// Make the API call to write data to the Google Sheet
87+
// Today, this is only append.
88+
std::string response = call_sheets_api(spreadsheet_id, token, encoded_sheet_name, HttpMethod::POST, request_body);
89+
90+
// Check for errors in the response
91+
json response_json = parseJson(response);
92+
if (response_json.contains("error")) {
93+
throw duckdb::IOException("Error writing to Google Sheet: " + response_json["error"]["message"].get<std::string>());
94+
}
6995

7096
return make_uniq<GSheetCopyGlobalState>(context, spreadsheet_id, token, encoded_sheet_name);
7197
}
@@ -90,12 +116,9 @@ namespace duckdb
90116
json sheet_data;
91117

92118
sheet_data["range"] = sheet_name;
93-
sheet_data["majorDimension"] = "ROWS";
94-
95-
vector<string> headers = bind_data_p.Cast<GSheetWriteBindData>().options.name_list;
119+
sheet_data["majorDimension"] = "ROWS";
96120

97121
vector<vector<string>> values;
98-
values.push_back(headers);
99122

100123
for (idx_t r = 0; r < input.size(); r++)
101124
{

0 commit comments

Comments
 (0)