【问题标题】:Python With Google Sheets Can't Write to Col B带有 Google 表格的 Python 无法写入 Col B
【发布时间】:2021-03-01 00:24:21
【问题描述】:

我有两个列表,我正试图将它们分别写入谷歌表格文档的 A 列和 B 列,但我只能让它写入 A 列,只是在行中交替。

这是我尝试写入 !A,

sheet.insert_row(fList)
sheet.insert_row(gList)

这就是我尝试将其添加到 COL !A-!B

    rows = [fList, gList]
    for row in rows:
        for column in row:
            sheet.insert_row(column)
        sheet.add_cols(1)

这导致错误,指出它是无效的数据类型。我如何让它完成我的要求?

【问题讨论】:

    标签: python python-3.x google-sheets google-api gspread


    【解决方案1】:

    我相信你的目标如下。

    • fListgList 是一维数组。
    • 您想将fListgList 的值分别放在“A”和“B”列中。
    • 您希望使用 Python 的 gspread 来实现此目的。

    在你的情况下,使用“更新”的方法怎么样?示例脚本如下。

    示例脚本:

    fList = ['a1', 'a2', 'a3'] # Please set your actual value.
    gList = ['b1', 'b2', 'b3', 'b4', 'b5'] # Please set your actual value.
    spreadsheetId = "###" # Please set your Spreadsheet ID.
    sheetName = "Sheet1" # Please set your sheet name.
    
    rows = [fList, gList]
    client = gspread.authorize(credentials)
    spreadsheet = client.open_by_key(spreadsheetId)
    sheet = spreadsheet.worksheet(sheetName)
    length = max(map(len, rows)) # If the array length of "fList" and "gList" are the same, this line is not required to be used.
    rows = [e + [''] * (length - len(e)) for e in rows] # If the array length of "fList" and "gList" are the same, this line is not required to be used.
    sheet.update('A1', [list(e) for e in zip(*rows)])
    

    参考:

    补充:

    对于我的以下评论,

    @GabeCunningham 不幸的是,我无法理解这是两个列表,而不是很多列表。 fListgList的值为fList = [['781411'],['781415'],['781412'],['781416'],['701355'],['70‌​1330'],['701346'],['‌​701352'],['782153'],‌​['701354'],['781347'‌​],['781345']]gList = [['$81.57'],['$80.91'],['$91.63'],['$91.63'],['$455.20'],['$‌​196.90'],['$282.60']‌​,['$146.10'],['$97.2‌​2'],['$166.70'],['$2‌​87.30'],['$237.50']]‌​

    你说的如下。

    正确,是的。那些是那些列表

    根据这种情况,我使用fList = [['781411'],['781415'],['781412'],['781416'],['701355'],['70‌​1330'],['701346'],['‌​701352'],['782153'],‌​['701354'],['781347'‌​],['781345']]gList = [['$81.57'],['$80.91'],['$91.63'],['$91.63'],['$455.20'],['$‌​196.90'],['$282.60']‌​,['$146.10'],['$97.2‌​2'],['$166.70'],['$2‌​87.30'],['$237.50']]‌​ 的值添加了一个示例脚本。

    示例脚本:

    spreadsheetId = "###" # Please set your Spreadsheet ID.
    sheetName = "Sheet1" # Please set your sheet name.
    
    # These values are from your comment.
    fList = [['781411'], ['781415'], ['781412'], ['781416'], ['701355'], ['701330'], ['701346'], ['701352'], ['782153'], ['701354'], ['781347'], ['781345']]
    gList = [['$81.57'], ['$80.91'], ['$91.63'], ['$91.63'], ['$455.20'], ['$196.90'], ['$282.60'], ['$146.10'], ['$97.22'], ['$166.70'], ['$287.30'], ['$237.50']]
    
    rows = [sum(fList, []), sum(gList, [])]
    client = gspread.authorize(credentials)
    spreadsheet = client.open_by_key(spreadsheetId)
    sheet = spreadsheet.worksheet(sheetName)
    sheet.update('A1', [list(e) for e in zip(*rows)])
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多