【问题标题】:How to insert a value in google sheets through python just when the cell is empty/null仅当单元格为空/null时,如何通过python在谷歌表格中插入一个值
【发布时间】:2021-09-24 21:56:28
【问题描述】:

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

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

EXAMPLE IMAGE

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

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

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

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

【问题讨论】:

  • 您是否考虑过使用Sheets API?还是 Apps 脚本?
  • 不,我不知道,但还是谢谢 :)

标签: python google-sheets


【解决方案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-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    相关资源
    最近更新 更多