I'm having problems creating a scatter plot in Google sheet API in Python. I've already been able to create the new sheet but with NO DATA. Also, I'm not sure I am adding the necessary number of variables for the scatter plot.
I've ran a test on updating the information in a new sheet and it did work well but I'm still having problem on creating a new chart
Here is my code:
from googleapiclient.discovery import build
from google.oauth2 import service_account
SERVICE_ACCOUNT_FILE = 'keys.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
The ID spreadsheet
SAMPLE_SPREADSHEET_ID = 'Wl4g72ovwE_H0IH3unrtt0oTr5shWFY'
sheet_Id = '1005435407'
service = build('sheets', 'v4', credentials=creds)
Call the Sheets API
sheet = service.spreadsheets()
Chart
request_body = {
"requests": [
{
"addChart": {
"chart": {
"spec": {
"title": "Categorical Expenses",
"basicChart": {
"chartType": "SCATTER",
"legendPosition": "BOTTOM_LEGEND",
"axis": [
# X-AXIS
{
"position": "BOTTOM_AXIS",
"title": "Clusters"
},
# Y-AXIS
{
"position": "LEFT_AXIS",
"title": "Dimensions"
}
],
# Chart labels
'domains': [
{
'domain': {
'sourceRange': {
'sources': [
{
"sheetId": sheet_Id,
"startRowIndex": 0, # Row # 1
"endRowIndex": 1001, # Row # 1000
"startColumnIndex": 9, # Column J
"endColumnIndex": 10 # Column K
}
]
}
}
}
],
"series": [
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_Id,
"startRowIndex": 0, # Row # 1
"endRowIndex": 1001, # Row # 1000
"startColumnIndex": 9, # Column J
"endColumnIndex": 10 # Column K
}
]
}
},
"targetAxis": "LEFT_AXIS"
}
]
}
},
"position": {
"newSheet": True
}
}
}
}
]
}
response = sheet.batchUpdate(
spreadsheetId=SAMPLE_SPREADSHEET_ID,
body=request_body).execute()
print('chart created successfully')