This page describes how to take existing charts from Google Sheets and add them to Google Slides presentations.
Adding charts to a presentation can help create more impact and make data meaningful to your audience. Charts are uniquely powerful tools for showing patterns or trends in a data set, and can help you communicate clearly about quantitative data.
The above video discusses how to generate slides from spreadsheet data. It includes a complete example (Python) that adds a chart to a slide, and also imports spreadsheet cell data into a table.
About adding charts
Whenever you're presenting quantitative information, data charts can enhance your presentation. The Slides API lets you include any chart that you can create in Google Sheets: bar charts, line charts, pie charts, and many more.
As shown in the diagram above, you must first create the chart in Google Sheets. Once the chart exists, you can embed it into your presentation.
The general process for managing charts in your presentation is:
- Create a chart in Google Sheets.
- Use the Sheets API to read the chart ID of the resulting EmbeddedChart.
- Use CreateSheetsChartRequest once to add it to a slide.
- Use RefreshSheetsChartRequest as needed to sync it to the source data.
Static vs linked charts
When you use the API to add a chart to your presentation, one parameter that you specify is the LinkingMode. This determines whether the chart is added as a static image or as an object that can be refreshed in the future.
Use LINKED
mode if:
- You want to refresh the chart in the future, reflecting changes in the underlying data.
- You want collaborators to have a link to the spreadsheet containing the source chart.
Use NOT_LINKED_IMAGE
(static) mode if:
- You want the chart to never change in the future.
- You do not want collaborators to see a link to the source spreadsheet.
Scopes for accessing Google Sheets
When you add a chart to your slide, the Slides API needs to access it in Google Sheets. This means your request must use one of the following scopes:
https://www.googleapis.com/auth/spreadsheets.readonly
(preferred)https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive
The spreadsheets.readonly
scope is generally the best to use, because it is
the most restrictive. However, if your app already uses one of the other scopes
listed above, then just use that scope.
Adding a chart
To add a chart, you'll need to know the spreadsheet ID and chart ID of the
chart in Google Sheets. Then you call the batchUpdate
method, using the
CreateSheetsChartRequest
once to add the chart.
The following example takes a chart and adds it to a slide as LINKED
so
that it can be refreshed later.
Apps Script
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Refreshing a chart
If you embedded a LINKED
chart in your presentation, then you can refresh it
at any time using
RefreshSheetsChartRequest.
This updates the chart to bring it in sync with the underlying Sheets chart and
data that it's based on.
The following example refreshes an existing chart: