gpt4 book ai didi

python - 如何在单元格为空/空时通过 python 在 google 工作表中插入值

转载 作者:行者123 更新时间:2023-12-04 17:11:38 25 4
gpt4 key购买 nike

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

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

EXAMPLE IMAGE

我正在尝试做类似的事情:

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

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

我无法只填充空白区域,似乎所有列或所有行都被替换了,等等。

最佳答案

通过其他账号解决:

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")}')

关于python - 如何在单元格为空/空时通过 python 在 google 工作表中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69321467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com