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 API 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 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

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.
 * @param {string} presentationId
 * @param {string} pageId
 * @param {string} shapeId
 * @param {string} sheetChartId
 * @returns {*}
 */
functioncreateSheetsChart(presentationId,pageId,shapeId,sheetChartId){
constemu4M={
magnitude:4000000,
unit:"EMU",
};
constpresentationChartId="MyEmbeddedChart";
constrequests=[
{
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.
try{
constbatchUpdateResponse=Slides.Presentations.batchUpdate(
{
requests:requests,
},
presentationId,
);
console.log("Added a linked Sheets chart with ID: %s",presentationChartId);
returnbatchUpdateResponse;
}catch(err){
// TODO (Developer) - Handle exception
console.log("Failed with error: %s",err.error);
}
}

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/CreateSheetsChart.java
importcom.google.api.client.googleapis.json.GoogleJsonError;
importcom.google.api.client.googleapis.json.GoogleJsonResponseException;
importcom.google.api.client.http.HttpRequestInitializer;
importcom.google.api.client.http.javanet.NetHttpTransport;
importcom.google.api.client.json.gson.GsonFactory;
importcom.google.api.services.slides.v1.Slides;
importcom.google.api.services.slides.v1.SlidesScopes;
importcom.google.api.services.slides.v1.model.AffineTransform;
importcom.google.api.services.slides.v1.model.BatchUpdatePresentationRequest;
importcom.google.api.services.slides.v1.model.BatchUpdatePresentationResponse;
importcom.google.api.services.slides.v1.model.CreateSheetsChartRequest;
importcom.google.api.services.slides.v1.model.Dimension;
importcom.google.api.services.slides.v1.model.PageElementProperties;
importcom.google.api.services.slides.v1.model.Request;
importcom.google.api.services.slides.v1.model.Size;
importcom.google.auth.http.HttpCredentialsAdapter;
importcom.google.auth.oauth2.GoogleCredentials;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
/* Class to demonstrate the use of Slides Create Chart API */
publicclass CreateSheetsChart{
/**
 * Adds chart from spreadsheet to slides as linked.
 *
 * @param presentationId - id of the presentation.
 * @param pageId - id of the page.
 * @param spreadsheetId - id of the spreadsheet.
 * @param sheetChartId - id of the chart in sheets.
 * @return presentation chart id
 * @throws IOException - if credentials file not found.
 */
publicstaticBatchUpdatePresentationResponsecreateSheetsChart(
StringpresentationId,StringpageId,StringspreadsheetId,IntegersheetChartId)
throwsIOException{
/* Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity for
 guides on implementing OAuth2 for your application. */
GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(SlidesScopes.PRESENTATIONS));
HttpRequestInitializerrequestInitializer=newHttpCredentialsAdapter(
credentials);
// Create the slides API client
Slidesservice=newSlides.Builder(newNetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Slides samples")
.build();
// 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=newArrayList<>();
Dimensionemu4M=newDimension().setMagnitude(4000000.0).setUnit("EMU");
StringpresentationChartId="MyEmbeddedChart";
requests.add(newRequest()
.setCreateSheetsChart(newCreateSheetsChartRequest()
.setObjectId(presentationChartId)
.setSpreadsheetId(spreadsheetId)
.setChartId(sheetChartId)
.setLinkingMode("LINKED")
.setElementProperties(newPageElementProperties()
.setPageObjectId(pageId)
.setSize(newSize()
.setHeight(emu4M)
.setWidth(emu4M))
.setTransform(newAffineTransform()
.setScaleX(1.0)
.setScaleY(1.0)
.setTranslateX(100000.0)
.setTranslateY(100000.0)
.setUnit("EMU")))));
BatchUpdatePresentationResponseresponse=null;
try{
// Execute the request.
BatchUpdatePresentationRequestbody=
newBatchUpdatePresentationRequest().setRequests(requests);
response=service.presentations().batchUpdate(presentationId,body).execute();
System.out.println("Added a linked Sheets chart with ID "+presentationChartId);
}catch(GoogleJsonResponseExceptione){
// TODO(developer) - handle error appropriately
GoogleJsonErrorerror=e.getDetails();
if(error.getCode()==404){
System.out.printf("Presentation not found with id '%s'.\n",presentationId);
}else{
throwe;
}
}
returnresponse;
}
}

JavaScript

slides/snippets/slides_create_sheets_chart.js
functioncreateSheetsChart(presentationId,pageId,shapeId,sheetChartId,callback){
// 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.
constemu4M={
magnitude:4000000,
unit:'EMU',
};
constpresentationChartId='MyEmbeddedChart';
constrequests=[{
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.
try{
gapi.client.slides.presentations.batchUpdate({
presentationId:presentationId,
requests:requests,
}).then((batchUpdateResponse)=>{
console.log(`Added a linked Sheets chart with ID: ${presentationChartId}`);
if(callback)callback(batchUpdateResponse.result);
});
}catch(err){
document.getElementById('content').innerText=err.message;
return;
}
}

Node.js

slides/snippets/slides_create_sheets_chart.js
import{GoogleAuth}from'google-auth-library';
import{google}from'googleapis';
/**
 * Embeds a Sheets chart into a presentation.
 * @param {string} presentationId The ID of the presentation.
 * @param {string} pageId The ID of the page to embed the chart on.
 * @param {string} spreadsheetId The ID of the spreadsheet containing the chart.
 * @param {string} sheetChartId The ID of the chart in the spreadsheet.
 * @return {Promise<object>} The response from the batch update.
 */
asyncfunctioncreateSheetsChart(
presentationId,
pageId,
spreadsheetId,
sheetChartId,
){
// Authenticate with Google and get an authorized client.
constauth=newGoogleAuth({
scopes:'https://www.googleapis.com/auth/presentations',
});
// Create a new Slides API client.
constservice=google.slides({version:'v1',auth});
// The size of the embedded chart, in English Metric Units (EMUs).
constemu4M={
magnitude:4000000,
unit:'EMU',
};
// The ID to use for the embedded chart.
constpresentationChartId='MyEmbeddedChart';
// The request to create a new chart.
constrequests=[
{
createSheetsChart:{
objectId:presentationChartId,
spreadsheetId,
chartId:sheetChartId,
// Linking mode allows the chart to be updated if the source sheet changes.
linkingMode:'LINKED',
elementProperties:{
pageObjectId:pageId,
size:{
height:emu4M,
width:emu4M,
},
transform:{
scaleX:1,
scaleY:1,
translateX:100000,
translateY:100000,
unit:'EMU',
},
},
},
},
];
// Execute the batch update request to create the chart.
constbatchUpdateResponse=awaitservice.presentations.batchUpdate({
presentationId,
requestBody:{
requests,
},
});
console.log(`Added a linked Sheets chart with ID: ${presentationChartId}`);
returnbatchUpdateResponse.data;
}

PHP

slides/snippets/src/SlidesCreateSheetsChart.php
<?php
use Google\Client;
use Google\Service\Drive;
use Google\Service\Slides;
use Google\Service\Slides\Request;
function createSheetsChart($presentationId, $pageId, $spreadsheetId, $sheetChartId)
{
 /* Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity for
 guides on implementing OAuth2 for your application. */
 $client = new Google\Client();
 $client->useApplicationDefaultCredentials();
 $client->addScope(Google\Service\Drive::DRIVE);
 $slidesService = new Google_Service_Slides($client);
 // 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.
 try {
 //creating new presentaion chart
 $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());
 return $response;
 } catch (Exception $e) {
 echo 'Message: ' . $e->getMessage();
 }
}

Python

slides/snippets/slides_create_sheets_chart.py
importgoogle.auth
fromgoogleapiclient.discoveryimport build
fromgoogleapiclient.errorsimport HttpError
defcreate_sheets_chart(
 presentation_id, page_id, spreadsheet_id, sheet_chart_id
):
"""
 create_sheets_chart the user has access to.
 Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity
 for guides on implementing OAuth2 for the application.
 """
 creds, _ = google.auth.default()
 # pylint: disable=maybe-no-member
 try:
 slides_service = build("slides", "v1", credentials=creds)
 # 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(f"Added a linked Sheets chart with ID: {presentation_chart_id}")
 return response
 except HttpError as error:
 print(f"An error occurred: {error}")
 return error
if __name__ == "__main__":
 # Put the presentation_id, Page_id of slides
 # spreadsheet_id and sheet_chart_id to be submitted.
 create_sheets_chart(
 "10QnVUx1X2qHsL17WUidGpPh_SQhXYx40CgIxaKk8jU4",
 "FIRSTSLIDE",
 "17eqFZl_WK4WVixX8PjvjfLD77DraoFwMDXeiHB3dvuM",
 "1107320627",
 )

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
/**
 * Refresh the sheets charts
 * @param {string} presentationId
 * @param {string} presentationChartId
 * @returns {*}
 */
functionrefreshSheetsChart(presentationId,presentationChartId){
constrequests=[
{
refreshSheetsChart:{
objectId:presentationChartId,
},
},
];
// Execute the request.
try{
constbatchUpdateResponse=Slides.Presentations.batchUpdate(
{
requests:requests,
},
presentationId,
);
console.log(
"Refreshed a linked Sheets chart with ID: %s",
presentationChartId,
);
returnbatchUpdateResponse;
}catch(err){
// TODO (Developer) - Handle exception
console.log("Failed with error: %s",err.error);
}
}

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/RefreshSheetsChart.java
importcom.google.api.client.googleapis.json.GoogleJsonError;
importcom.google.api.client.googleapis.json.GoogleJsonResponseException;
importcom.google.api.client.http.HttpRequestInitializer;
importcom.google.api.client.http.javanet.NetHttpTransport;
importcom.google.api.client.json.gson.GsonFactory;
importcom.google.api.services.slides.v1.Slides;
importcom.google.api.services.slides.v1.SlidesScopes;
importcom.google.api.services.slides.v1.model.BatchUpdatePresentationRequest;
importcom.google.api.services.slides.v1.model.BatchUpdatePresentationResponse;
importcom.google.api.services.slides.v1.model.RefreshSheetsChartRequest;
importcom.google.api.services.slides.v1.model.Request;
importcom.google.auth.http.HttpCredentialsAdapter;
importcom.google.auth.oauth2.GoogleCredentials;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
/* Class to demonstrate the use of Slides Refresh Chart API */
publicclass RefreshSheetsChart{
/**
 * Refresh the sheets charts.
 *
 * @param presentationId - id of the presentation.
 * @param presentationChartId - id of the presentation chart.
 * @return presentation chart id
 * @throws IOException - if credentials file not found.
 */
publicstaticBatchUpdatePresentationResponserefreshSheetsChart(
StringpresentationId,StringpresentationChartId)throwsIOException{
/* Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity for
 guides on implementing OAuth2 for your application. */
GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(SlidesScopes.PRESENTATIONS));
HttpRequestInitializerrequestInitializer=newHttpCredentialsAdapter(
credentials);
// Create the slides API client
Slidesservice=newSlides.Builder(newNetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Slides samples")
.build();
List<Request>requests=newArrayList<>();
// Refresh an existing linked Sheets chart embedded a presentation.
requests.add(newRequest()
.setRefreshSheetsChart(newRefreshSheetsChartRequest()
.setObjectId(presentationChartId)));
BatchUpdatePresentationResponseresponse=null;
try{
// Execute the request.
BatchUpdatePresentationRequestbody=
newBatchUpdatePresentationRequest().setRequests(requests);
response=service.presentations().batchUpdate(presentationId,body).execute();
System.out.println("Refreshed a linked Sheets chart with ID "+presentationChartId);
}catch(GoogleJsonResponseExceptione){
// TODO(developer) - handle error appropriately
GoogleJsonErrorerror=e.getDetails();
if(error.getCode()==400){
System.out.printf("Presentation chart not found with id '%s'.\n",presentationChartId);
}elseif(error.getCode()==404){
System.out.printf("Presentation not found with id '%s'.\n",presentationId);
}else{
throwe;
}
}
returnresponse;
}
}

JavaScript

slides/snippets/slides_refresh_sheets_chart.js
functionrefreshSheetsChart(presentationId,presentationChartId,callback){
constrequests=[{
refreshSheetsChart:{
objectId:presentationChartId,
},
}];
// Execute the request.
try{
gapi.client.slides.presentations.batchUpdate({
presentationId:presentationId,
requests:requests,
}).then((batchUpdateResponse)=>{
console.log(`Refreshed a linked Sheets chart with ID: ${presentationChartId}`);
if(callback)callback(batchUpdateResponse.result);
});
}catch(err){
document.getElementById('content').innerText=err.message;
return;
}
}

Node.js

slides/snippets/slides_refresh_sheets_chart.js
import{GoogleAuth}from'google-auth-library';
import{google}from'googleapis';
/**
 * Refreshes an embedded Sheets chart in a presentation.
 * @param {string} presentationId The ID of the presentation.
 * @param {string} presentationChartId The ID of the chart to refresh.
 * @return {Promise<object>} The response from the batch update.
 */
asyncfunctionrefreshSheetsChart(presentationId,presentationChartId){
// Authenticate with Google and get an authorized client.
constauth=newGoogleAuth({
scopes:'https://www.googleapis.com/auth/presentations',
});
// Create a new Slides API client.
constservice=google.slides({version:'v1',auth});
// The request to refresh the chart.
constrequests=[
{
refreshSheetsChart:{
objectId:presentationChartId,
},
},
];
// Execute the batch update request to refresh the chart.
constbatchUpdateResponse=awaitservice.presentations.batchUpdate({
presentationId,
requestBody:{
requests,
},
});
console.log(
`Refreshed a linked Sheets chart with ID: ${presentationChartId}`,
);
returnbatchUpdateResponse.data;
}

PHP

slides/snippets/src/SlidesRefreshSheetsChart.php
<?php
use Google\Client;
use Google\Service\Drive;
use Google\Service\Slides;
use Google\Service\Slides\BatchUpdatePresentationRequest;
function refreshSheetsChart($presentationId, $presentationChartId)
{
 /* Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity for
 guides on implementing OAuth2 for your application. */
 $client = new Google\Client();
 $client->useApplicationDefaultCredentials();
 $client->addScope(Google\Service\Drive::DRIVE);
 $slidesService = new Google_Service_Slides($client);
 try {
 $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());
 return $response;
 } catch (Exception $e) {
 echo 'Message: ' . $e->getMessage();
 }
}

Python

slides/snippets/slides_refresh_sheets_chart.py
importgoogle.auth
fromgoogleapiclient.discoveryimport build
fromgoogleapiclient.errorsimport HttpError
defrefresh_sheets_chart(presentation_id, presentation_chart_id):
"""
 refresh_sheets_chart the user has access to.
 Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity
 for guides on implementing OAuth2 for the application.
 """
 creds, _ = google.auth.default()
 # pylint: disable=maybe-no-member
 try:
 slides_service = build("slides", "v1", credentials=creds)
 # 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(f"Refreshed a linked Sheets chart with ID:{presentation_chart_id}")
 return response
 except HttpError as error:
 print(f"An error occurred: {error}")
 return error
if __name__ == "__main__":
 # Put the presentation_id, presentation_chart_id
 # to be submitted.
 refresh_sheets_chart(
 "10QnVUx1X2qHsL17WUidGpPh_SQhXYx40CgIxaKk8jU4", "1107320627"
 )

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}"

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月03日 UTC.