猿问

Python 谷歌电子表格更新在错误的列中

我正在尝试用 python 更新我的谷歌电子表格。不幸的是,我的代码总是更新左侧的一列/单元格。


我的代码:


headers = wks2.row_values(7)

colToUpdate = headers.index("12.11.")



#find the char

cellLookup = wks2.find('Gul')

# get the cell to be updated

cellToUpdate = wks2.cell(cellLookup.row, colToUpdate)


# update the cell's value

cellToUpdate.value = 3

# put it in the queue

cell_list.append(cellToUpdate)

# now, do it

wks2.update_cells(cell_list)

print(cell_list)输出 [<Cell R8C6 3>] 但它应该是/它希望它是 R8C7。


我可以改变他从 1 开始计数或者只是添加 +1 或类似的东西吗?


我添加了电子表格的图像。

幕布斯7119047
浏览 124回答 1
1回答

慕仙森

我想我看到了你的问题。看这段代码:headers = wks2.row_values(7)colToUpdate = headers.index("12.11.")#find the charcellLookup = wks2.find('Gul')# get the cell to be updatedcellToUpdate = wks2.cell(cellLookup.row, colToUpdate)该行:wks2.row_values(7)电子表格的图片表明行和列是通过基于 1 的索引来寻址的。也就是说,第一行或第一列的索引为1而非0。但是操作:colToUpdate = headers.index("12.11.")正在将一个从 0 开始的索引返回到标头列表中。因此这个值必须在从 0 到 1 的尺度之间转换。这就是为什么你偏离了 1 列。所以就这样做:colToUpdate = headers.index("12.11.") + 1你就准备好了,现在你知道为什么这是必要的了。
随时随地看视频慕课网APP

相关分类

Python
我要回答