Stay organized with collections
Save and categorize content based on your preferences.
This page describes how to add slides to an existing Google Slides
presentation.
Slides
Each individual page of a Slides presentation is a slide.
To add a slide to the presentation, you need the presentation ID. You can find
the presentation's ID by opening the presentation in Slides and
looking at its URL:
To add a slide, use the
batchUpdate() method
with the presentation ID and include a
CreateSlideRequest
in the request body. The ID of the new slide is returned in the response body.
Example
The following example shows how to add a slide to a presentation. All the fields
for
CreateSlideRequest
are optional. This example creates the slide as the second slide, with
TITLE_AND_TWO_COLUMNS layout, and an explicit object ID.
/** * Creates a slide * @param {string} presentationId * @param {string} pageId * @returns {*} */functioncreateSlide(presentationId,pageId){// See Presentation.insertSlide(...) to learn how to add a slide using SlidesApp.// http://developers.google.com/apps-script/reference/slides/presentation#appendslidelayoutconstrequests=[{createSlide:{objectId:pageId,insertionIndex:'1',slideLayoutReference:{predefinedLayout:'TITLE_AND_TWO_COLUMNS'}}}];// If you wish to populate the slide with elements, add element create requests here,// using the pageId.// Execute the request.try{constcreateSlideResponse=Slides.Presentations.batchUpdate({requests:requests},presentationId);console.log('CreatedslidewithID:%s',createSlideResponse.replies[0].createSlide.objectId);returncreateSlideResponse;}catch(err){// TODO (Developer) - Handle exceptionconsole.log('Failedwitherror:%s',err.error);}};
// Add a slide at index 1 using the predefined "TITLE_AND_TWO_COLUMNS" layout// and the ID "MyNewSlide_001".slideId:="MyNewSlide_001"requests:=[]*slides.Request{{
CreateSlide: &slides.CreateSlideRequest{
ObjectId: slideId,
InsertionIndex: 1,
SlideLayoutReference: &slides.LayoutReference{
PredefinedLayout: "TITLE_AND_TWO_COLUMNS",
},
},
}}// If you wish to populate the slide with elements, add create requests here,// using the slide ID specified above.// Execute the request.body:=&slides.BatchUpdatePresentationRequest{Requests:requests,}response,err:=slidesService.Presentations.BatchUpdate(presentationId,body).Do()iferr!=nil{log.Fatalf("Unable to create slide. %v",err)}fmt.Printf("Created slide with ID: %s",response.Replies[0].CreateSlide.ObjectId)
importcom.google.api.client.googleapis.json.GoogleJsonResponseException;importcom.google.api.client.googleapis.json.GoogleJsonError;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.CreateSlideRequest;importcom.google.api.services.slides.v1.model.CreateSlideResponse;importcom.google.api.services.slides.v1.model.LayoutReference;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 Create Slides API */publicclassCreateSlide{/** * Creates a new slide. * * @param presentationId - id of the presentation. * @return slide id * @throws IOException - if credentials file not found. */publicstaticBatchUpdatePresentationResponsecreateSlide(StringpresentationId)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 clientSlidesservice=newSlides.Builder(newNetHttpTransport(),GsonFactory.getDefaultInstance(),requestInitializer).setApplicationName("Slides samples").build();// Add a slide at index 1 using the predefined "TITLE_AND_TWO_COLUMNS" layoutList<Request>requests=newArrayList<>();StringslideId="MyNewSlide_001";BatchUpdatePresentationResponseresponse=null;try{requests.add(newRequest().setCreateSlide(newCreateSlideRequest().setObjectId(slideId).setInsertionIndex(1).setSlideLayoutReference(newLayoutReference().setPredefinedLayout("TITLE_AND_TWO_COLUMNS"))));// If you wish to populate the slide with elements, add create requests here,// using the slide ID specified above.// Execute the request.BatchUpdatePresentationRequestbody=newBatchUpdatePresentationRequest().setRequests(requests);response=service.presentations().batchUpdate(presentationId,body).execute();CreateSlideResponsecreateSlideResponse=response.getReplies().get(0).getCreateSlide();// Prints the slide id.System.out.println("Created slide with ID: "+createSlideResponse.getObjectId());}catch(GoogleJsonResponseExceptione){// TODO(developer) - handle error appropriatelyGoogleJsonErrorerror=e.getDetails();if(error.getCode()==400){System.out.printf(" Id '%s' should be unique among all pages and page elements.\n",presentationId);}elseif(error.getCode()==404){System.out.printf("Presentation not found with id '%s'.\n",presentationId);}else{throwe;}}returnresponse;}}
functioncreateSlide(presentationId,pageId,callback){constrequests=[{createSlide:{objectId:pageId,insertionIndex:'1',slideLayoutReference:{predefinedLayout:'TITLE_AND_TWO_COLUMNS',},},},];// If you wish to populate the slide with elements, add element create requests here,// using the pageId.// Execute the request.try{gapi.client.slides.presentations.batchUpdate({presentationId:presentationId,requests:requests,}).then((createSlideResponse)=>{constobjectId=createSlideResponse.result.replies[0].createSlide.objectId;console.log(`Created slide with ID: ${objectId}`);if(callback)callback(createSlideResponse);});}catch(err){document.getElementById('content').innerText=err.message;return;}}
/** * Creates a new slide in a presentation. * @param {string} presentationId The presentation ID. * @param {string} pageId The object ID for the new slide. */asyncfunctioncreateSlide(presentationId,pageId){const{GoogleAuth}=require('google-auth-library');const{google}=require('googleapis');constauth=newGoogleAuth({scopes:'https://www.googleapis.com/auth/presentations',});constservice=google.slides({version:'v1',auth});constrequests=[{createSlide:{objectId:pageId,insertionIndex:'1',slideLayoutReference:{predefinedLayout:'TITLE_AND_TWO_COLUMNS',},},},];// If you wish to populate the slide with elements, add element create requests here,// using the pageId.// Execute the request.try{constres=awaitservice.presentations.batchUpdate({presentationId,resource:{requests,},});console.log(`Created slide with ID: ${res.data.replies[0].createSlide.objectId}`,);returnres;}catch(err){// TODO (developer) - handle exceptionthrowerr;}}
importgoogle.authfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.errorsimportHttpErrordefcreate_slide(presentation_id,page_id):""" Creates the Presentation 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.\n """creds,_=google.auth.default()# pylint: disable=maybe-no-membertry:service=build("slides","v1",credentials=creds)# Add a slide at index 1 using the predefined# 'TITLE_AND_TWO_COLUMNS' layout and the ID page_id.requests=[{"createSlide":{"objectId":page_id,"insertionIndex":"1","slideLayoutReference":{"predefinedLayout":"TITLE_AND_TWO_COLUMNS"},}}]# If you wish to populate the slide with elements,# add element create requests here, using the page_id.# Execute the request.body={"requests":requests}response=(service.presentations().batchUpdate(presentationId=presentation_id,body=body).execute())create_slide_response=response.get("replies")[0].get("createSlide")print(f"Created slide with ID:{(create_slide_response.get('objectId'))}")exceptHttpErroraserror:print(f"An error occurred: {error}")print("Slides not created")returnerrorreturnresponseif__name__=="__main__":# Put the presentation_id, Page_id of slides whose list needs# to be submitted.create_slide("12SQU9Ik-ShXecJoMtT-LlNwEPiFR7AadnxV2KiBXCnE","My4ndpage")
body=Google::Apis::SlidesV1::Presentation.newrequests=[{create_slide:{object_id_prop:page_id,insertion_index:'1',slide_layout_reference:{predefined_layout:'TITLE_AND_TWO_COLUMNS'}}}]# If you wish to populate the slide with elements, add element create requests here,# using the page_id.# Execute the request.req=Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests:requests)response=slides_service.batch_update_presentation(presentation_id,req)create_slide_response=response.replies[0].create_slideputs"Created slide with ID: #{create_slide_response.object_id}"
After you create the slide, you can add text and shapes to it.
Fill in placeholders
Layouts normally have placeholder shapes that are copied to the slide when it's
created. CreateSlideRequest lets you specify the object IDs used for these
copied placeholders through the placeholderIdMappings field. This lets you
modify the copied placeholders in the same batchUpdate request, which improves
performance and saves quota. For more information, see the
CreateSlideRequest sample.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-19 UTC."],[[["This page provides instructions on adding new slides to existing Google Slides presentations."],["You need the presentation ID to add a slide, which can be found in the presentation's URL."],["The `batchUpdate()` method and `CreateSlideRequest` are used to add slides, with the new slide's ID returned in the response."],["Slides can be further customized by adding text, shapes, and filling in placeholders after creation."],["The `placeholderIdMappings` field in `CreateSlideRequest` allows for efficient modification of copied placeholders during the slide creation process."]]],[]]