python-3.7 - 如何使用python在谷歌表格中一一添加数据
问题描述
这是 Google 表格的图像,它仍然不断更新同一行,但我想一一追加数据, 在此处输入图像描述
def AddEventReasonRecord(self, EventReasonList):
try:
GlobalObj = Global()
records = []
rows = len(EventReasonList)
sheet_tile = self.GoogleSheetTitle()
ItemRow = int(GlobalObj.LasItemRow)
if rows > 0:
for index in range(rows):
ItemRow = ItemRow + 1
sheet_row = ItemRow + 1
Obj: EventReason = EventReasonList[index]
records.append(Cell(sheet_row, 1, Obj.EventID))
records.append(Cell(sheet_row, 2, Obj.EventName))
records.append(Cell(sheet_row, 3, Obj.Description))
records.append(Cell(sheet_row, 4, Obj.Status))
records.append(Cell(sheet_row, 5, Obj.Event))
records.append(Cell(sheet_row, 6, Obj.EmpStatus))
records.append(Cell(sheet_row, 7, Obj.Position))
records.append(Cell(sheet_row, 8, Obj.PayrollEvent))
records.append(Cell(sheet_row, 9, Obj.JobPortlet))
sheet_tile.update_cells(records)
[![enter code here][1]][1]
解决方案
- 您想将 的值附加
records
到 的工作表中sheet_tile
。 - 您想使用 gspread 和 python 来实现这一点。
- 您已经能够使用 Sheets API 获取和放置值。
如果我的理解是正确的,这个答案怎么样?请认为这只是几个可能的答案之一。
修改点:
- 为了将值附加到工作表,使用 的方法
append_row
。
当你的脚本被修改时,下面的修改呢?
模式一:
修改后的脚本:
从:sheet_tile.update_cells(records)
至:
sheet_tile.append_row([e.value for e in records], value_input_option="USER_ENTERED")
模式二:
作为另一种修改模式,下面的模式怎么样?
修改后的脚本:
从:for index in range(rows):
ItemRow = ItemRow + 1
sheet_row = ItemRow + 1
Obj: EventReason = EventReasonList[index]
records.append(Cell(sheet_row, 1, Obj.EventID))
records.append(Cell(sheet_row, 2, Obj.EventName))
records.append(Cell(sheet_row, 3, Obj.Description))
records.append(Cell(sheet_row, 4, Obj.Status))
records.append(Cell(sheet_row, 5, Obj.Event))
records.append(Cell(sheet_row, 6, Obj.EmpStatus))
records.append(Cell(sheet_row, 7, Obj.Position))
records.append(Cell(sheet_row, 8, Obj.PayrollEvent))
records.append(Cell(sheet_row, 9, Obj.JobPortlet))
sheet_tile.update_cells(records)
至:
for index in range(rows):
# ItemRow = ItemRow + 1
# sheet_row = ItemRow + 1
Obj: EventReason = EventReasonList[index]
records.append(Obj.EventID)
records.append(Obj.EventName)
records.append(Obj.Description)
records.append(Obj.Status)
records.append(Obj.Event)
records.append(Obj.EmpStatus)
records.append(Obj.Position)
records.append(Obj.PayrollEvent)
records.append(Obj.JobPortlet)
sheet_tile.append_row(records, value_input_option="USER_ENTERED")
参考:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
推荐阅读
- php - 如何使用 PHP 以安全的方式从第三方 Web 应用发出 POST 请求?
- typescript - 使用 Cloud Functions 时出现“TypeError:无法读取未定义的属性‘替换’”
- gzip - 带有 TFRecord 和压缩 GZIP 的 Beam Java SDK
- bluetooth-lowenergy - 使用 RFcommsocket 连接到蓝牙 DUAL-SPP
- c# - 从继承的类中读取文本框
- sails.js - 如何覆盖sailsjs中的所有蓝图查找操作
- java - Java 并行流产生 HashMap
- php - 我应该始终将 ignore_user_abort 设置为 true 吗?
- java - Android Java 应用程序无法通过蓝牙发送选定的文件/数据
- c# - 如何使用 Activator.CreateInstance 创建列表
哪里 T 在运行时是未知的?