简体   繁体   English

gspread update_cells写入错误的位置

[英]gspread update_cells writes to wrong place

I'm writing code for a personal twitter bot project that includes a bunch of google spreadsheets. 我正在为一个个人Twitter机器人项目编写代码,其中包括一堆Google电子表格。 The twitter side is working well, but I'm having problem with the google spreadsheet side. Twitter方面运作良好,但Google电子表格方面存在问题。 I'm using gspread, and the program keeps writing to the wrong place. 我使用的是gspread,该程序不断将内容写入错误的位置。

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.) josa_record是我正在写的工作表,内容和行正确显示,因此我认为解释该部分的代码不是必需的。(但请告诉我。)

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. 真正的问题是,尽管我已将列指定为2、1和2,但应将第二列上的列写入第一列,而应将第一列上的列完全消失了。 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.) (我不想发布整个函数,因为它的长度为130行,并且几乎无关紧要。)

How about this answer? 这个答案怎么样?

Issue: 问题:

update_cells() of gspread uses the method of spreadsheets.values.update in Sheets API. gspread的update_cells()使用Sheets API中的sheets.values.update方法。 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. 在这种情况下,为了建立名单把值的函数cell_list_to_rect()的文件中utils.py使用。 At this time, in your script, when cells is created with the following order, 目前,在您的脚本中,按照以下顺序创建cells时,

  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. [[tweet.text], [josa_place_vals[coord[0]-1][coord[1]-1]]]作为放置值的列表。 gspread.models.Cell(josa_row+3, 1, "END") is not included in the list. gspread.models.Cell(josa_row+3, 1, "END")不包括在列表中。

When cells is created with the following order, 按照以下顺序创建cells

  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. [[tweet.text], [josa_place_vals[coord[0]-1][coord[1]-1]]]作为放置值的列表。 location[0]+"/"+str(coord[0])+"/"+str(coord[1]) is not included in the list. 列表中不包含location[0]+"/"+str(coord[0])+"/"+str(coord[1])

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 . 看来,当从创造了把值到电子表格中的列表cells ,这些值由功能删除cell_list_to_rect()的文件中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. 从以上结果可以gspread.models.Cell ,当使用gspread.models.Cell的gspread.models.Cell时,发现将值附加到坐标的顺序很重要。

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 . 如果您不想修改gspread的原始脚本并想要修改脚本,则需要考虑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. 在此答案中,假定您已经能够使用gspread放置和获取Spreadsheet的值。

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() ? 作为另一种模式,如何使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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