【发布时间】:2016-08-31 06:50:10
【问题描述】:
Ruby:2.3.1
导轨:5.0.0
我有一张包含如下数据的表格:A、B、C、D 代表表格中的列名,1、2、3 代表行号。
我已经编写了一个提到 here 的脚本,我正在尝试更新单元格 B2 和 B3 中的值,并且我正在使用以下更新代码:
spreadsheet_id = "<My Private Spreadsheet ID>"
range = 'Sheet1!B2:B3'
value_range_object = {
"majorDimension"=>"COLUMNS",
"values"=>[
[9, 18] # B2 should be updated with value 9 and B3 should be updated with value 18
]
}
service = Google::Apis::SheetsV4::SheetsService.new
service.authorization = authorization
update_res = service.update_spreadsheet_value(spreadsheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')
puts ">>>>>>>>>> update_res: #{update_res.inspect}"
但这就是打印
>>>>>>>>>> update_res: #<Google::Apis::SheetsV4::UpdateValuesResponse:0x000000068acc70 @updated_range="Sheet1!B2", @spreadsheet_id="<My Private Spreadsheet ID>">
并且没有对工作表进行更新。
根据documentation,当更新成功时应收到以下响应
{
"spreadsheetId": spreadsheetId,
"updatedRange": "Sheet1!B1:D5",
"updatedRows": 3,
"updatedColumns": 2,
"updatedCells": 6,
}
与我得到的响应相比,我的代码中肯定有一些东西缺失或不正确,这阻碍了更新的应用。
我能否获得一些指导以解决我的问题,从而实现我想要的目标?
更新
按照https://developers.google.com/sheets/samples/writing#write_a_single_range 给出的示例,我尝试了一种稍微不同的方法
作为其中的一部分,我在 Google 云端硬盘上创建了一个全新的空白表,将其与我的服务帐户共享,并尝试使用以下代码对其进行更新:
require 'google/apis/sheets_v4'
ENV["GOOGLE_ACCOUNT_TYPE"] = 'service_account'
ENV["GOOGLE_CLIENT_EMAIL"] = '<client_email_from_downloaded_json_here>'
ENV["GOOGLE_PRIVATE_KEY"] = "<private_key_from_downloaded_json_here>"
SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS
authorization = Google::Auth.get_application_default(SCOPE)
# Initialize the API
service = Google::Apis::SheetsV4::SheetsService.new
service.authorization = authorization
spreadsheet_id = '<MY SPREADSHEET ID HERE>'
sheet_name = 'Sheet1'
range = "Sheet1!A1:D5"
value_range_object = {
"majorDimension" => "ROWS",
"values" => [
["Item", "Cost", "Stocked", "Ship Date"],
["Wheel", "$20.50", "4", "3/1/2016"],
["Door", "$15", "2", "3/15/2016"],
["Engine", "$100", "1", "30/20/2016"],
["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
]
}
update_res = service.update_spreadsheet_value(spreadsheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')
puts ">>>>>>>>>> update_res: #{update_res.inspect}"
但奇怪的是,这也不起作用。我收到以下回复
>>>>>>>>>> update_res: #<Google::Apis::SheetsV4::UpdateValuesResponse:0x00000006540348 @updated_range="Sheet1!A1", @spreadsheet_id="MY SPREADSHEET ID HERE">
执行代码后工作表也是空白的。
任何人都可以看看这个并评论发生了什么吗?
【问题讨论】:
标签: ruby google-sheets-api google-api-ruby-client