I am working on a python script to retrieve and work with data from Google Sheets. So far, I can log in with no issues and read/write cell data.
However, as far as some cells containing formulas are concerned, the value python returns is #DIV/0
!, instead of the result I see in the cell of the spreadsheet itself.
Why is that? I tried to specify the "Value Render Option" parameter as either FORMATTED_VALUE
, UNFORMATTED_VALUE
or FORMULA
, but nothing works.
For example, one cell contains an IF formula, which shows up as "Positive" or "Negative" in the sheet itself. I want python to retrieve this value. Instead, it gets me #DIV/0!
when I print out the values. Same goes for a calculation I do in the sheet itself (which generates a proper result when I view the sheet online).
My script resembles the example ones on the Google tutorial pages.
Thanks for helping out a begginer! Cheers!
Here's the script:
from __future__ import print_function
from pprint import pprint
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient import discovery
import pickle
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
spreadsheet_id = '1-V95KxzJCPglRa90KnvnHBvY2doffeAxuRrEBEA0mHg'
range_ = 'Finance_3!A1:D30'
credentials = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
credentials = pickle.load(token)
service = discovery.build('sheets', 'v4', credentials=credentials)
value_render_option = 'FORMATTED_VALUE'
date_time_render_option = 'FORMATTED_STRING'
request = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_, valueRenderOption=value_render_option, dateTimeRenderOption=date_time_render_option)
response = request.execute()
pprint(response)