从第一张表格中提取信息时如何更新其他Google表格?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从第一张表格中提取信息时如何更新其他Google表格?相关的知识,希望对你有一定的参考价值。
我能够从googlesheets中提取并更新我从中提取的同一张工作表。 (该脚本仅适用于Google电子表格的第一张工作表。)
从第一张纸上拉出来后,我想生成一个有效的报告,但是我不希望它覆盖第一张纸,而是希望它将信息放在我制作的另一张纸上。这是我尝试更改脚本以使用其他工作表时遇到的问题。
def get_google_sheet(spreadsheet_id, range_name):
# Retrieve sheet data using OAuth credentials and Google Python API.
global scopes
global gsheet
scopes = ['https://www.googleapis.com/auth/spreadsheets']
# Setup the Sheets API
store = file.Storage('credentials1.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', scopes)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
gsheet = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
return gsheet
def gsheet2df(gsheet):
# Converts Google sheet data to a Pandas DataFrame.
header = gsheet.get('values', [])[0] # Assumes first line is header!
values = gsheet.get('values', [])[1:] # Everything else is data.
#header = values[0]
if not values:
print('No data found.')
else:
df = pd.DataFrame.from_records(values)
df.columns = header
return df
def Export_Data_To_Sheets():
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', scopes)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
RANGE_AND_SHEET = "ReportListA1:E500"
response_date = service.spreadsheets().values().update(
spreadsheetId=SPREADSHEET_ID,
valueInputOption='RAW',
range=RANGE_AND_SHEET,
body=dict(
majorDimension='ROWS',
values=df.T.reset_index().T.values.tolist())
).execute()
print('Sheet successfully Updated')
当我只使用范围而不识别工作表时,它没有问题。使用范围和工作表,我会收到此错误。
Traceback (most recent call last):
File "/Users/adaniel/PycharmProjects/EmailAutomation/venv/AutoEmailScript/test.py", line 126, in <module>
Export_Data_To_Sheets()
File "/Users/adaniel/PycharmProjects/EmailAutomation/venv/AutoEmailScript/test.py", line 68, in Export_Data_To_Sheets
response_date = service.spreadsheets().values().update(
File "/Users/adaniels/PycharmProjects/EmailAutomation/venv/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/adaniels/PycharmProjects/EmailAutomation/venv/lib/python3.8/site-packages/googleapiclient/http.py", line 840, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/*******yAABhNJvARr9BzsVLvCYSfpcvQAmpWESL-DJI/values/%28%27ReportList%21%27%2C%20%27A1%3AE500%27%29?valueInputOption=RAW&alt=json returned "Invalid JSON payload received. Unexpected token.
House", "10/19/19", NaN, NaN, NaN], ["Co
^">
I already have two different credential files for different authentication scopes. I also deleted them and reauthenticated, so I'm not sure why I'm getting a 400 error.
答案
答案:
您需要在请求的range
部分中指定要更新的工作表。
更多信息:
[service.open('SheetName').sheet
不是东西-open
不是该服务的方法,Sheets服务目前不知道您要更新哪个Spreadsheet的值。
根据the documentation,您需要在range参数中指定要更新的值的范围。这包括工作表名称。
代码段:
RANGE_AND_SHEET = "ReportList!" + RANGE_NAME
response_date = service.spreadsheets().values().update(
spreadsheetId=SPREADSHEET_ID,
range=RANGE_AND_SHEET,
valueInputOption='RAW',
body=value_range_body).execute()
希望对您有帮助!
参考:
以上是关于从第一张表格中提取信息时如何更新其他Google表格?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 python 将给定的 PDF 提取到文本和表格并将数据存储在 .csv 文件中?
如何在同一电子表格中获取已发布的Google工作表的不同网址
在 BigQuery Cloud Shell 中从 Google 表格创建外部表格时,如何选择默认表格(第一张表格)以外的表格?