我正在嘗試使用 google api 制作一個專案。在那個專案中,我給出了一個字串,python 腳本應該更改/修改谷歌作業表中的單元格并更改該字串單元格的值。但問題是出現了一個錯誤——
request = sheet.service.spreadsheets().values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID, AttributeError: 'Resource' object has no attribute 'service'
我該怎么辦 ?
這是代碼 -
from __future__ import print_function
from googleapiclient.discovery import build
from google.oauth2 import service_account
SERVICE_ACCOUNT_FILE = 'python-sheets-keys.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# If modifying these scopes, delete the file token.json.
# The ID of the spreadsheet.
SAMPLE_SPREADSHEET_ID = '14uNIk1Q_jNKRL-yuz5Fssu3iVsQzmYf6wHXZDnjWkW0'
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range="test!A1:B10").execute()
values = result.get('values', [])
value_for_write = [("present")]
request = sheet.service.spreadsheets().values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range="Sheet2!A1", valueInputOption="USER_ENTERED", body={"values":value_for_write}).execute()
print(values)
uj5u.com熱心網友回復:
如下修改如何?
從:
value_for_write = [("present")]
request = sheet.service.spreadsheets().values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range="Sheet2!A1", valueInputOption="USER_ENTERED", body={"values":value_for_write}).execute()
到:
value_for_write = [["present"]]
request = sheet.values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID, range="Sheet2!A1", valueInputOption="USER_ENTERED", body={"values":value_for_write}).execute()
- 在您的腳本中,
sheet = service.spreadsheets()使用了。所以我用了它。 value_for_writeof{"values":value_for_write}forsheet.values().update必須是二維陣列。
參考:
- 方法:spreadsheets.values.update
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360222.html
標籤:Python 谷歌表格 google-sheets-api
上一篇:Google表格-將文本中的引號""替換為希臘語??
下一篇:為什么ContactsApp.getContactsByEmailAdress('[email protected]')使用GoogleApps腳本回傳[Contact]?
