Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete sheet1 content before writing #25

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/gsheets_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ namespace duckdb
std::string sheet_id = extract_sheet_id(file_path);
std::string sheet_name = "Sheet1"; // TODO: make this configurable

// 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(sheet_id, token, sheet_name);

return make_uniq<GSheetCopyGlobalState>(context, sheet_id, token, sheet_name);
}

Expand All @@ -92,6 +96,8 @@ namespace duckdb
sheet_data["range"] = "Sheet1";
sheet_data["majorDimension"] = "ROWS";

// TODO: Add column headers

vector<vector<string>> values;
for (idx_t r = 0; r < input.size(); r++)
{
Expand Down Expand Up @@ -129,9 +135,6 @@ namespace duckdb

// Make the API call to write data to the Google Sheet
// Today, this is only append.
// To overwrite, need to detect if we are the first data chunk and clear out the sheet.
// Is there a way to force that to happen only once, even in the presence of multithreading?
// Maybe this? https://github.com/duckdb/duckdb/pull/7368
std::string response = fetch_sheet_data(gstate.sheet_id, gstate.token, gstate.sheet_name, HttpMethod::POST, request_body);

// Check for errors in the response
Expand Down
9 changes: 8 additions & 1 deletion src/gsheets_requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>

namespace duckdb
{

Expand Down Expand Up @@ -102,4 +101,12 @@ namespace duckdb

return perform_https_request(host, path, token, method, body);
}

std::string delete_sheet_data(const std::string &sheet_id, const std::string &token, const std::string &sheet_name)
{
std::string host = "sheets.googleapis.com";
std::string path = "/v4/spreadsheets/" + sheet_id + "/values/" + sheet_name + ":clear";

return perform_https_request(host, path, token, HttpMethod::POST, "{}");
}
}
3 changes: 3 additions & 0 deletions src/include/gsheets_requests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ std::string perform_https_request(const std::string& host, const std::string& pa
HttpMethod method = HttpMethod::GET, const std::string& body = "", const std::string& content_type = "application/json");

std::string fetch_sheet_data(const std::string& sheet_id, const std::string& token, const std::string& sheet_name, HttpMethod method = HttpMethod::GET, const std::string& body = "");

std::string delete_sheet_data(const std::string& sheet_id, const std::string& token, const std::string& sheet_name);

}
Loading