Home > Software design >  Added rows like "ABCD -> DEFG -> GHIJ" by apped_row
Added rows like "ABCD -> DEFG -> GHIJ" by apped_row

Time:07-07

import gspread
import random

gc = gspread.service_account(filename='영업인만들기\gspread-******-**********.json')

sh = gc.open("관리자_종합_DB") 
worksheet = sh.worksheet("영업인") 


Name = input('영업인 이름 : ')
Email = input('영업인 이메일 : ')


alist = worksheet.col_values(4)[1:] 

a = random.randint(10000,99999)       
while a in alist :              
    a = random.randint(10000,99999)

worksheet.append_row([Name,Email,'',a])

If I write down name and email, a random code will come out.

But when I run it, it's written strangely on the Google sheet.


This is the result of running it 3 times.

enter image description here

It's not written in the form "ABCD -> ABCD ··· ABCD", it's written in the form "ABCD -> DEFG -> GHIJ".

How do I solve this?

CodePudding user response:

The issue is that append_row tries to find a logical table to append to. And when you have empty cells in your previous row it could move this logical table to the right. The parameter table_range was introduced to specify where you want to start appending the data from. This avoids append_row trying to find the logical table on its own.

worksheet.append_row([Name,Email,'',a], table_range='A1')
  • Related