簡體   English   中英

如何使用Google表格API Ruby客戶端創建新工作表?

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

我正在使用ruby客戶端使用Google :: Apis :: SheetsV4 :: SheetsService類更新現有的電子表格范圍,但無法找到創建新工作表的方法。 我想為每個新的一年創建一個新表。 我想通過標題測試電子表格中是否存在工作表,如果不存在則添加新工作表。 我找不到任何幫助我完成任務的ruby代碼示例。

這不能與作為重復問題提出的鏈接重復,因為我在下面的回答中的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'

好的,這里的答案我認為與建議的副本有很多不同的代碼,應刪除哪些引用。 此代碼包括用於查詢工作表屬性和更新值以及將新列附加到工作表的方法。

此答案應與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_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}"

更新電子表格值:

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')

獲取電子表格的屬性,標題和列數:

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

將新列附加到工作表:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM