简体   繁体   中英

gspread update_cells writes to wrong place

I'm writing code for a personal twitter bot project that includes a bunch of google spreadsheets. The twitter side is working well, but I'm having problem with the google spreadsheet side. I'm using gspread, and the program keeps writing to the wrong place.

cells=[]
cells.append(gspread.models.Cell(josa_row+2, 2, tweet.text))

if josa_place_vals[coord[0]-1][coord[1]]=="END":
    cells.append(gspread.models.Cell(josa_row+3, 1, "END"))
else:
    cells.append(gspread.models.Cell(josa_row+2, 1,
        location[0]+"/"+str(coord[0])+"/"+str(coord[1])))

cells.append(gspread.models.Cell(josa_row+3, 2,
    josa_place_vals[coord[0]-1][coord[1]-1]))

josa_record.update_cells(cells)

josa_record is the worksheet I'm writing to, and the the content&row comes out correctly so I don't think the code that explains that part is necessary.(But please tell me if it is.)

The real problem is that although I've specified the columns to be 2, 1, and 2, the ones that should be on the second column are written to the first column and the one that should be on the first column disappear entirely. It doesn't raise an error though.

What am I doing wrong? Do I need to post more code for you guys to help? (I didn't want to post the whole function because it's 130 lines long and mostly irrelevant.)

How about this answer?

Issue:

update_cells() of gspread uses the method of spreadsheets.values.update in Sheets API. In this case, in order to create the list for putting the values, the function of cell_list_to_rect() in the file of utils.py is used. At this time, in your script, when cells is created with the following order,

  1. gspread.models.Cell(josa_row+2, 2, tweet.text)
  2. gspread.models.Cell(josa_row+3, 1, "END")
  3. gspread.models.Cell(josa_row+3, 2, josa_place_vals[coord[0]-1][coord[1]-1])

[[tweet.text], [josa_place_vals[coord[0]-1][coord[1]-1]]] is created as the list for putting the values. gspread.models.Cell(josa_row+3, 1, "END") is not included in the list.

When cells is created with the following order,

  1. gspread.models.Cell(josa_row+2, 2, tweet.text)
  2. gspread.models.Cell(josa_row+2, 1, location[0]+"/"+str(coord[0])+"/"+str(coord[1]))
  3. gspread.models.Cell(josa_row+3, 2, josa_place_vals[coord[0]-1][coord[1]-1])

[[tweet.text], [josa_place_vals[coord[0]-1][coord[1]-1]]] is created as the list for putting the values. location[0]+"/"+str(coord[0])+"/"+str(coord[1]) is not included in the list.

It seems that when the list for putting values to the Spreadsheet is created from cells , these values are removed by the function of cell_list_to_rect() in the file of utils.py .

From above results, when gspread.models.Cell of gspread is used, it is found that the order for appending values with the coordinates is important.

Solution:

If you don't want to modify the original script of gspread and want to modify your script, it is required to think of the order of each value in cells . It appends the values in small order of row and column. So how about the following modification?

I think that there are several modifications for your situation, so please think of this as just one of several answers.

Modified script:

cells=[]
cells.append(gspread.models.Cell(josa_row+2, 2, tweet.text))

if josa_place_vals[coord[0]-1][coord[1]]=="END":
    cells.insert(0, gspread.models.Cell(josa_row+2, 1, ""))  # Added
    cells.append(gspread.models.Cell(josa_row+3, 1, "END"))
else:
    cells.insert(0, gspread.models.Cell(josa_row+2, 1, location[0]+"/"+str(coord[0])+"/"+str(coord[1])))  # Modified

cells.append(gspread.models.Cell(josa_row+3, 2,
    josa_place_vals[coord[0]-1][coord[1]-1]))

josa_record.update_cells(cells)

Note:

  • In this answer, it supposes that you have already been able to put and get values for Spreadsheet using gspread.

References:

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

Added:

As an another pattern, how about using sort() ?

Modified script:

cells=[]
cells.append(gspread.models.Cell(josa_row+2, 2, tweet.text))

if josa_place_vals[coord[0]-1][coord[1]]=="END":
    cells.append(gspread.models.Cell(josa_row+2, 1, ""))  # Added
    cells.append(gspread.models.Cell(josa_row+3, 1, "END"))
else:
    cells.append(gspread.models.Cell(josa_row+2, 1, location[0]+"/"+str(coord[0])+"/"+str(coord[1])))

cells.append(gspread.models.Cell(josa_row+3, 2,
    josa_place_vals[coord[0]-1][coord[1]-1]))

cells.sort(key=lambda x: (x._row, x._col))  # Added
josa_record.update_cells(cells)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM