Skip to content

Instantly share code, notes, and snippets.

@blackrez
Last active February 18, 2020 08:08
Show Gist options
  • Save blackrez/d1e7642d048566e4ea4c11a7941773c8 to your computer and use it in GitHub Desktop.
Save blackrez/d1e7642d048566e4ea4c11a7941773c8 to your computer and use it in GitHub Desktop.
Creation of a basic chart.
from googleapiclient.discovery import build
from google.oauth2 import service_account
SCOPES = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets"]
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)
spreadsheetId = "O188999ITC"
sheetid_src = "123a"
chart = {'addChart':{'chart':{'position':{"newSheet":True},'spec':{'title':'chart','basicChart': {'chartType': 'AREA', 'legendPosition': 'RIGHT_LEGEND',
'axis': [{'position': 'BOTTOM_AXIS', 'viewWindowOptions': {}},
{'position': 'LEFT_AXIS', 'viewWindowOptions': {}}],
'domains': [{'domain': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 0,
'endColumnIndex': 1}]}}}],
'series': [{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 1,
'endColumnIndex': 2}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 2,
'endColumnIndex': 3}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 3,
'endColumnIndex': 4}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 4,
'endColumnIndex': 5}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 5,
'endColumnIndex': 6}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 6,
'endColumnIndex': 7}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 7,
'endColumnIndex': 8}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 8,
'endColumnIndex': 9}]}},
'targetAxis': 'LEFT_AXIS',
'lineStyle': {'width': 1}},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 9,
'endColumnIndex': 10}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 10,
'endColumnIndex': 11}]}},
'targetAxis': 'LEFT_AXIS'},
{'series': {'sourceRange': {'sources': [{'sheetId': sheetid_src,
'startRowIndex': 1,
'endRowIndex': 1000,
'startColumnIndex': 11,
'endColumnIndex': 12}]}},
'targetAxis': 'LEFT_AXIS'}],
'stackedType': 'STACKED',
'lineSmoothing': False},
'hiddenDimensionStrategy': 'SKIP_HIDDEN_ROWS_AND_COLUMNS',
'fontName': 'Roboto'},
}}}
request = {'requests': chart)}
response = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=request).execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment