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

fix(reader): default to first sheet by index if no ID found #54

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
15 changes: 10 additions & 5 deletions src/gsheets_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ unique_ptr<FunctionData> ReadSheetBind(ClientContext &context, TableFunctionBind
auto sheet_input = input.inputs[0].GetValue<string>();

// Flags
bool header = true;
bool use_explicit_sheet_name = false;

// Default values
bool header = true;
string sheet_name = "Sheet1";
string sheet_id = "0";
string sheet_name = "";
string sheet_id = "";

// Extract the spreadsheet ID from the input (URL or ID)
std::string spreadsheet_id = extract_spreadsheet_id(sheet_input);
Expand Down Expand Up @@ -150,7 +150,12 @@ unique_ptr<FunctionData> ReadSheetBind(ClientContext &context, TableFunctionBind
// Get sheet name from URL if not provided as input
if (!use_explicit_sheet_name) {
sheet_id = extract_sheet_id(sheet_input);
sheet_name = get_sheet_name_from_id(spreadsheet_id, sheet_id, token);
if (sheet_id.empty()) {
// Fallback to first sheet by index
sheet_name = get_sheet_name_from_index(spreadsheet_id, "0", token);
} else {
sheet_name = get_sheet_name_from_id(spreadsheet_id, sheet_id, token);
}
}

std::string encoded_sheet_name = url_encode(sheet_name);
Expand Down
14 changes: 12 additions & 2 deletions src/gsheets_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <json.hpp>
#include <iostream>
#include <sstream>
#include <string>

using json = nlohmann::json;
namespace duckdb {
Expand Down Expand Up @@ -37,7 +36,7 @@ std::string extract_sheet_id(const std::string& input) {
return match.str(1);
}
}
return "0";
return "";
}

std::string get_sheet_name_from_id(const std::string& spreadsheet_id, const std::string& sheet_id, const std::string& token) {
Expand All @@ -51,6 +50,17 @@ std::string get_sheet_name_from_id(const std::string& spreadsheet_id, const std:
throw duckdb::InvalidInputException("Sheet with ID %s not found", sheet_id);
}

std::string get_sheet_name_from_index(const std::string& spreadsheet_id, const std::string& sheet_index, const std::string& token) {
std::string metadata_response = get_spreadsheet_metadata(spreadsheet_id, token);
json metadata = parseJson(metadata_response);
for (const auto& sheet : metadata["sheets"]) {
if (sheet["properties"]["index"].get<int>() == std::stoi(sheet_index)) {
return sheet["properties"]["title"].get<std::string>();
}
}
throw duckdb::InvalidInputException("Sheet with index %s not found", sheet_index);
}

std::string get_sheet_id_from_name(const std::string& spreadsheet_id, const std::string& sheet_name, const std::string& token) {
std::string metadata_response = get_spreadsheet_metadata(spreadsheet_id, token);
json metadata = parseJson(metadata_response);
Expand Down
11 changes: 10 additions & 1 deletion src/include/gsheets_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ std::string extract_sheet_id(const std::string& input);
*/
std::string get_sheet_name_from_id(const std::string& spreadsheet_id, const std::string& sheet_id, const std::string& token);

/**
* Gets the sheet name from a spreadsheet ID and sheet ID
* @param spreadsheet_id The spreadsheet ID
* @param sheet_index The sheet index (zero-based)
* @param token The Google API token
* @return The sheet name
*/
std::string get_sheet_name_from_index(const std::string& spreadsheet_id, const std::string& sheet_index, const std::string& token);

/**
* Gets the sheet ID from a spreadsheet ID and sheet name
* @param spreadsheet_id The spreadsheet ID
Expand Down Expand Up @@ -73,4 +82,4 @@ std::string generate_random_string(size_t length);
*/
std::string url_encode(const std::string& str);

} // namespace duckdb
} // namespace duckdb
Loading