Python Google 工作表无法写入 Col B

Python With Google Sheets Can't Write to Col B

我有两个列表,我试图将它们分别写入 google 工作表文档的 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)

这导致了一个错误,指出它是一个无效的数据类型。我怎样才能让它按照我的要求去做?

我相信你的目标如下。

  • 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 Unfortunately, I cannot understand about It is two lists, not a lot of lists. The values of fList and gList are fList = [['781411'],['781415'],['781412'],['781416'],['701355'],['70‌​1330'],['701346'],['‌​701352'],['782153'],‌​['701354'],['781347'‌​],['781345']] and gList = [['.57'],['.91'],['.63'],['.63'],['5.20'],['$‌​196.90'],['2.60']‌​,['6.10'],['.2‌​2'],['6.70'],['‌​87.30'],['7.50']]‌​?

你说的如下。

Correct, yes. Those are those lists

根据这种情况,我使用 fList = [['781411'],['781415'],['781412'],['781416'],['701355'],['70‌​1330'],['701346'],['‌​701352'],['782153'],‌​['701354'],['781347'‌​],['781345']]gList = [['.57'],['.91'],['.63'],['.63'],['5.20'],['$‌​196.90'],['2.60']‌​,['6.10'],['.2‌​2'],['6.70'],['‌​87.30'],['7.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 = [['.57'], ['.91'], ['.63'], ['.63'], ['5.20'], ['6.90'], ['2.60'], ['6.10'], ['.22'], ['6.70'], ['7.30'], ['7.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)])