scorpion35
scorpion35

Reputation: 1084

Ruby - Google Sheets API | InsertDimension

Using gem 'google-api-client' V4 to work with Google Sheets. I am trying to insert a few rows at an index, and here is what I have -

spreadsheet = sheet(sheet_id: Utilities.global_config[:googlesheets][:sheetid], sheet_name: 'Sheetname')

insert_dimension_range = Google::Apis::SheetsV4::DimensionRange.new
insert_dimension_range.sheet_id = spreadsheet.properties.sheet_id
insert_dimension_range.dimension = 'ROWS'
insert_dimension_range.start_index = 1
insert_dimension_range.end_index = 3

insert_dimension = Google::Apis::SheetsV4::InsertDimensionRequest.new(inherit_from_before: false)
insert_dimension.range = insert_dimension_range

insert_dimension_request = Google::Apis::SheetsV4::Request.new
insert_dimension_request.insert_dimension = insert_dimension

batch_update_spreadsheet_request = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new
batch_update_spreadsheet_request.requests = [insert_dimension: insert_dimension_request]

response = service.batch_update_spreadsheet(Utilities.global_config[:googlesheets][:sheetid], batch_update_spreadsheet_request)


Not sure what is wrong with the above code. I am getting an error message - Google::Apis::ClientError: badRequest: Must specify at least one request.

This is how the in-memory representation of object is looking before the API sends a request to Google's servers - enter image description here

I can see the @request_object.requests array has the proper insert_dimension in it, but don't understand the error message.

Does anyone know what I need to fix? Many thanks in advance 🙇‍♂️

Upvotes: 2

Views: 555

Answers (1)

scorpion35
scorpion35

Reputation: 1084

Figured it out! In my question, I specified the start_index = 1, which means I am requesting the API to start inserting from row #1, but in my sheet, I had the first two rows merged. Obviously it would fail.

Changed start_index = 3, and it's working now! Wish the error message was a bit more clear.

Upvotes: 1

Related Questions