如何使用 Google Sheets API Ruby 客户端创建新工作表?

How to create a new worksheet with Google Sheets API Ruby Client?

我正在使用 ruby 客户端用 class Google::Apis::SheetsV4::SheetsService 更新现有的点差 sheet 范围,但找不到创建新点差的方法sheet。我想为每个新年创建一个新的 sheet。我想按标题测试 spreadsheet 中是否存在 sheet,如果不存在则添加一个新的 sheet。我找不到任何可以帮助我完成任务的 ruby 代码示例。

这不能与作为重复问题提出的 link 重复,因为下面我的回答中的 ruby 代码与用 C# 编写的解决方案有很大不同。

http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/SheetsV4/SheetsService

这是我的一些代码:

require 'google/apis/sheets_v4'

SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS

spreadsheet_id = '1NTvP-VkDDE1_xzz_etc'

好的,这是我认为与建议的重复代码非常不同的答案,应该删除对它的引用。此代码包括用于查询 sheet 属性和更新值以及将新列附加到 sheet.

的方法

此答案应与 https://developers.google.com/drive/v3/web/quickstart/ruby

一起阅读

常用代码:

service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

添加一个新的 sheet:

sheet_name = '2020'
column_count = 55

add_sheet_request = Google::Apis::SheetsV4::AddSheetRequest.new
add_sheet_request.properties = Google::Apis::SheetsV4::SheetProperties.new
add_sheet_request.properties.title = sheet_name 

grid_properties = Google::Apis::SheetsV4::GridProperties.new
grid_properties.column_count = column_count
add_sheet_request.properties.grid_properties = grid_properties

batch_update_spreadsheet_request = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new
batch_update_spreadsheet_request.requests = Google::Apis::SheetsV4::Request.new

batch_update_spreadsheet_request_object = [ add_sheet: add_sheet_request ] 
batch_update_spreadsheet_request.requests = batch_update_spreadsheet_request_object 
response = service.batch_update_spreadsheet(spreadsheet_id,
     batch_update_spreadsheet_request)

puts ">>>>>>>>>> response: #{response.inspect}"

正在更新点差sheet 值:

range = 'Sheet1!A1:C2'

value_range_object = {
   "major_dimension": "ROWS",
   "values": [
      ["Multiplicand", "Multiplier", "Result"],
      ["2", "8", "=A2*B2"]
   ]
}

response = service.clear_values(spreadsheet_id, "Sheet1!A1:Z99")
response = service.update_spreadsheet_value(spreadsheet_id, range,
    value_range_object, value_input_option: 'USER_ENTERED')

获取传播sheet的属性、标题和列数:

response = service.get_spreadsheet(spreadsheet_id)
puts ">>>>>>>>>> response: #{response.inspect}"

response.sheets.each do |s|
   puts s.properties.sheet_id
   puts s.properties.index 
   puts s.properties.title
   puts s.properties.grid_properties.column_count
end

正在将新列追加到 sheet:

append_dimension_request = Google::Apis::SheetsV4::AppendDimensionRequest.new

append_dimension_request.dimension = 'COLUMNS'    
append_dimension_request.length = 30
append_dimension_request.sheet_id = 1491311133

batch_update_spreadsheet_request = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new
batch_update_spreadsheet_request.requests = Google::Apis::SheetsV4::Request.new

batch_update_spreadsheet_request_object = [ append_dimension: append_dimension_request ]
batch_update_spreadsheet_request.requests = batch_update_spreadsheet_request_object

response = service.batch_update_spreadsheet(spreadsheet_id, batch_update_spreadsheet_request)