rubyのclientでスプレッドシート書き込み&データ追記のメモ

require 'google/apis/sheets_v4'
require 'googleauth'
require 'fileutils'

range = "NewSheet:A1"
spreadsheet_id = "foobar_id"
credentials_file_path = "/path/to/cred.json"
service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = "foobar_name1"
service.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open(credentials_file_path),
  scope: Google::Apis::SheetsV4::AUTH_SPREADSHEETS
)
values = []

add_sheet_request = Google::Apis::SheetsV4::AddSheetRequest.new
add_sheet_request.properties = Google::Apis::SheetsV4::SheetProperties.new(title: target_sheet_title)
batch_update_spreadsheet_request = [{ add_sheet: add_sheet_request }]
request_body = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new(requests: batch_update_spreadsheet_request)
response = service.batch_update_spreadsheet(spreadsheet_id, request_body)

data = [
  {
    range: range,
    majorDimension: 'ROWS',
    values: values
  }
]

request_body = Google::Apis::SheetsV4::BatchUpdateValuesRequest.new(value_input_option: 'USER_ENTERED', insert_data_option: 'OVERWRITE', data: data)
service.batch_update_values(spreadsheet_id, request_body)