简体   繁体   English

如何使用 Python 中的列表、数组或循环将 append 行值添加到 Google 工作表?

[英]How can I append row values to a Google sheet using a list, array or loop in Python?

I have a Python script which is called by Bash script with arguments (parameters) using sys.我有一个 Python 脚本,它由 Bash 脚本和 arguments(参数)使用 sys 调用。 I can successfully append these parameters to a Google Sheet when they are included as individual values in the worksheet.append.rows command.当这些参数作为单独的值包含在 worksheet.append.rows 命令中时,我可以成功地将这些参数 append 添加到 Google 表格。 If the number of parameters in the call do not match the number of values in the append command, it fails.如果调用中的参数数量与 append 命令中的值数量不匹配,它将失败。

It's easy to find the length of the sys.argv array and print the values in a loop.很容易找到 sys.argv 数组的长度并在循环中打印值。 How can I provide the worksheet.append.rows command with a similarly dynamic list of the arguments passed by the Bash script, removing the need to hard code the number of values?如何为 worksheet.append.rows 命令提供 Bash 脚本传递的类似动态列表 arguments,从而无需对值的数量进行硬编码?

Posts on this topic seem to me to be rather complicated.在我看来,关于这个主题的帖子相当复杂。 I'm hoping that there is a simple solution out there.我希望那里有一个简单的解决方案。

This is my first question and I am new to Python!这是我的第一个问题,我是 Python 的新手!

import sys
import gspread

for i in range(len(sys.argv)):
    print(sys.argv[i])

gc = gspread.service_account(filename='gsa.json')

worksheet = gc.open("gfx-garden-meta").sheet1

# this is where I am stuck for a flexible way to supply the right number of values to the append command:

worksheet.append_rows(values=[[(sys.argv[1]), (sys.argv[2]), (sys.argv[3]), (sys.argv[4]), (sys.argv[5]), (sys.argv[6])]])

#

exit()

Below will create a list of lists, where each inner list contains a single element, which is an argument passed by the Bash script.下面将创建一个列表列表,其中每个内部列表包含一个元素,这是 Bash 脚本传递的参数。 Then you can pass that list to append_rows function然后你可以将该列表传递给 append_rows function

values = [[arg] for arg in sys.argv[1:]]
worksheet.append_rows(values)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中,如何使用循环将行添加/添加到numpy数组中而不删除前一行? - In python, how do I append/add, using a loop, a row to a numpy array without deleting the previous row? 我如何使用 python 在谷歌表格中的行尾 append - How do I append at the end of row in in google sheets using python 如何使用Python将列表的值附加到DataFrame列的值? - How can I append the values of a list to the values of a DataFrame column using Python? 如何使用 for 循环 append python 中的列表并替换 function? - How do I append a list in python using a for loop and replace function? Python - 初学者帮助 - 如何将 append 多个值添加到一个列表中? - Python - beginner help - How can I append multiple values to a list? 如何使用 Python 根据匹配的键值在字典列表中附加其他数据 - How can I append other data in a list of dictionaries based on matching key values using Python 从 Python 在 Google Sheet 中附加一个列表 - Append a list in Google Sheet from Python 如何使用 Python 检查 excel 表的值? - How can I check values of an excel sheet using Python? 从python中的循环将值附加到单个列表或数组 - append values to a single list or array from a loop in python 使用python在csv中每一行的末尾附加字典或值列表 - Append Dictionary or list of values at the end of each row in csv using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM