我正在处理一个python脚本,该脚本使用Google Spreadsheet API创建电子表格,然后从中读取值。 我的问题是,要创建电子表格,我想使用模板或复制现有的电子表格。因为我不想仅使用drive.file范围来使用电子表格.read,所以无法从公共模板表中读取。有什么方法可以解决此问题,并且仍将特定的工作表用作模板,而无需扩大范围?
我要访问的代码如下:
dest_body = {
'destination_spreadsheet_id': new_spreadsheet_id,
}
jobs_sheet = service.spreadsheets().sheets().copyTo(
spreadsheetId=template_id, sheetId=SHEETID, body=dest_body).execute()
访问我的模板表(已公开共享)时,出现此错误:
<HttpError 404 when requesting ... returned "Requested entity was not found.">
使用此功能创建的服务,也可以在https://developers.google.com/sheets/api/quickstart/python处找到:
def get_authenticated_service_sheets():
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRETS_FILE, ['https://www.googleapis.com/auth/drive.file'])
if os.path.exists('src/util/token.pickle'):
with open('src/util/token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
creds = flow.run_local_server()
with open('src/util/token.pickle', 'wb') as token:
pickle.dump(creds, token)
return build('sheets', 'v4', credentials=creds)