为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

幕布斯7119047 2023-12-09 16:55:59
我正在尝试用 python 更新我的谷歌电子表格。不幸的是,我的代码总是更新左侧的一列/单元格。我的代码: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)# update the cell's valuecellToUpdate.value = 3# put it in the queuecell_list.append(cellToUpdate)# now, do itwks2.update_cells(cell_list)print(cell_list)输出 [<Cell R8C6 3>] 但它应该是/它希望它是 R8C7。我可以改变他从 1 开始计数或者只是添加 +1 或类似的东西吗?我添加了电子表格的图像。
查看完整描述

1 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

我想我看到了你的问题。看这段代码:


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)

该行:


wks2.row_values(7)

电子表格的图片表明行和列是通过基于 1 的索引来寻址的。也就是说,第一行或第一列的索引为1而非0。但是操作:


colToUpdate = headers.index("12.11.")

正在将一个从 0 开始的索引返回到标头列表中。因此这个值必须在从 0 到 1 的尺度之间转换。这就是为什么你偏离了 1 列。所以就这样做:


colToUpdate = headers.index("12.11.") + 1

你就准备好了,现在你知道为什么这是必要的了。


查看完整回答
反对 回复 2023-12-09
  • 1 回答
  • 0 关注
  • 51 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信