Adding Charts to Your Slides

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.

concept of adding a Google Sheets chart to a Slides presentation

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:

  1. Create a chart in Google Sheets.
  2. Use the Sheets API to read the chart ID of the resulting EmbeddedChart.
  3. Use CreateSheetsChartRequest once to add it to a slide.
  4. 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 never to 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 the a chart and adds it to a slide as LINKED so that it can be refreshed later.

Apps Script

slides/api/Snippets.gs
// Embed a Sheets chart (indicated by the spreadsheetId and sheetChartId) onto
// a page in the presentation. Setting the linking mode as 'LINKED' allows the
// chart to be refreshed if the Sheets version is updated.
var emu4M = {
  magnitude: 4000000,
  unit: 'EMU'
};
var presentationChartId = 'MyEmbeddedChart';
var requests = [{
  createSheetsChart: {
    objectId: presentationChartId,
    spreadsheetId: shapeId,
    chartId: sheetChartId,
    linkingMode: 'LINKED',
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU'
      }
    }
  }
}];

// Execute the request.
var batchUpdateResponse = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId);
console.log('Added a linked Sheets chart with ID: %s', presentationChartId);

Go

slides/snippets/presentations.go
// Embed a Sheets chart (indicated by the spreadsheetId and sheetChartId) onto
// a page in the presentation. Setting the linking mode as "LINKED" allows the
// chart to be refreshed if the Sheets version is updated.
emu4M := slides.Dimension{Magnitude: 4000000, Unit: "EMU"}
presentationChartId := "MyEmbeddedChart"
requests := []*slides.Request{{	CreateSheetsChart: &slides.CreateSheetsChartRequest{
		ObjectId:      presentationChartId,
		SpreadsheetId: spreadsheetId,
		ChartId:       sheetChartId,
		LinkingMode:   "LINKED",
		ElementProperties: &slides.PageElementProperties{
			PageObjectId: pageId,
			Size: &slides.Size{
				Height: &emu4M,
				Width:  &emu4M,
			},
			Transform: &slides.AffineTransform{
				ScaleX:     1.0,
				ScaleY:     1.0,
				TranslateX: 100000.0,
				TranslateY: 100000.0,
				Unit:       "EMU",
			},
		},
	},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Added a linked Sheets chart with ID %s", presentationChartId)

Java

slides/snippets/src/main/java/Snippets.java
// Embed a Sheets chart (indicated by the spreadsheetId and sheetChartId) onto
// a page in the presentation. Setting the linking mode as "LINKED" allows the
// chart to be refreshed if the Sheets version is updated.
List<Request> requests = new ArrayList<>();
Dimension emu4M = new Dimension().setMagnitude(4000000.0).setUnit("EMU");
String presentationChartId = "MyEmbeddedChart";
requests.add(new Request()
        .setCreateSheetsChart(new CreateSheetsChartRequest()
                .setObjectId(presentationChartId)
                .setSpreadsheetId(spreadsheetId)
                .setChartId(sheetChartId)
                .setLinkingMode("LINKED")
                .setElementProperties(new PageElementProperties()
                        .setPageObjectId(pageId)
                        .setSize(new Size()
                                .setHeight(emu4M)
                                .setWidth(emu4M))
                        .setTransform(new AffineTransform()
                                .setScaleX(1.0)
                                .setScaleY(1.0)
                                .setTranslateX(100000.0)
                                .setTranslateY(100000.0)
                                .setUnit("EMU")))));

// Execute the request.
BatchUpdatePresentationRequest body =
        new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
        slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Added a linked Sheets chart with ID " + presentationChartId);

JavaScript

slides/snippets/snippets.js
// Embed a Sheets chart (indicated by the spreadsheetId and sheetChartId) onto
// a page in the presentation. Setting the linking mode as "LINKED" allows the
// chart to be refreshed if the Sheets version is updated.
var emu4M = {
  magnitude: 4000000,
  unit: 'EMU'
};
var presentationChartId = 'MyEmbeddedChart';
var requests = [{
  createSheetsChart: {
    objectId: presentationChartId,
    spreadsheetId: shapeId,
    chartId: sheetChartId,
    linkingMode: 'LINKED',
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU'
      }
    }
  }
}];

// Execute the request.
gapi.client.slides.presentations.batchUpdate({
  presentationId: presentationId,
  requests: requests
}).then((batchUpdateResponse) => {
  console.log(`Added a linked Sheets chart with ID: ${presentationChartId}`);

Node.js

slides/snippets/snippets.js
// Embed a Sheets chart (indicated by the spreadsheetId and sheetChartId) onto
// a page in the presentation. Setting the linking mode as "LINKED" allows the
// chart to be refreshed if the Sheets version is updated.
let emu4M = {
  magnitude: 4000000,
  unit: 'EMU',
};
let presentationChartId = 'MyEmbeddedChart';
let requests = [{
  createSheetsChart: {
    objectId: presentationChartId,
    spreadsheetId: shapeId,
    chartId: sheetChartId,
    linkingMode: 'LINKED',
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M,
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU',
      },
    },
  },
}];

// Execute the request.
this.slidesService.presentations.batchUpdate({
  presentationId,
  resource: {
    requests,
  },
}, (err, batchUpdateResponse) => {
  console.log(`Added a linked Sheets chart with ID: ${presentationChartId}`);
  console.log(err, batchUpdateResponse);

PHP

slides/snippets/src/SlidesSnippets.php
<?php
// Embed a Sheets chart (indicated by the spreadsheet_id and sheet_chart_id) onto
// a page in the presentation. Setting the linking mode as "LINKED" allows the
// chart to be refreshed if the Sheets version is updated.
$presentationChartId = 'MyEmbeddedChart';
$emu4M = array('magnitude' => 4000000, 'unit' => 'EMU');
$requests = array();
$requests[] = new Google_Service_Slides_Request(array(
    'createSheetsChart' => array(
        'spreadsheetId' => $spreadsheetId,
        'chartId' => $sheetChartId,
        'linkingMode' => 'LINKED',
        'elementProperties' => array(
            'pageObjectId' => $pageId,
            'size' => array(
                'height' => $emu4M,
                'width' => $emu4M
            ),
            'transform' => array(
                'scaleX' => 1,
                'scaleY' => 1,
                'translateX' => 100000,
                'translateY' => 100000,
                'unit' => 'EMU'
            )
        )
    )
));

// Execute the request.
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
    'requests' => $requests
));
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
printf("Added a linked Sheets chart with ID: %s\n", $response->getPresentationId());

Python

slides/snippets/slides_snippets.py
# Embed a Sheets chart (indicated by the spreadsheet_id and
# sheet_chart_id) onto a page in the presentation.
# Setting the linking mode as "LINKED" allows the
# chart to be refreshed if the Sheets version is updated.
emu4M = {
    'magnitude': 4000000,
    'unit': 'EMU'
}
presentation_chart_id = 'MyEmbeddedChart'
requests = [
    {
        'createSheetsChart': {
            'objectId': presentation_chart_id,
            'spreadsheetId': spreadsheet_id,
            'chartId': sheet_chart_id,
            'linkingMode': 'LINKED',
            'elementProperties': {
                'pageObjectId': page_id,
                'size': {
                    'height': emu4M,
                    'width': emu4M
                },
                'transform': {
                    'scaleX': 1,
                    'scaleY': 1,
                    'translateX': 100000,
                    'translateY': 100000,
                    'unit': 'EMU'
                }
            }
        }
    }
]

# Execute the request.
body = {
    'requests': requests
}
response = slides_service.presentations().batchUpdate(
    presentationId=presentation_id, body=body).execute()
print('Added a linked Sheets chart with ID: {0}'.format(
    presentation_chart_id))

Ruby

slides/snippets/lib/file_snippets.rb
# Embed a Sheets chart (indicated by the spreadsheet_id and sheet_chart_id) onto
# a page in the presentation. Setting the linking mode as "LINKED" allows the
# chart to be refreshed if the Sheets version is updated.
emu4M = {
  magnitude: 4000000,
  unit:      'EMU'
}
presentation_chart_id = 'my_embedded_chart'
requests = [{
  create_sheets_chart: {
    object_id_prop:     presentation_chart_id,
    spreadsheet_id:     spreadsheet_id,
    chart_id:           sheet_chart_id,
    linking_mode:       'LINKED',
    element_properties: {
      page_object_id: page_id,
      size:           {
        height: emu4M,
        width:  emu4M
      },
      transform:      {
        scale_x:     1,
        scale_y:     1,
        translate_x: 100000,
        translate_y: 100000,
        unit:        'EMU'
      }
    }
  }
}]

# Execute the request.
req = Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests)
response = slides_service.batch_update_presentation(presentation_id, req)

puts "Added a linked Sheets chart with ID: #{presentation_chart_id}"

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:

Apps Script

slides/api/Snippets.gs
var requests = [{
  refreshSheetsChart: {
    objectId: presentationChartId
  }
}];

// Execute the request.
var batchUpdateResponse = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId);
console.log('Refreshed a linked Sheets chart with ID: %s', presentationChartId);

Go

slides/snippets/presentations.go
requests := []*slides.Request{{	RefreshSheetsChart: &slides.RefreshSheetsChartRequest{
		ObjectId: presentationChartId,
	},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Refreshed a linked Sheets chart with ID %s", presentationChartId)

Java

slides/snippets/src/main/java/Snippets.java
List<Request> requests = new ArrayList<>();

// Refresh an existing linked Sheets chart embedded a presentation.
requests.add(new Request()
        .setRefreshSheetsChart(new RefreshSheetsChartRequest()
                .setObjectId(presentationChartId)));

// Execute the request.
BatchUpdatePresentationRequest body =
        new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
        slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Refreshed a linked Sheets chart with ID " + presentationChartId);

JavaScript

slides/snippets/snippets.js
var requests = [{
  refreshSheetsChart: {
    objectId: presentationChartId
  }
}];

// Execute the request.
gapi.client.slides.presentations.batchUpdate({
  presentationId: presentationId,
  requests: requests
}).then((batchUpdateResponse) => {
  console.log(`Refreshed a linked Sheets chart with ID: ${presentationChartId}`);
});

Node.js

slides/snippets/snippets.js
let requests = [{
  refreshSheetsChart: {
    objectId: presentationChartId,
  },
}];

// Execute the request.
this.slidesService.presentations.batchUpdate({
  presentationId,
  resource: {
    requests,
  },
}, (err, batchUpdateResponse) => {
  console.log(`Refreshed a linked Sheets chart with ID: ${presentationChartId}`);
});

PHP

slides/snippets/src/SlidesSnippets.php
<?php
// Refresh an existing linked Sheets chart embedded in a presentation.
$requests = array();
$requests[] = new Google_Service_Slides_Request(array(
    'refreshSheetsChart' => array(
        'objectId' => $presentationChartId
    )
));

// Execute the request.
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
    'requests' => $requests
));
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
printf("Refreshed a linked Sheets chart with ID: %s\n", $response->getPresentationId());

Python

slides/snippets/slides_snippets.py
# Refresh an existing linked Sheets chart embedded in a presentation.
requests = [
    {
        'refreshSheetsChart': {
            'objectId': presentation_chart_id
        }
    }
]

# Execute the request.
body = {
    'requests': requests
}
response = slides_service.presentations().batchUpdate(
    presentationId=presentation_id, body=body).execute()
print('Refreshed a linked Sheets chart with ID: {0}' \
    .format(presentation_chart_id))

Ruby

slides/snippets/lib/file_snippets.rb
# Refresh an existing linked Sheets chart embedded in a presentation.
requests = [{
  refresh_sheets_chart: {
    object_id_prop: presentation_chart_id
  }
}]

# Execute the request.
req = Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests)
response = slides_service.batch_update_presentation(presentation_id, req)

puts "Refreshed a linked Sheets chart with ID: #{presentation_chart_id}"