【问题标题】:Gspread append_row appending data to different columnGspread append_row 将数据附加到不同的列
【发布时间】:2020-03-21 20:53:56
【问题描述】:

see the output here

我正在使用 append_row 将数据添加到谷歌工作表......我不明白为什么会这样
#the code
def write_to_dangersheet(flow):
            credentials_file = "file.json"
            scope = ['https://spreadsheets.google.com/feeds',
                     'https://www.googleapis.com/auth/drive']
            credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scope)
            gc=gspread.authorize(credentials)

            test_wb=gc.open_by_key('yyyyyyyyyyyyyy').worksheet('betaflow')

            f=[phonenumber,date," ",flow,]

            if flow=='webhook-test':
                test_wb.append_row(f)
        write_to_dangersheet(flow)

【问题讨论】:

  • 不幸的是,您的脚本无法复制您的问题。那我能问一下复制问题的详细方法吗?
  • 查看我在 gspread 问题中的回复:github.com/burnash/gspread/issues/735

标签: python gspread


【解决方案1】:

使用append_row()table_range参数显式指定表格范围:

worksheet.append_row(['Test1', '', 'Test2'], table_range='A1')

背景:

Worksheet.append_row() 方法对应于 Sheets API spreadsheets.values.append

当调用spreadsheets.values.append 时,它会搜索逻辑“表”以附加一行值。值将附加到表格的下一行,从表格的第一列开始。

根据电子表格中的数据,可能会有多个潜在的“表格”(通常由空列或空行分隔)。默认情况下,append_row() 不指定使用哪个“表格”,而是让 Sheets API 隐式检测表格。如果有多个表格,Sheets API 可以选择您不打算附加到的“表格”。在这种情况下,您需要明确告诉 API 您想使用哪个逻辑“表”。

Google Sheets API v4 文档的Appending values 部分提供了一个工作表中多个表格的很好示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2016-01-26
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    相关资源
    最近更新 更多