ExcelScript.Shape interface
Represents a generic shape object in the worksheet. A shape could be a geometric shape, a line, a group of shapes, etc.
Remarks
Examples
/**
* This script creates a hexagon shape on the current worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
const currentSheet = workbook.getActiveWorksheet();
const hexagon: ExcelScript.Shape =
currentSheet.addGeometricShape(ExcelScript.GeometricShapeType.hexagon);
// Set the hexagon size to 40x40 pixels.
hexagon.setHeight(40);
hexagon.setWidth(40);
// Position the hexagon at [100,100] pixels.
hexagon.setLeft(100);
hexagon.setTop(100);
}
Methods
copy |
Copies and pastes a |
delete() | Removes the shape from the worksheet. |
get |
Specifies the alternative description text for a |
get |
Specifies the alternative title text for a |
get |
Converts the shape to an image and returns the image as a base64-encoded string. The DPI is 96. The only supported formats are |
get |
Returns the number of connection sites on this shape. |
get |
Returns the fill formatting of this shape. |
get |
Returns the geometric shape associated with the shape. An error will be thrown if the shape type is not "GeometricShape". |
get |
Specifies the geometric shape type of this geometric shape. See |
get |
Returns the shape group associated with the shape. An error will be thrown if the shape type is not "GroupShape". |
get |
Specifies the height, in points, of the shape. Throws an |
get |
Specifies the shape identifier. |
get |
Returns the image associated with the shape. An error will be thrown if the shape type is not "Image". |
get |
Converts the shape to an image and returns the image as a base64-encoded string. The DPI is 96. The only supported formats are |
get |
The distance, in points, from the left side of the shape to the left side of the worksheet. Throws an |
get |
Specifies the level of the specified shape. For example, a level of 0 means that the shape is not part of any groups, a level of 1 means the shape is part of a top-level group, and a level of 2 means the shape is part of a sub-group of the top level. |
get |
Returns the line associated with the shape. An error will be thrown if the shape type is not "Line". |
get |
Returns the line formatting of this shape. |
get |
Specifies if the aspect ratio of this shape is locked. |
get |
Specifies the name of the shape. |
get |
Specifies the parent group of this shape. |
get |
Represents how the object is attached to the cells below it. |
get |
Specifies the rotation, in degrees, of the shape. |
get |
Returns the text frame object of this shape. |
get |
The distance, in points, from the top edge of the shape to the top edge of the worksheet. Throws an |
get |
Returns the type of this shape. See |
get |
Specifies if the shape is visible. |
get |
Specifies the width, in points, of the shape. Throws an |
get |
Returns the position of the specified shape in the z-order, with 0 representing the bottom of the order stack. |
increment |
Moves the shape horizontally by the specified number of points. |
increment |
Rotates the shape clockwise around the z-axis by the specified number of degrees. Use the |
increment |
Moves the shape vertically by the specified number of points. |
scale |
Scales the height of the shape by a specified factor. For images, you can indicate whether you want to scale the shape relative to the original or the current size. Shapes other than pictures are always scaled relative to their current height. |
scale |
Scales the width of the shape by a specified factor. For images, you can indicate whether you want to scale the shape relative to the original or the current size. Shapes other than pictures are always scaled relative to their current width. |
set |
Specifies the alternative description text for a |
set |
Specifies the alternative title text for a |
set |
Specifies the geometric shape type of this geometric shape. See |
set |
Specifies the height, in points, of the shape. Throws an |
set |
The distance, in points, from the left side of the shape to the left side of the worksheet. Throws an |
set |
Specifies if the aspect ratio of this shape is locked. |
set |
Specifies the name of the shape. |
set |
Represents how the object is attached to the cells below it. |
set |
Specifies the rotation, in degrees, of the shape. |
set |
The distance, in points, from the top edge of the shape to the top edge of the worksheet. Throws an |
set |
Specifies if the shape is visible. |
set |
Specifies the width, in points, of the shape. Throws an |
set |
Moves the specified shape up or down the collection's z-order, which shifts it in front of or behind other shapes. |
Method Details
copyTo(destinationSheet)
Copies and pastes a Shape
object. The pasted shape is copied to the same pixel location as this shape.
copyTo(destinationSheet?: Worksheet | string): Shape;
Parameters
- destinationSheet
-
ExcelScript.Worksheet | string
The sheet to which the shape object will be pasted. The default value is the copied shape's worksheet.
Returns
delete()
Removes the shape from the worksheet.
delete(): void;
Returns
void
Examples
/**
* This script deletes all the shapes on the current worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the collection of shapes on the currently selected worksheet.
const shapes = workbook.getActiveWorksheet().getShapes();
// Remove each shape.
shapes.forEach(shape =>{
shape.delete();
});
}
getAltTextDescription()
Specifies the alternative description text for a Shape
object.
getAltTextDescription(): string;
Returns
string
getAltTextTitle()
Specifies the alternative title text for a Shape
object.
getAltTextTitle(): string;
Returns
string
getAsImage(format)
Warning
This API is now deprecated.
Use getImageAsBase64
instead.
Converts the shape to an image and returns the image as a base64-encoded string. The DPI is 96. The only supported formats are ExcelScript.PictureFormat.BMP
, ExcelScript.PictureFormat.PNG
, ExcelScript.PictureFormat.JPEG
, and ExcelScript.PictureFormat.GIF
.
getAsImage(format: PictureFormat): string;
Parameters
- format
- ExcelScript.PictureFormat
Specifies the format of the image.
Returns
string
Examples
/**
* This script creates a star shape with the value from cell A1.
* It then returns the image as a base64-encoded string.
* This string would be used as part of a Power Automate flow to add the image elsewhere.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the value of A1 from the worksheet named Sheet1.
const sheet = workbook.getWorksheet("Sheet1");
const value = sheet.getRange("A1").getValue();
// Create a Shape object that looks like a 5-pointed star.
const star = sheet.addGeometricShape(ExcelScript.GeometricShapeType.star5);
// Set the text of star and make sure the shape fits the text.
const textFrame = star.getTextFrame();
textFrame.getTextRange().setText(value.toString());
textFrame.setAutoSizeSetting(ExcelScript.ShapeAutoSize.autoSizeShapeToFitText);
// Return the shape as a PNG image represented by a base64-encoded string.
return star.getAsImage(ExcelScript.PictureFormat.png);
}
getConnectionSiteCount()
Returns the number of connection sites on this shape.
getConnectionSiteCount(): number;
Returns
number
getFill()
getGeometricShape()
Returns the geometric shape associated with the shape. An error will be thrown if the shape type is not "GeometricShape".
getGeometricShape(): GeometricShape;
Returns
getGeometricShapeType()
Specifies the geometric shape type of this geometric shape. See ExcelScript.GeometricShapeType
for details. Returns null
if the shape type is not "GeometricShape".
getGeometricShapeType(): GeometricShapeType;
Returns
getGroup()
Returns the shape group associated with the shape. An error will be thrown if the shape type is not "GroupShape".
getGroup(): ShapeGroup;
Returns
getHeight()
Specifies the height, in points, of the shape. Throws an InvalidArgument
exception when set with a negative value or zero as an input.
getHeight(): number;
Returns
number
getId()
Specifies the shape identifier.
getId(): string;
Returns
string
getImage()
Returns the image associated with the shape. An error will be thrown if the shape type is not "Image".
getImage(): Image;
Returns
Examples
/**
* This script transfers an image from one worksheet to another.
*/
function main(workbook: ExcelScript.Workbook)
{
// Get the worksheet with the image on it.
let firstWorksheet = workbook.getWorksheet("FirstSheet");
// Get the first image from the worksheet.
// If a script added the image, you could add a name to make it easier to find.
let image: ExcelScript.Image;
firstWorksheet.getShapes().forEach((shape, index) => {
if (shape.getType() === ExcelScript.ShapeType.image) {
image = shape.getImage();
return;
}
});
// Copy the image to another worksheet.
image.getShape().copyTo("SecondSheet");
}
getImageAsBase64(format)
Converts the shape to an image and returns the image as a base64-encoded string. The DPI is 96. The only supported formats are ExcelScript.PictureFormat.BMP
, ExcelScript.PictureFormat.PNG
, ExcelScript.PictureFormat.JPEG
, and ExcelScript.PictureFormat.GIF
.
getImageAsBase64(format: PictureFormat): string;
Parameters
- format
- ExcelScript.PictureFormat
Specifies the format of the image.
Returns
string
getLeft()
The distance, in points, from the left side of the shape to the left side of the worksheet. Throws an InvalidArgument
exception when set with a negative value as an input.
getLeft(): number;
Returns
number
getLevel()
Specifies the level of the specified shape. For example, a level of 0 means that the shape is not part of any groups, a level of 1 means the shape is part of a top-level group, and a level of 2 means the shape is part of a sub-group of the top level.
getLevel(): number;
Returns
number
getLine()
Returns the line associated with the shape. An error will be thrown if the shape type is not "Line".
getLine(): Line;
Returns
Examples
/**
* This script adds a line that goes from cell B2 to cell F4 on the current worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
const sheet = workbook.getActiveWorksheet();
// Get the ranges for the two cells.
const b2Range = sheet.getRange("B2");
const f4Range = sheet.getRange("F4");
// Add a straight line that connects the top-left corners of both cells.
const newShape = sheet.addLine(
b2Range.getLeft(),
b2Range.getTop(),
f4Range.getLeft(),
f4Range.getTop(),
ExcelScript.ConnectorType.straight);
// Add an open arrowhead to the end of the line, such that it points at F4.
const line = newShape.getLine();
line.setEndArrowheadStyle(ExcelScript.ArrowheadStyle.open);
}
getLineFormat()
Returns the line formatting of this shape.
getLineFormat(): ShapeLineFormat;
Returns
getLockAspectRatio()
Specifies if the aspect ratio of this shape is locked.
getLockAspectRatio(): boolean;
Returns
boolean
getName()
Specifies the name of the shape.
getName(): string;
Returns
string
getParentGroup()
getPlacement()
Represents how the object is attached to the cells below it.
getPlacement(): Placement;
Returns
getRotation()
Specifies the rotation, in degrees, of the shape.
getRotation(): number;
Returns
number
getTextFrame()
Returns the text frame object of this shape.
getTextFrame(): TextFrame;
Returns
Examples
/**
* This script creates a star shape with the value from cell A1.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the value of A1 from the worksheet named Sheet1.
const sheet = workbook.getWorksheet("Sheet1");
const value = sheet.getRange("A1").getValue();
// Create a Shape object that looks like a 5-pointed star.
const star = sheet.addGeometricShape(ExcelScript.GeometricShapeType.star5);
// Set the text of star and make sure the shape fits the text.
const textFrame = star.getTextFrame();
textFrame.getTextRange().setText(value.toString());
textFrame.setAutoSizeSetting(ExcelScript.ShapeAutoSize.autoSizeShapeToFitText);
}
getTop()
The distance, in points, from the top edge of the shape to the top edge of the worksheet. Throws an InvalidArgument
exception when set with a negative value as an input.
getTop(): number;
Returns
number
getType()
Returns the type of this shape. See ExcelScript.ShapeType
for details.
getType(): ShapeType;
Returns
getVisible()
Specifies if the shape is visible.
getVisible(): boolean;
Returns
boolean
getWidth()
Specifies the width, in points, of the shape. Throws an InvalidArgument
exception when set with a negative value or zero as an input.
getWidth(): number;
Returns
number
getZOrderPosition()
Returns the position of the specified shape in the z-order, with 0 representing the bottom of the order stack.
getZOrderPosition(): number;
Returns
number
incrementLeft(increment)
Moves the shape horizontally by the specified number of points.
incrementLeft(increment: number): void;
Parameters
- increment
-
number
The increment, in points, the shape will be horizontally moved. A positive value moves the shape to the right and a negative value moves it to the left. If the sheet is right-to-left oriented, this is reversed: positive values will move the shape to the left and negative values will move it to the right.
Returns
void
incrementRotation(increment)
Rotates the shape clockwise around the z-axis by the specified number of degrees. Use the rotation
property to set the absolute rotation of the shape.
incrementRotation(increment: number): void;
Parameters
- increment
-
number
How many degrees the shape will be rotated. A positive value rotates the shape clockwise and a negative value rotates it counterclockwise.
Returns
void
incrementTop(increment)
Moves the shape vertically by the specified number of points.
incrementTop(increment: number): void;
Parameters
- increment
-
number
The increment, in points, the shape will be vertically moved. A positive value moves the shape down and a negative value moves it up.
Returns
void
scaleHeight(scaleFactor, scaleType, scaleFrom)
Scales the height of the shape by a specified factor. For images, you can indicate whether you want to scale the shape relative to the original or the current size. Shapes other than pictures are always scaled relative to their current height.
scaleHeight(
scaleFactor: number,
scaleType: ShapeScaleType,
scaleFrom?: ShapeScaleFrom
): void;
Parameters
- scaleFactor
-
number
Specifies the ratio between the height of the shape after you resize it and the current or original height.
- scaleType
- ExcelScript.ShapeScaleType
Specifies whether the shape is scaled relative to its original or current size. The original size scaling option only works for images.
- scaleFrom
- ExcelScript.ShapeScaleFrom
Optional. Specifies which part of the shape retains its position when the shape is scaled. If omitted, it represents the shape's upper left corner retains its position.
Returns
void
scaleWidth(scaleFactor, scaleType, scaleFrom)
Scales the width of the shape by a specified factor. For images, you can indicate whether you want to scale the shape relative to the original or the current size. Shapes other than pictures are always scaled relative to their current width.
scaleWidth(
scaleFactor: number,
scaleType: ShapeScaleType,
scaleFrom?: ShapeScaleFrom
): void;
Parameters
- scaleFactor
-
number
Specifies the ratio between the width of the shape after you resize it and the current or original width.
- scaleType
- ExcelScript.ShapeScaleType
Specifies whether the shape is scaled relative to its original or current size. The original size scaling option only works for images.
- scaleFrom
- ExcelScript.ShapeScaleFrom
Optional. Specifies which part of the shape retains its position when the shape is scaled. If omitted, it represents the shape's upper left corner retains its position.
Returns
void
setAltTextDescription(altTextDescription)
Specifies the alternative description text for a Shape
object.
setAltTextDescription(altTextDescription: string): void;
Parameters
- altTextDescription
-
string
Returns
void
setAltTextTitle(altTextTitle)
Specifies the alternative title text for a Shape
object.
setAltTextTitle(altTextTitle: string): void;
Parameters
- altTextTitle
-
string
Returns
void
setGeometricShapeType(geometricShapeType)
Specifies the geometric shape type of this geometric shape. See ExcelScript.GeometricShapeType
for details. Returns null
if the shape type is not "GeometricShape".
setGeometricShapeType(geometricShapeType: GeometricShapeType): void;
Parameters
- geometricShapeType
- ExcelScript.GeometricShapeType
Returns
void
setHeight(height)
Specifies the height, in points, of the shape. Throws an InvalidArgument
exception when set with a negative value or zero as an input.
setHeight(height: number): void;
Parameters
- height
-
number
Returns
void
setLeft(left)
The distance, in points, from the left side of the shape to the left side of the worksheet. Throws an InvalidArgument
exception when set with a negative value as an input.
setLeft(left: number): void;
Parameters
- left
-
number
Returns
void
setLockAspectRatio(lockAspectRatio)
Specifies if the aspect ratio of this shape is locked.
setLockAspectRatio(lockAspectRatio: boolean): void;
Parameters
- lockAspectRatio
-
boolean
Returns
void
setName(name)
Specifies the name of the shape.
setName(name: string): void;
Parameters
- name
-
string
Returns
void
Examples
/**
* This script creates a triangle shape on the current worksheet and names it "TRI".
*/
function main(workbook: ExcelScript.Workbook) {
const currentSheet = workbook.getActiveWorksheet();
const triangle: ExcelScript.Shape =
currentSheet.addGeometricShape(ExcelScript.GeometricShapeType.triangle);
triangle.setName("TRI");
}
setPlacement(placement)
Represents how the object is attached to the cells below it.
setPlacement(placement: Placement): void;
Parameters
- placement
- ExcelScript.Placement
Returns
void
Examples
/**
* This script creates a diamond shape at cell C3.
* The shape moves and resizes as the grid underneath it changes.
*/
function main(workbook: ExcelScript.Workbook) {
// Get cell C3 in the current worksheet.
const sheet = workbook.getActiveWorksheet();
const cell = sheet.getRange("C3");
// Create a diamond that slightly overlaps the cell C3.
const diamond = sheet.addGeometricShape(ExcelScript.GeometricShapeType.diamond);
// Set each dimension so that the shape extends 5 pixels beyond the cell borders.
diamond.setLeft(cell.getLeft() - 5);
diamond.setTop(cell.getTop() - 5);
diamond.setHeight(cell.getHeight() + 10);
diamond.setWidth(cell.getWidth() + 10);
// Set the placement of the shape so that it resizes and moves with the grid.
diamond.setPlacement(ExcelScript.Placement.twoCell);
}
setRotation(rotation)
Specifies the rotation, in degrees, of the shape.
setRotation(rotation: number): void;
Parameters
- rotation
-
number
Returns
void
setTop(top)
The distance, in points, from the top edge of the shape to the top edge of the worksheet. Throws an InvalidArgument
exception when set with a negative value as an input.
setTop(top: number): void;
Parameters
- top
-
number
Returns
void
setVisible(visible)
Specifies if the shape is visible.
setVisible(visible: boolean): void;
Parameters
- visible
-
boolean
Returns
void
setWidth(width)
Specifies the width, in points, of the shape. Throws an InvalidArgument
exception when set with a negative value or zero as an input.
setWidth(width: number): void;
Parameters
- width
-
number
Returns
void
setZOrder(position)
Moves the specified shape up or down the collection's z-order, which shifts it in front of or behind other shapes.
setZOrder(position: ShapeZOrder): void;
Parameters
- position
- ExcelScript.ShapeZOrder
Where to move the shape in the z-order stack relative to the other shapes. See ExcelScript.ShapeZOrder
for details.
Returns
void
Office Scripts