Skip to content

Commit

Permalink
Only copy headers once (in global init)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Monahan committed Nov 16, 2024
1 parent f51bf5b commit f7d3cb8
Show file tree
Hide file tree
Showing 2 changed files with 10,076 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/gsheets_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,33 @@ namespace duckdb

// If writing, clear out the entire sheet first.
// Do this here in the initialization so that it only happens once
std::string response = delete_sheet_data(spreadsheet_id, token, encoded_sheet_name);
std::string delete_response = delete_sheet_data(spreadsheet_id, token, encoded_sheet_name);

// Write out the headers to the file here in the Initialize so they are only written once
// Create object ready to write to Google Sheet
json sheet_data;

sheet_data["range"] = sheet_name;
sheet_data["majorDimension"] = "ROWS";

vector<string> headers = bind_data.Cast<GSheetWriteBindData>().options.name_list;

vector<vector<string>> values;
values.push_back(headers);
sheet_data["values"] = values;

// Convert the JSON object to a string
std::string request_body = sheet_data.dump();

// Make the API call to write data to the Google Sheet
// Today, this is only append.
std::string response = call_sheets_api(spreadsheet_id, token, encoded_sheet_name, HttpMethod::POST, request_body);

// Check for errors in the response
json response_json = parseJson(response);
if (response_json.contains("error")) {
throw duckdb::IOException("Error writing to Google Sheet: " + response_json["error"]["message"].get<std::string>());
}

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

sheet_data["range"] = sheet_name;
sheet_data["majorDimension"] = "ROWS";

vector<string> headers = bind_data_p.Cast<GSheetWriteBindData>().options.name_list;
sheet_data["majorDimension"] = "ROWS";

vector<vector<string>> values;
values.push_back(headers);

for (idx_t r = 0; r < input.size(); r++)
{
Expand Down
Loading

0 comments on commit f7d3cb8

Please sign in to comment.