i was trying to make a project using google api. in that project i give a string and the python script is supposed to change/modify that a cell in google sheet and change the value of the cell that string. But the problem is that there is a error-
request = sheet.service.spreadsheets().values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID, AttributeError: 'Resource' object has no attribute 'service'
what should i do ?
here is the code -
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)
CodePudding user response:
How about modifying as follows?
From:
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()
To:
value_for_write = [["present"]]
request = sheet.values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID, range="Sheet2!A1", valueInputOption="USER_ENTERED", body={"values":value_for_write}).execute()
- In your script,
sheet = service.spreadsheets()
is used. So I used it. value_for_write
of{"values":value_for_write}
forsheet.values().update
is required to be 2 dimensional array.