@@ -65,7 +65,33 @@ namespace duckdb
65
65
66
66
// If writing, clear out the entire sheet first.
67
67
// 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
+ }
69
95
70
96
return make_uniq<GSheetCopyGlobalState>(context, spreadsheet_id, token, encoded_sheet_name);
71
97
}
@@ -90,12 +116,9 @@ namespace duckdb
90
116
json sheet_data;
91
117
92
118
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" ;
96
120
97
121
vector<vector<string>> values;
98
- values.push_back (headers);
99
122
100
123
for (idx_t r = 0 ; r < input.size (); r++)
101
124
{
0 commit comments