googleapis/google-api-ruby-client

batch_update_spreadsheet with update_cells request not working

patmesideways opened this issue · 2 comments

I'm experiencing the issue where the update_cells request does not seem to be creating the pivot table in the request body below when trying to do batch_update_spreadsheet. The response from is just a BatchUpdateSpreadSheetResponse with one reply but the reply seems to be empty.

I've tried other update_cells requests and the changes don't reflect on the sheet as well. Is there anything that I'm missing?

I've verified that requests like add_sheet with batch_update_spreadsheet works fine.

Environment details

  • Ruby version: 2.7.4

Code example

spreadsheet = {
  properties: {
    title: 'Example'
  }
}
spreadsheet = service.create_spreadsheet(spreadsheet,
                                         fields: 'spreadsheetId')
spreadsheet_id = spreadsheet.spreadsheet_id
add_sheets_body = { requests: [{ add_sheet: { properties: { title: 'sheet1' } } }, { add_sheet: { properties: { title: 'sheet2'}}}]}
result = service.batch_update_spreadsheet(spreadsheet_id, add_sheets_body, {})

# These two sheets get created no problem
source_sheet_id = result.replies[0].add_sheet.properties.sheet_id
target_sheet_id = result.replies[1].add_sheet.properties.sheet_id

rows = CSV.read('data.csv').to_a
values = rows.reduce([]) do |list, row|
    list << row
end
range_name = "'sheet1'!A1:H#{rows.count}"

# The following commands get updated with no issues
value_range_object = Google::Apis::SheetsV4::ValueRange.new(range: range_name, values: values)
service.update_spreadsheet_value(spreadsheet_id,
                                                           range_name,
                                                           value_range_object,
                                                           value_input_option: 'USER_ENTERED')

pivot_table_request = [
  {
    update_cells: {
      rows: {
        values: [
          {
            pivot_table: {
              source: {
                sheet_id: source_sheet_id,
                start_row_index: 0,
                start_column_index: 0,
                end_row_index: 1118,
                end_column_index: 8
              },
              rows: [
                {
                  source_column_offset: 0,
                  show_totals: false
                },
                {
                  source_column_offset: 1,
                  show_totals: false
                }
              ],
              values: [
                {
                  summarize_function: 'SUM',
                  source_column_offset: 2
                },
                {
                  summarize_function: 'SUM',
                  source_column_offset: 3
                },
                {
                  summarize_function: 'SUM',
                  source_column_offset: 4
                },
                {
                  summarize_function: 'AVG',
                  source_column_offset: 5
                },
                {
                  summarize_function: 'SUM',
                  source_column_offset: 6
                },
                {
                  summarize_function: 'SUM',
                  source_column_offset: 7
                }
              ],
              value_layout: 'HORIZONTAL'
            }
          }
        ]
      },
      start: {
        sheet_id: target_sheet_id,
        row_index: 10,
        column_index: 0
      },
      fields: 'pivotTable'
    }
  }
]
pivot_table_body = { requests: pivot_table_request }
result = service.batch_update_spreadsheet(spreadsheet_id, pivot_table_body, {})