Bri*_*ani 4 python google-sheets python-3.x google-sheets-api
我有一张带有数据的谷歌工作表,但它每天都有一个问题,我必须输入数据(相同类型的数据),有人知道如何使用 python 在谷歌工作表中附加数据,帮助我。
我有这种类型的结果,并且是字符串形式的
print(time, " ", todayMaxProfit, " ", todayMaxLoss, " ", pl, " ", len(
orderList), " First pair sum:- ", int(orderList[0][4]+orderList[1][4]))
"2021-08-18 15:00:00 [1451, '2021-08-18 11:07:00'] [-10203, '2021-08-18 14:45:00'] -6900 2 First pair sum:- 234"
Run Code Online (Sandbox Code Playgroud)
我想最后追加数据。
按照快速入门进行设置。确保您完全遵循所有步骤!您需要为您启动的每个使用 API 的项目执行此操作,因此您不妨按照此处的说明进行操作。在继续之前确保获得预期的输出!
然后,您可以修改快速入门以获取service单独的函数:
def getService():
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
return build('sheets', 'v4', credentials=creds)
Run Code Online (Sandbox Code Playgroud)
service = getService()
appendValues(service)
values = [
[time, todayMaxProfit, todayMaxLoss, pl, len(orderList), int(orderList[0][4]+orderList[1][4])]
]
body = {'values': values}
result = service.spreadsheets().values().append(
spreadsheetId="13rdolwpUD4h4RTuEgn1QbtgPMpJiZGMFubdh4loAfNQ", range="Sheet1!A1",
valueInputOption="RAW", body=body).execute()
Run Code Online (Sandbox Code Playgroud)
请注意,必须采用的格式values是二维列表:
[
[A1, B1, C1],
[A2, B2, C2]
]
Run Code Online (Sandbox Code Playgroud)
使用该append方法,只需将行按原样添加到工作表的末尾。这append需要一些参数:
id您想要附加值的电子表格的 IDA1通常,如果您只有一个从底部到底部填充数据的表格,则可以将其保留为A1,或者C5如果您有标题或空格,则可以将其保留为 。这个想法是将 API 指向您想要附加的数据集合。| 归档时间: |
|
| 查看次数: |
6212 次 |
| 最近记录: |