我相信你的目标如下。
-
fList 和 gList 是一维数组。
- 您想将
fList 和gList 的值分别放在“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 不幸的是,我无法理解这是两个列表,而不是很多列表。 fList和gList的值为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']]?
你说的如下。
正确,是的。那些是那些列表
根据这种情况,我使用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']] 的值添加了一个示例脚本。
示例脚本:
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)])