Skip to content

Commit

Permalink
Merge pull request #25 from Alex-Monahan/delete-before-write
Browse files Browse the repository at this point in the history
Delete sheet1 content before writing
  • Loading branch information
archiewood authored Oct 25, 2024
2 parents 39a162a + 2bc9378 commit a384fdf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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);

}

0 comments on commit a384fdf

Please sign in to comment.