从 Google Spreadsheet 中获取单个 sheet 作为 Ruby 中的字节
Fetching a single sheet from a Google Spreadsheet as bytes in Ruby
我正在尝试从 google 传播 sheet 中以 excel 格式检索单个 sheet,我已正确设置所有访问权限并且可以 运行 不同的 google sheet v4 api 函数。
我想使用 Google::Apis::SheetsV4::SheetsService::copy_spreadsheet 函数复制单个 sheet,如此处 Ruby 示例中所述 - https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.sheets/copyTo
这是我的代码 -
service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
spreadsheet_id = "<passing my spreadsheet id here>"
gid = "<setting this as my sheet id from the spreadsheet>"
request = Google::Apis::SheetsV4::CopySheetToAnotherSpreadsheetRequest.new(
destination_spreadsheet_id: "0",
)
response1 = service.copy_spreadsheet(spreadsheet_id,gid,request)
puts response1.to_json
这总是失败并出现以下错误 -
/usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/http_command.rb:229:in `check_status': badRequest: Invalid destinationSpreadsheetId [0] (Google::Apis::ClientError)
from /usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/api_command.rb:134:in `check_status'
如果有人可以帮助我如何正确使用它,那就太好了,如果有更好的方法来 download/export 从 sheet 中的传播 sheet 中获得单个 sheet 32=] 让我知道。
问题 1 的答案
This always fails with the following error -
/usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/http_command.rb:229:in `check_status': badRequest: Invalid destinationSpreadsheetId [0] (Google::Apis::ClientError)
from /usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/api_command.rb:134:in `check_status'
Would be great if someone can help me on how to use this properly,
当我看到您的错误消息和您的脚本时,我认为 destination_spreadsheet_id: "0",
不正确。在这种情况下,请设置目标 Spreadsheet ID。当这反映在您的脚本中时,它会变成如下。
src_spreadsheet_id = "###" # Please set the source Spreadsheet ID.
src_sheet_id = "###" # Please set the sheet ID of the source Spreadsheet.
dst_spreadsheet_id = "###" # Please set the destination Spreadsheet ID.
request = Google::Apis::SheetsV4::CopySheetToAnotherSpreadsheetRequest.new(
destination_spreadsheet_id: dst_spreadsheet_id,
)
response1 = service.copy_spreadsheet(src_spreadsheet_id,src_sheet_id,request)
puts response1.to_json
问题 2 的答案
I'm trying to retrieve a single sheet from a google spreadsheet in excel format, I have all the access setup correctly and can run different google sheet v4 api functions on it.
also if there's a better way to download/export a single sheet from a spreadsheet in Ruby let me know.
在这种情况下,下面的示例脚本怎么样?在此脚本中,使用 https://docs.google.com/spreadsheets/d/{spreadsheetId}/export?format=xlsx&gid={sheetId}
的端点下载包含特定 sheet 的 XLSX 数据。因此,请将您的 Spreadsheet ID 和 sheet ID 设置为 URL。并且,在这种情况下,访问令牌是从您正在使用的 service
中检索的。
url = 'https://docs.google.com/spreadsheets/d/{spreadsheetId}/export?format=xlsx&gid={sheetId}'
filename = 'sample.xlsx' # Please set the saved filename.
access_token = service.request_options.authorization.access_token
open(
url,
"Authorization" => "Bearer " + access_token,
:redirect => true
) do |file|
open(filename, "w+b") do |out|
out.write(file.read)
end
end
- 当此脚本为运行时,
spreadsheetId
的sheetId
的特定sheet被下载为XLSX数据并保存。
- 此脚本使用
require "open-uri"
.
注:
- 当Spreadsheet作为XLSX数据下载时,出现与范围相关的错误时,请将
https://www.googleapis.com/auth/drive.readonly
添加到范围并重新授权。这样,脚本就可以工作了。
参考:
我正在尝试从 google 传播 sheet 中以 excel 格式检索单个 sheet,我已正确设置所有访问权限并且可以 运行 不同的 google sheet v4 api 函数。
我想使用 Google::Apis::SheetsV4::SheetsService::copy_spreadsheet 函数复制单个 sheet,如此处 Ruby 示例中所述 - https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.sheets/copyTo
这是我的代码 -
service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
spreadsheet_id = "<passing my spreadsheet id here>"
gid = "<setting this as my sheet id from the spreadsheet>"
request = Google::Apis::SheetsV4::CopySheetToAnotherSpreadsheetRequest.new(
destination_spreadsheet_id: "0",
)
response1 = service.copy_spreadsheet(spreadsheet_id,gid,request)
puts response1.to_json
这总是失败并出现以下错误 -
/usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/http_command.rb:229:in `check_status': badRequest: Invalid destinationSpreadsheetId [0] (Google::Apis::ClientError)
from /usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/api_command.rb:134:in `check_status'
如果有人可以帮助我如何正确使用它,那就太好了,如果有更好的方法来 download/export 从 sheet 中的传播 sheet 中获得单个 sheet 32=] 让我知道。
问题 1 的答案
This always fails with the following error -
/usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/http_command.rb:229:in `check_status': badRequest: Invalid destinationSpreadsheetId [0] (Google::Apis::ClientError) from /usr/local/lib/ruby/gems/3.1.0/gems/google-apis-core-0.4.2/lib/google/apis/core/api_command.rb:134:in `check_status'
Would be great if someone can help me on how to use this properly,
当我看到您的错误消息和您的脚本时,我认为 destination_spreadsheet_id: "0",
不正确。在这种情况下,请设置目标 Spreadsheet ID。当这反映在您的脚本中时,它会变成如下。
src_spreadsheet_id = "###" # Please set the source Spreadsheet ID.
src_sheet_id = "###" # Please set the sheet ID of the source Spreadsheet.
dst_spreadsheet_id = "###" # Please set the destination Spreadsheet ID.
request = Google::Apis::SheetsV4::CopySheetToAnotherSpreadsheetRequest.new(
destination_spreadsheet_id: dst_spreadsheet_id,
)
response1 = service.copy_spreadsheet(src_spreadsheet_id,src_sheet_id,request)
puts response1.to_json
问题 2 的答案
I'm trying to retrieve a single sheet from a google spreadsheet in excel format, I have all the access setup correctly and can run different google sheet v4 api functions on it.
also if there's a better way to download/export a single sheet from a spreadsheet in Ruby let me know.
在这种情况下,下面的示例脚本怎么样?在此脚本中,使用 https://docs.google.com/spreadsheets/d/{spreadsheetId}/export?format=xlsx&gid={sheetId}
的端点下载包含特定 sheet 的 XLSX 数据。因此,请将您的 Spreadsheet ID 和 sheet ID 设置为 URL。并且,在这种情况下,访问令牌是从您正在使用的 service
中检索的。
url = 'https://docs.google.com/spreadsheets/d/{spreadsheetId}/export?format=xlsx&gid={sheetId}'
filename = 'sample.xlsx' # Please set the saved filename.
access_token = service.request_options.authorization.access_token
open(
url,
"Authorization" => "Bearer " + access_token,
:redirect => true
) do |file|
open(filename, "w+b") do |out|
out.write(file.read)
end
end
- 当此脚本为运行时,
spreadsheetId
的sheetId
的特定sheet被下载为XLSX数据并保存。 - 此脚本使用
require "open-uri"
.
注:
- 当Spreadsheet作为XLSX数据下载时,出现与范围相关的错误时,请将
https://www.googleapis.com/auth/drive.readonly
添加到范围并重新授权。这样,脚本就可以工作了。