Home > Enterprise >  Error when i try do a batch_update gspread
Error when i try do a batch_update gspread

Time:06-30

I have a problem with gspread, i'm trying do a worksheet.batch_update, but i receive this error mesage

File "D:\ProyectoReal-Statement\venv\lib\site-packages\gspread\worksheet.py", line 903, in dict(vr, range=absolute_range_name(self.title, vr["range"])) for vr in data TypeError: string indices must be integers

My code is:

floor = [typeFloor #String, titleFloor #String, href #String, m2Floor #String, roomsFloor #Integer, priceFloor #Double]
listaFinal.append(floor)
rangeRows = 'A2'   ":F"   str(len(listaFinal))
worksheet.batch_update({
        'range': rangeRows,
        'values': listaFinal
                           })

Any idea how I can add a lot of information to my sheet or how I can solve this problem?

CodePudding user response:

Modification points:

  • The argument data of batch_update(data) is required to be an array.
  • In your script, when len(listaFinal) is 1, the value is put from the cell "A1". From rangeRows = 'A2' ":F" str(len(listaFinal)), I believe that you wanted to put the values from "A2".

When these points are reflected to your script, it becomes as follows.

Modified script:

floor = [typeFloor #String, titleFloor #String, href #String, m2Floor #String, roomsFloor #Integer, priceFloor #Double]
listaFinal.append(floor)

rangeRows = 'A2' # Modified
worksheet.batch_update([{'range': rangeRows, 'values': listaFinal}], value_input_option="USER_ENTERED") # Modified

Or, I thought that in your script, the following modified script might be able to be used.

floor = [typeFloor #String, titleFloor #String, href #String, m2Floor #String, roomsFloor #Integer, priceFloor #Double]
listaFinal.append(floor)

rangeRows = 'A2' # Modified
worksheet.update(rangeRows, listaFinal, value_input_option="USER_ENTERED") # Modified

Note:

  • This modified script supposes that your value of listaFinal is 2 dimensional array. Please be careful about this.

References:

  • Related