【问题标题】:Google Sheets v4 API Ruby - Trying to update values in cells in a column but the updates are not happeningGoogle Sheets v4 API Ruby - 尝试更新列中单元格中的值,但没有发生更新
【发布时间】:2016-08-31 06:50:10
【问题描述】:

Ruby:2.3.1

导轨:5.0.0

我有一张包含如下数据的表格:A、B、C、D 代表表格中的列名,1、2、3 代表行号。

我已经编写了一个提到 here 的脚本,我正在尝试更新单元格 B2B3 中的值,并且我正在使用以下更新代码:

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


    【解决方案1】:

    解决了。

    解决方案是在 ValueRangeObject 中使用 Symbol-keys,而不是 String-keys。那就是我必须从关注中更改我的value_range_object

    {
      "major_dimension" => "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)"]
       ]
    }
    

    value_range_object = {
      major_dimension: "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)"]
       ]
    }
    

    请注意,哈希键是符号而不是字符串。

    看起来很简单,但我花了几个小时才弄明白。

    更多详情请访问https://github.com/google/google-api-ruby-client/issues/462#issuecomment-243927914

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多