user3465684
user3465684

Reputation: 1

gspread deleting a specific cell and shifting up

All I want to do is delete a specific cell and shift up, I can't find the answer in the docs.

I can find a cells value easy enough, sheet.cell(X,Y).value

I can delete a row easy enough sheet.delete_row(Z)

And in the UI I can replicate the action I want

delete cell up

But I cant figure out how to do this action with gspread python

Upvotes: 0

Views: 735

Answers (1)

Tanaike
Tanaike

Reputation: 201553

I believe your goal and situation as follows.

  • You want to achieve "Delete cells" and "Shift up" using gspread.
  • You have already been able to get and put values for Google Spreadsheet using Sheets API with gspread.

In this case, the method of batch_update() of gspread is used. The simple sample script is as follows.

Sample script:

In this sample script, the cell "A2" is deleted, and the cells after A3 are shifted up. Please set the range as the GridRange.

spreadsheetId = "###" # Please set Spreadsheet ID.
sheetName = "Sheet1" # Please set sheet name.

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheetId = spreadsheet.worksheet(sheetName)._properties['sheetId']
requests = [
    {
        'deleteRange': {
            'range': {
                'sheetId': sheetId,
                'startRowIndex': 1,
                'endRowIndex': 2,
                'startColumnIndex': 0,
                'endColumnIndex': 1,
            },
            'shiftDimension': 'ROWS',
        }
    }
]
spreadsheet.batch_update({'requests': requests})

References:

Upvotes: 1

Related Questions