如何使用python在Google表格中一一添加数据

问题描述 投票:0回答:1

这里是Google工作表的图像,它仍在不断更新同一行,但我想一一追加数据enter image description here

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.7 google-sheets-api gspread
1个回答
0
投票
    您想将records的值附加到sheet_tile的表上。
  • 您想通过gspread和python实现此目的。
  • 您已经能够使用Sheets API获取和放置值。
  • 如果我的理解正确,那么这个答案如何?请认为这只是几个可能的答案之一。

    修改点:

      为了将值附加到工作表,使用append_row的方法。

  • 修改脚本后,如何进行以下修改?

    模式1:

    修改的脚本:从:

    sheet_tile.update_cells(records) 至:
    sheet_tile.append_row([e.value for e in records], value_input_option="USER_ENTERED")
    
    模式2:

    作为另一个修改模式,以下模式如何?

    修改的脚本:从:

    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")
    
    参考:

  • ValueInputOption
  • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意。
  • © www.soinside.com 2019 - 2024. All rights reserved.