Faraz Khan Yz
Faraz Khan Yz

Reputation: 43

how to add data one by one in google sheets using python

Here is the Image of Google sheet it still keep updating the same row but i want to append the data one by one 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]

Upvotes: 1

Views: 326

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to append the values of records to the sheet of sheet_tile.
  • You want to achieve this using gspread with python.
  • You have already been able to get and put values using Sheets API.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification point:

  • In order to append values to the sheet, the method of append_row is used.

When your script is modified, how about the following modification?

Pattern 1:

Modified script:

From:
sheet_tile.update_cells(records)
To:
sheet_tile.append_row([e.value for e in records], value_input_option="USER_ENTERED")

Pattern 2:

As another modification pattern, how about the following pattern?

Modified script:

From:
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)
To:
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")

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Upvotes: 2

Related Questions