Basic writing (original) (raw)
The Google Slides API lets you add and modify elements on presentation pages. The examples on this page show how to perform common read operations using thepresentations.batchUpdatemethod.
These examples use the following variables:
- PRESENTATION_ID—Indicates where you provide thepresentation ID. You can discover the value for this ID from the presentation URL.
- PAGE_ID—Indicates where you provide the page object ID. You can retrieve the value for this from the URL or by using an API read request.
- PAGE_ELEMENT_ID—Indicates where you provide the page element object ID. You can specify this ID for elements you create (with some restrictions) or allow the Slides API to automatically create one. Element IDs can be retrieved through an API read request.
These examples are presented as HTTP requests to be language neutral. To learn how to implement a batch update in different languages using the Google API client libraries, see Add shapes and text.
Add a text box to a slide
The followingpresentations.batchUpdatecode sample shows how to use theCreateShapeRequestmethod to add a new text box (containing the string "My Added Text Box") to a slide specified by the PAGE_ID. Two requests are specified in the request body—one to create the text box shape (with a given size and location) and a second to insert text into it.
The first request specifies the object ID to use for the text box. This lets the second request use it in the same API call, reducing overhead.
The following is the request protocol to add a text box to a slide:
POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{ "requests": [ { "createShape": { "objectId": PAGE_ELEMENT_ID, "shapeType": "TEXT_BOX", "elementProperties": { "pageObjectId": PAGE_ID, "size": { "width": { "magnitude": 150, "unit": "PT" }, "height": { "magnitude": 50, "unit": "PT" } }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 200, "translateY": 100, "unit": "PT" } } } }, { "insertText": { "objectId": PAGE_ELEMENT_ID, "text": "My Added Text Box", "insertionIndex": 0 } } ] }
Add an image to a slide
The followingpresentations.batchUpdatecode sample shows how to use theCreateImageRequestmethod to add an image to a slide specified by the PAGE_ID. The API retrieves the image using the IMAGE_URL. This request also scales and positions the image in the slide.
The following is the request protocol to add an image to a slide:
POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{ "requests": [ { "createImage": { "url": IMAGE_URL, "elementProperties": { "pageObjectId": PAGE_ID, "size": { "width": { "magnitude": 30, "unit": "PT" }, "height": { "magnitude": 30, "unit": "PT" } }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 200, "translateY": 100, "unit": "PT" } } } } ] }
Delete a page or page element
The followingpresentations.batchUpdatecode sample shows how to use theDeleteObjectRequestmethod to delete the page element specified by PAGE_ELEMENT_IDand the slide specified by PAGE_ID using two separate requests.
The following is the request protocol to delete a page or page element:
POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{ "requests": [ { "deleteObject": { "objectId": PAGE_ELEMENT_ID }, "deleteObject": { "objectId": PAGE_ID } } ] }
Edit text in a specified shape
The followingpresentations.batchUpdatecode sample shows how to use theDeleteTextRequestmethod to replace a portion of the text present in the shape specified byPAGE_ELEMENT_ID. To accomplish this, first delete text using the zero-based startIndex
and then insert new text in that position. In this example, the original text string "My Shape Text: ????" is replaced with "My Shape Text: Trapezoid".
This request only affects text in a specified shape. To replace text everywhere within a presentation, use theReplaceAllTextRequestmethod.
The following is the request protocol to edit text in a specified shape:
POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{ "requests": [ { "deleteText": { "objectId": PAGE_ELEMENT_ID, "textRange": { "type": "FROM_START_INDEX", "startIndex": 15 } } }, { "insertText": { "objectId": PAGE_ELEMENT_ID, "text": "Trapezoid", "insertionIndex": 15 } } ] }
Replace a shape tag with an image
Tags are text boxes or shapes with a unique string name, such as "account-holder-name".
The followingpresentations.batchUpdatecode sample shows how to use theCreateImageRequestmethod to replace a single instance of a shape tag with an image, maintaining the same position and scaling it to fit the tag's size while keeping the image's aspect ratio.
The request can also be used to replace one image with another. The request consists of adding the new image and then deleting the tag.
The CreateImageRequest
method only replaces a specified shape. To replace tag shapes everywhere within a presentation, use aReplaceAllShapesWithImageRequestmethod.
The shape tag has the followingPageElementproperties (which can be found using apresentations.pages.getrequest):
{ "objectId": PAGE_ELEMENT_ID, "size": { "width": { "magnitude": 3000000, "unit": "EMU" }, "height": { "magnitude": 3000000, "unit": "EMU" } }, "transform": { "scaleX": 1.13, "scaleY": 0.62, "translateX": 4800000, "translateY": 450000, "unit": "EMU" }, "shape": { "shapeType": "RECTANGLE" } }
The shape resides on the slide specified by PAGE_ID. To specify the image that replaces the shape, the API retrieves the image using theIMAGE_URL. To preserve the image aspect ratio while limiting it to the size of the tag, theCreateImageRequestmethod sets both the image size to the product of the tag size and scale, and the image scale factors to 1
. For more information, see Preserve aspect ratio.
The following is the request protocol to replace a shape tag with an image:
POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{ "requests": [ { "createImage": { "url": IMAGE_URL, "elementProperties": { "pageObjectId": PAGE_ID, "size": { "width": { "magnitude": 3000000 * 1.13, "unit": "EMU" }, "height": { "magnitude": 3000000 * 0.62, "unit": "EMU" } }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 4800000, "translateY": 450000, "unit": "PT" } } } }, { "deleteObject": { "objectId": PAGE_ELEMENT_ID } } ] }