Home > Software engineering >  How to insert a value in google sheets through python just when the cell is empty/null
How to insert a value in google sheets through python just when the cell is empty/null

Time:09-29

I'm trying to automate googlesheets through python, and every time my DF query runs, it inserts the data with the current day.

To put it simple, when a date column is empty, it have to be fulfilled with date when the program runs. The image is:

EXAMPLE IMAGE

I was trying to do something like it:

ws = client.open("automation").worksheet('sheet2')

ws.update(df_h.fillna('0').columns.values.tolist())

I'm not able to fulfill just the empty space, seems that or all the column is replaced, or all rows, etc.

CodePudding user response:

Solved it thorugh another account:

ws_date_pipe = client.open("automation").worksheet('sheet2')
# Range of date column (targeted one, which is the min range)
next_row_min = str(len(list(filter(None, ws_date_pipe.col_values(8)))) 1)
# Range of first column (which is the max range)
next_row_max = str(len(list(filter(None, ws_date_pipe.col_values(1)))))

cell_list = ws_date_pipe.range(f"H{next_row_min}:H{next_row_max}")
cell_values = []

# Difference between max-min ranges, space that needs to be fulfilled
for x in range(0, ((int(next_row_max) 1)-int(next_row_min)), 1):
    iterator = x
    iterator = datetime.datetime.now().strftime("%Y-%m-%d")
    iterator = str(iterator)
    cell_values.append(iterator)

for i, val in enumerate(cell_values):  
    cell_list[i].value = val

# If date range len "next_row_min" is lower than the first column, then fill.
if int(next_row_min) < int(next_row_max) 1:
    ws_date_pipe.update_cells(cell_list)


print(f'Saved to csv file. {datetime.datetime.now().strftime("%Y-%m-%d")}')
  • Related