Class Shape

Shape

A PageElement representing a generic shape that does not have a more specific classification. Includes text boxes, rectangles, and other predefined shapes.

Methods

MethodReturn typeBrief description
alignOnPage(alignmentPosition)ShapeAligns the element to the specified alignment position on the page.
duplicate()PageElementDuplicates the page element.
getBorder()BorderReturns the Border of the shape.
getContentAlignment()ContentAlignmentReturns the ContentAlignment of the text in the shape.
getDescription()StringGets the page element's description.
getFill()FillReturns the Fill of the shape.
getHeight()NumberGets the element's height in points, which is the height of the element's bounding box when the element has no rotation.
getInherentHeight()NumberGets the element's inherent height in points.
getInherentWidth()NumberGets the element's inherent width in points.
getLeft()NumberGets the element's horizontal position in points, measured from the upper left corner of the page when the element has no rotation.
getLink()LinkReturns the Link or null if there is no link.
getObjectId()StringGets the unique ID for this object.
getPageElementType()PageElementTypeGets the page element's type, represented as a PageElementType enum.
getParentGroup()GroupGets the group this page element is in, or null if the element is not in a group.
getParentPage()PageGets the page this page element is on.
getParentPlaceholder()PageElementReturns the parent page element of the placeholder.
getPlaceholderIndex()IntegerReturns the placeholder index of the shape.
getPlaceholderType()PlaceholderTypeReturns the placeholder type of the shape, or PlaceholderType.NONE if the shape is not a placeholder.
getRotation()NumberGets the element's clockwise rotation angle around its center in degrees, where 0 degrees means no rotation.
getShapeType()ShapeTypeReturns the type of the shape.
getText()TextRangeReturns the text content of the shape.
getTitle()StringGets the page element's title.
getTop()NumberGets the element's vertical position in points, measured from the upper left corner of the page when the element has no rotation.
getTransform()AffineTransformGets the page element's transform.
getWidth()NumberGets the element's width in points, which is the width of the element's bounding box when the element has no rotation.
preconcatenateTransform(transform)ShapePreconcatenates the provided transform to the existing transform of the page element.
remove()voidRemoves the page element.
removeLink()voidRemoves a Link.
replaceWithImage(blobSource)ImageReplaces this shape with an image provided by a BlobSource.
replaceWithImage(blobSource, crop)ImageReplaces this shape with an image provided by a BlobSource.
replaceWithImage(imageUrl)ImageReplaces this shape with an image.
replaceWithImage(imageUrl, crop)ImageReplaces this shape with an image.
replaceWithSheetsChart(sourceChart)SheetsChartReplaces this shape with an Google Sheets chart.
replaceWithSheetsChartAsImage(sourceChart)ImageReplaces this shape with an image of a Google Sheets chart.
scaleHeight(ratio)ShapeScales the element's height by the specified ratio.
scaleWidth(ratio)ShapeScales the element's width by the specified ratio.
select()voidSelects only the PageElement in the active presentation and removes any previous selection.
select(replace)voidSelects the PageElement in the active presentation.
setContentAlignment(contentAlignment)ShapeSets the ContentAlignment of the text in the shape.
setHeight(height)ShapeSets the element's height in points, which is the height of the element's bounding box when the element has no rotation.
setLeft(left)ShapeSets the element's horizontal position in points, measured from the upper left corner of the page when the element has no rotation.
setLinkSlide(slideIndex)LinkSets a Link to the given Slide using the zero-based index of the slide.
setLinkSlide(slide)LinkSets a Link to the given Slide, the link is set by the given slide ID.
setLinkSlide(slidePosition)LinkSets a Link to the given Slide using the relative position of the slide.
setLinkUrl(url)LinkSets a Link to the given non-empty URL string.
setRotation(angle)ShapeSets the element's clockwise rotation angle around its center in degrees.
setTop(top)ShapeSets the element's vertical position in points, measured from the upper left corner of the page when the element has no rotation.
setTransform(transform)ShapeSets the transform of the page element with the provided transform.
setWidth(width)ShapeSets the element's width in points, which is the width of the element's bounding box when the element has no rotation.

Detailed documentation

alignOnPage(alignmentPosition)

Aligns the element to the specified alignment position on the page.

Parameters

NameTypeDescription
alignmentPositionAlignmentPosition

Return

Shape


duplicate()

Duplicates the page element.

The duplicate page element is placed on the same page at the same position as the original.

Return

PageElement


getBorder()

Returns the Border of the shape.

Return

Border


getContentAlignment()

Returns the ContentAlignment of the text in the shape.

Return

ContentAlignment


getDescription()

Gets the page element's description. The description is combined with the title to display and read alt text.

Return

String — the page element's description.


getFill()

Returns the Fill of the shape.

Return

Fill


getHeight()

Gets the element's height in points, which is the height of the element's bounding box when the element has no rotation.

Return

Number — the page element's inherent height in points, or null if the page element does not have a height.


getInherentHeight()

Gets the element's inherent height in points.

The page element's transform is relative to its inherent size. Use the inherent size in conjunction with the element's transform to determine the element's final visual appearance.

Return

Number — the page element's inherent height in points, or null if the page element does not have a height.


getInherentWidth()

Gets the element's inherent width in points.

The page element's transform is relative to its inherent size. Use the inherent size in conjunction with the element's transform to determine the element's final visual appearance.

Return

Number — the page element's inherent width in points, or null if the page element does not have a width.


getLeft()

Gets the element's horizontal position in points, measured from the upper left corner of the page when the element has no rotation.

Return

Number


getLink()

Returns the Link or null if there is no link.

 var link = shape.getLink();
 if (link != null) {
   Logger.log('Shape has a link of type: ' + link.getLinkType());
 }
 

Return

Link


getObjectId()

Gets the unique ID for this object. Object IDs used by pages and page elements share the same namespace.

Return

String — the unique ID for this object.


getPageElementType()

Gets the page element's type, represented as a PageElementType enum.

Return

PageElementType — the page element's type.


getParentGroup()

Gets the group this page element is in, or null if the element is not in a group.

Return

Group


getParentPage()

Gets the page this page element is on.

Return

Page


getParentPlaceholder()

Returns the parent page element of the placeholder. Returns null if the shape is not a placeholder or has no parent.

Return

PageElement


getPlaceholderIndex()

Returns the placeholder index of the shape. If two or more instances of the same placeholder types are present in the same page, they would each have their own unique index value. Returns null if the shape is not a placeholder.

Return

Integer


getPlaceholderType()

Returns the placeholder type of the shape, or PlaceholderType.NONE if the shape is not a placeholder.

Return

PlaceholderType


getRotation()

Gets the element's clockwise rotation angle around its center in degrees, where 0 degrees means no rotation.

Return

Number — the rotation angle in degrees between 0 (inclusive) and 360 (exclusive)


getShapeType()

Returns the type of the shape.

Return

ShapeType


getText()

Returns the text content of the shape.

Text within a shape always terminates with a newline character.

Return

TextRange


getTitle()

Gets the page element's title. The title is combined with the description to display and read alt text.

Return

String — the page element's title.


getTop()

Gets the element's vertical position in points, measured from the upper left corner of the page when the element has no rotation.

Return

Number


getTransform()

Gets the page element's transform.

The visual appearance of the page element is determined by its absolute transform. To compute the absolute transform, preconcatenate a page element's transform with the transforms of all of its parent groups. If the page element is not in a group, its absolute transform is the same as the value in this field.

Return

AffineTransform — the page element's transform.


getWidth()

Gets the element's width in points, which is the width of the element's bounding box when the element has no rotation.

Return

Number — the page element's inherent width in points, or null if the page element does not have a width.


preconcatenateTransform(transform)

Preconcatenates the provided transform to the existing transform of the page element.

newTransform = argument * existingTransform

For example, to move a page elements 36 points to the left:


 element.preconcatenateTransform(SlidesApp.newAffineTransformBuilder()
     .setTranslateX(-36.0)
     .build());
 

You can also replace the page element's transform with setTransform(transform).

Parameters

NameTypeDescription
transformAffineTransform

Return

Shape


remove()

Removes the page element.

If after a remove operation, a Group contains only one or no page elements, the group itself is also removed.

If a placeholder Shape is removed on a master or layout, any empty inheriting shapes are also removed.


removeLink()

Removes a Link.

 shape.removeLink();
 

replaceWithImage(blobSource)

Replaces this shape with an image provided by a BlobSource.

The image is fetched from the provided BlobSource once at insertion time and a copy is stored for display inside the presentation. Images must be less than 50MB in size, cannot exceed 25 megapixels, and must be in PNG, JPEG, or GIF format.

In order to maintain the image's aspect ratio, the image is scaled and centered with respect to the size of the existing shape.

 var shape = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0];
 // Get the Drive image file with the given ID.
 var driveImage = DriveApp.getFileById(fileId);
 shape.replaceWithImage(driveImage);
 

Parameters

NameTypeDescription
blobSourceBlobSourceThe image data.

Return

Image — the Image that replaced the shape


replaceWithImage(blobSource, crop)

Replaces this shape with an image provided by a BlobSource.

Inserting the image fetches it from the BlobSource once and a copy is stored for display inside the presentation. Images must be less than 50MB in size, cannot exceed 25 megapixels, and must be in either in PNG, JPEG, or GIF format.

 var shape = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0];
 // Get the Drive image file with the given ID.
 var driveImage = DriveApp.getFileById(fileId);
 // Replace and crop the replaced image.
 shape.replaceWithImage(driveImage, true);
 

Parameters

NameTypeDescription
blobSourceBlobSourceThe image data.
cropBooleanIf true, crops the image to fit the existing shape's size. Otherwise, the image is scaled and centered.

Return

Image — the Image that replaced the shape


replaceWithImage(imageUrl)

Replaces this shape with an image.

Inserting the image fetches it from the URL once and a copy is stored for display inside the presentation. Images must be less than 50MB in size, cannot exceed 25 megapixels, and must be in either in PNG, JPEG, or GIF format.

The provided URL must be no larger than 2kB.

In order to maintain the image's aspect ratio, the image is scaled and centered with respect to the size of the existing shape.

Parameters

NameTypeDescription
imageUrlStringThe image URL to download the image from.

Return

Image — the Image that replaced the shape


replaceWithImage(imageUrl, crop)

Replaces this shape with an image.

Inserting the image fetches it from the URL once and a copy is stored for display inside the presentation. Images must be less than 50MB in size, cannot exceed 25 megapixels, and must be in either in PNG, JPEG, or GIF format.

The provided URL must be no larger than 2kB.

Parameters

NameTypeDescription
imageUrlStringThe image URL to download the image from.
cropBooleanIf true, crops the image to fit the existing shape's size. Otherwise, the image is scaled and centered.

Return

Image — the Image that replaced the shape


replaceWithSheetsChart(sourceChart)

Replaces this shape with an Google Sheets chart.

The chart is linked with the source Google Sheets chart which allows it to be updated. Other collaborators can see the link to the source spreadsheet.

 var sheet = SpreadsheetApp.openById('spreadsheetId').getSheets()[0];
 var chart = sheet.getCharts()[0];
 // Replace the shape with the Sheets chart.
 var shape = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0];
 shape.replaceWithSheetsChart(chart);
 

Parameters

NameTypeDescription
sourceChartEmbeddedChartThe chart in a spreadsheet that replaces the shape.

Return

SheetsChart — the chart that replaced the shape


replaceWithSheetsChartAsImage(sourceChart)

Replaces this shape with an image of a Google Sheets chart.

In order to maintain the Google Sheets chart's aspect ratio, the chart image is scaled and centered with respect to the size of the existing shape.

The image of the chart is not linked with the source Google Sheets chart.

 var sheet = SpreadsheetApp.openById('spreadsheetId').getSheets()[0];
 var chart = sheet.getCharts()[0];
 // Replace the shape with the Sheets chart as an image.
 var shape = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0];
 shape.replaceWithSheetsChartAsImage(chart);
 

Parameters

NameTypeDescription
sourceChartEmbeddedChartThe chart in a spreadsheet that replaces the shape.

Return

Image — the image of the chart that replaced the shape


scaleHeight(ratio)

Scales the element's height by the specified ratio. The element's height is the height of its bounding box when the element has no rotation.

Parameters

NameTypeDescription
ratioNumber

Return

Shape


scaleWidth(ratio)

Scales the element's width by the specified ratio. The element's width is the width of its bounding box when the element has no rotation.

Parameters

NameTypeDescription
ratioNumber

Return

Shape


select()

Selects only the PageElement in the active presentation and removes any previous selection. This is same as calling select(replace) with true.

A script can only access the selection of the user who is running the script, and only if the script is bound to the presentation.

This sets the parent Page of the PageElement as the current page selection.

 var slide = SlidesApp.getActivePresentation().getSlides()[0];
 var pageElement = slide.getPageElements()[0];
 // Only select this page element and replace any previous selection.
 pageElement.select();
 

select(replace)

Selects the PageElement in the active presentation.

A script can only access the selection of the user who is running the script, and only if the script is bound to the presentation.

The select(true) selects only the PageElement and removes any previous selection. This also sets the parent Page of the PageElement as the current page selection.

select(false) should be used to select multiple PageElement objects. The PageElement objects must be in the same Page.

The following conditions must be met while selecting a page element using select(false):
1) The parent Page of the PageElement object must be the current page selection
2) There should not be multiple Page objects selected.

To make sure that’s the case the preferred approach is to select the parent Page first using Page.selectAsCurrentPage() and then select the page elements in that page.

 var slide = SlidesApp.getActivePresentation().getSlides()[0];
 // First select the slide page, as the current page selection.
 slide.selectAsCurrentPage();
 // Then select all the page elements in the selected slide page.
 var pageElements = slide.getPageElements();
 for (var i = 0; i < pageElements.length; i++) {
   pageElements[i].select(false);
 }
 

Parameters

NameTypeDescription
replaceBooleanif true, the selection replaces any previous selection, otherwise the selection is added to any previous selection.

setContentAlignment(contentAlignment)

Sets the ContentAlignment of the text in the shape.

Parameters

NameTypeDescription
contentAlignmentContentAlignment

Return

Shape


setHeight(height)

Sets the element's height in points, which is the height of the element's bounding box when the element has no rotation.

Parameters

NameTypeDescription
heightNumber

Return

Shape


setLeft(left)

Sets the element's horizontal position in points, measured from the upper left corner of the page when the element has no rotation.

Parameters

NameTypeDescription
leftNumber

Return

Shape


setLinkSlide(slideIndex)

Sets a Link to the given Slide using the zero-based index of the slide.

 // Set a link to the first slide of the presentation.
 shape.setLinkSlide(0);
 

Parameters

NameTypeDescription
slideIndexIntegerThe zero-based index to the slide.

Return

Link — the Link that was set


setLinkSlide(slide)

Sets a Link to the given Slide, the link is set by the given slide ID.

 // Set a link to the first slide of the presentation.
 var slide = presentation.getSlides()[0];
 shape.setLinkSlide(slide);
 

Parameters

NameTypeDescription
slideSlideThe Slide to be linked.

Return

Link — the Link that was set


setLinkSlide(slidePosition)

Sets a Link to the given Slide using the relative position of the slide.

 // Set a link to the first slide of the presentation.
 shape.setLinkSlide(SlideApp.SlidePosition.FIRST_SLIDE);
 

Parameters

NameTypeDescription
slidePositionSlidePositionThe relative SlidePosition.

Return

Link — the Link that was set


setLinkUrl(url)

Sets a Link to the given non-empty URL string.

 // Set a link to the URL.
 shape.setLinkUrl("https://slides.google.com");
 

Parameters

NameTypeDescription
urlStringThe URL string.

Return

Link — the Link that was set


setRotation(angle)

Sets the element's clockwise rotation angle around its center in degrees.

Parameters

NameTypeDescription
angleNumber

Return

Shape


setTop(top)

Sets the element's vertical position in points, measured from the upper left corner of the page when the element has no rotation.

Parameters

NameTypeDescription
topNumber

Return

Shape


setTransform(transform)

Sets the transform of the page element with the provided transform.

Updating the transform of a group changes the absolute transform of the page elements in that group, which can change their visual appearance.

Updating the transform of a page element that is in a group only changes the transform of that page element; it doesn't affect the transforms of the group or other page elements in the group.

For details on how transforms impact visual appearance of page elements, see getTransform().

Parameters

NameTypeDescription
transformAffineTransform

Return

Shape


setWidth(width)

Sets the element's width in points, which is the width of the element's bounding box when the element has no rotation.

Parameters

NameTypeDescription
widthNumber

Return

Shape

Send feedback about...

Apps Script
Apps Script