我有一个连接到我的python代码的Google表格 我想让python只在第一行搜索数字 例如,如果输入为“ 3”,则从第一行中找到3,然后输入日期或其他内容 这是我的代码:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import datetime
scope = ['https://www.googleapis.com/auth/spreadsheets', "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json.json',scope)
a = raw_input("Type your Number")
client = gspread.authorize(credentials)
date = datetime.datetime.now()
sheet = client.open('hi').sheet1
if raw_input == '1':
sheet.update_acell('B2', 'This Value for B2')
else:
print('r')
#print(sheet.get_all_records())
#sheet.append_row([str(date), 'this goes'])
答案 0 :(得分:0)
让我们将第一行的值存储在列表中,并遍历它以找到匹配项并获取列号以更新该列。
我试图更正代码。请尝试运行此程序,它应该适合您。 :)
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import datetime
scope = ['https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"]
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json.json',scope)
a = raw_input("Type your Number")
client = gspread.authorize(credentials)
date = datetime.datetime.now()
sheet = client.open('hi').sheet1
values_list = sheet.row_values(1) #Get first row values as a list
col_num = 1 #To get the column number
flag = 0
#Traverse the list to find the match
for item in values_list:
if a == item:
flag=1
break
col_num = col_num + 1
if flag == 1:
sheet.update_cell(1, col_num, 'Updated Value')
else:
print('r')
#print(sheet.get_all_records())
#sheet.append_row([str(date), 'this goes']