3

我正在尝试通过python自动化googlesheets,每次我的DF查询运行时,它都会插入当天的数据。

简单来说,当日期栏为空时,程序运行时必须填写日期。图像是:

示例图片

我试图做类似的事情:

ws = client.open("自动化").worksheet('sheet2')

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

我不能只满足空白,似乎或所有列都被替换,或所有行等。

4

1 回答 1

1

通过另一个帐户解决了它:

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")}')
于 2021-09-28T17:05:18.327 回答