首页 > 解决方案 > 如何使用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]

标签: python-3.7google-sheets-apigspread

解决方案


  • 您想将 的值附加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")

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。


推荐阅读