1

Based on this answer:

Move a sheet to a particular position using Python & the Google Sheets API

I wrote this piece of code:

body = {'requests': [
    {'updateSheetProperties': {
        'properties': {
            'sheetId': sheetId,
            'index': 1
        }
    }}
]}
spreadsheet.batch_update(body)

But I am getting this error:

"Invalid requests[0].updateSheetProperties: At least one field must be listed in 'fields'. (Use '*' to indicate all fields.)"

If I add this:

body = {'requests': [
    {'updateSheetProperties': {
        'properties': {
            'sheetId': sheetId,
            'index': 1
        },
        "fields": "*"
    }}
]}

I get:

"Invalid requests[0].updateSheetProperties: The sheet name cannot be empty."

I even tried with my sheet name:

"fields": "2019-08-09" 

But I get:

"Invalid requests[0].updateSheetProperties: Invalid field: 2019-08-09"

Any suggestions?

Georgi Bonchev
  • 269
  • 4
  • 12

1 Answers1

3
  • You want to move a sheet using gspread with Python.
    • You want to use batch_update of gspread.
  • You have already been able to put and get values for Spreadsheet using Sheets API.

If my understanding is correct, how about this modification?

Modified script:

In this modification, body was modified.

From:
body = {'requests': [
    {'updateSheetProperties': {
        'properties': {
            'sheetId': sheetId,
            'index': 1
        }
    }}
]}
To:
body = {'requests': [
    {'updateSheetProperties': {
        'properties': {
            'sheetId': sheetId,
            'index': 1
        },
        'fields': 'index',
    }}
]}
  • In your case, please use index to fields.

Note:

  • When you want to move the sheet and rename the sheet name, simultaneously, please use the following request body.

    body = {'requests': [
        {'updateSheetProperties': {
            'properties': {
                'sheetId': sheetId,
                'index': 1,
                'title': 'updatedTitle'
            },
            'fields': 'index,title',
        }}
    ]}
    

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Google landed me here when I was trying to make this work in Java, so for those people: https://stackoverflow.com/questions/23565275/freeze-rows-and-set-styles-with-google-spreadsheet-api/74693377#74693377 – CeePlusPlus Dec 05 '22 at 19:28