@@ -95,7 +95,33 @@ namespace duckdb
95
95
96
96
// If writing, clear out the entire sheet first.
97
97
// 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
+ }
99
125
100
126
return make_uniq<GSheetCopyGlobalState>(context, spreadsheet_id, token, encoded_sheet_name);
101
127
}
@@ -121,11 +147,8 @@ namespace duckdb
121
147
122
148
sheet_data[" range" ] = sheet_name;
123
149
sheet_data[" majorDimension" ] = " ROWS" ;
124
-
125
- vector<string> headers = bind_data_p.Cast <GSheetWriteBindData>().options .name_list ;
126
150
127
151
vector<vector<string>> values;
128
- values.push_back (headers);
129
152
130
153
for (idx_t r = 0 ; r < input.size (); r++)
131
154
{
0 commit comments