Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Google工作表的第一个空行中插入值_Python_Google Sheets - Fatal编程技术网

Python 在Google工作表的第一个空行中插入值

Python 在Google工作表的第一个空行中插入值,python,google-sheets,Python,Google Sheets,尝试运行python函数,该函数将在电子表格的第一个空行中插入一些值。我不知道如何找到第一个空行并向其中插入数据 以下是我的代码示例: scope = ["https://spreadsheets.google.com/feeds","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

尝试运行python函数,该函数将在电子表格的第一个空行中插入一些值。我不知道如何找到第一个空行并向其中插入数据

以下是我的代码示例:

scope = ["https://spreadsheets.google.com/feeds","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
sheet = client.open('Payment Tracker').sheet1
data = sheet.get_all_records()
today = datetime.today().strftime('%m-%d-%Y')
insertRow = ['Person','1000',today,'Bank']
row = sheet.row_values(4)
sheet.insert_row(row, 2)

我相信你的目标如下

  • 您希望使用带有python的gspread将
    insertRow
    的值放入工作表中的第一个空行
对于这个问题,这个答案如何

模式1: 在此模式中,使用了
append\u行
。在这种情况下,这些值将放在图纸的最后一行

修改脚本: 请按如下方式修改您的脚本

发件人: 致: 模式2: 在该模式中,值被放置到图纸的第一个空行。在这种情况下,当数据范围为“A1:A5”下的空行为“A3”时,该值被放入第3行

修改脚本: 请按如下方式修改您的脚本

发件人: 致: 参考资料:
row = sheet.row_values(4)
sheet.insert_row(row, 2)
sheet.append_row(insertRow, value_input_option='USER_ENTERED')
data = sheet.get_all_records()
today = datetime.today().strftime('%m-%d-%Y')
insertRow = ['Person','1000',today,'Bank']
row = sheet.row_values(4)
sheet.insert_row(row, 2)
data = sheet.get_all_values()
row = 0
for i, r in enumerate(data):
    if all([c == '' for c in r]) is True:
        row = i + 1
        break
today = datetime.today().strftime('%m-%d-%Y')
insertRow = ['Person', '1000', today, 'Bank']
spreadsheet.values_update('A' + str(row), params={'value_input_option': 'USER_ENTERED'}, body={'values': [insertRow]})