addShapeToSlide
This function adds a shape to the slide with optional text. By default, the shape is 139x42 mm, centered, with a blue fill and a dark border.
Prompts
- Add a rectangle with text "Important Point" on slide 2
- Add a star shape on the current slide
- Add a rounded rectangle with text "Key Message"
- Add a diamond shape with text "Decision Point"
- Add a right arrow with text "Next Step"
Function registration
(function () {
let func = new RegisteredFunction({
name: "addShapeToSlide",
description:
"Adds a shape to the slide with optional text (139x42mm, centered, blue fill with dark border)",
parameters: {
type: "object",
properties: {
slideNumber: {
type: "number",
description: "Slide number to add shape to",
minimum: 1,
},
shapeType: {
type: "string",
description:
"shape type - rect, roundRect, ellipse, triangle, diamond, pentagon, hexagon, star5, plus, mathMinus, mathMultiply, mathEqual, mathNotEqual, heart, cloud, leftArrow, rightArrow, upArrow, downArrow, leftRightArrow, chevron, bentArrow, curvedRightArrow, blockArc, wedgeRectCallout, cloudCallout, ribbon, wave, can, cube, pie, donut, sun, moon, smileyFace, lightningBolt, noSmoking",
},
text: {
type: "string",
description: "Text to add to the shape",
},
},
required: [],
},
examples: [
{
prompt: "add a rectangle with text on slide 2",
arguments: { slideNumber: 2, shapeType: "rect" },
},
{
prompt: "add a star shape on current slide",
arguments: { shapeType: "star5" },
},
{
prompt: "add a rounded rectangle with text",
arguments: { text: "Key Message" },
},
{
prompt: "add a diamond shape with text",
arguments: { shapeType: "diamond", text: "Decision Point" },
},
{
prompt: "add a right arrow with text",
arguments: { shapeType: "rightArrow", text: "Next Step" },
},
],
});
return func;
})();
Parameters
| Name | Type | Example | Description |
|---|---|---|---|
| slideNumber | number | 2 | The slide number where the shape will be added. If omitted, the shape is added to the current slide. |
| shapeType | string | "rect" | The shape type can be "rect", "roundRect", "ellipse", "triangle", "diamond", "pentagon", "hexagon", "star5", "plus", "mathMinus", "mathMultiply", "mathEqual", "mathNotEqual", "heart", "cloud", "leftArrow", "rightArrow", "upArrow", "downArrow", "leftRightArrow", "chevron", "bentArrow", "curvedRightArrow", "blockArc", "wedgeRectCallout", "cloudCallout", "ribbon", "wave", "can", "cube", "pie", "donut", "sun", "moon", "smileyFace", "lightningBolt", "noSmoking". The default value is "roundRect". |
| text | string | "Decision Point" | The text to add to the shape. |
Function execution
func.call = async function (params) {
Asc.scope.params = params;
await Asc.Editor.callCommand(function () {
let presentation = Api.GetPresentation();
let slide;
if (Asc.scope.params.slideNumber) {
slide = presentation.GetSlideByIndex(
Asc.scope.params.slideNumber - 1
);
} else {
slide = presentation.GetCurrentSlide();
}
if (!slide) return;
let slideWidth = presentation.GetWidth();
let slideHeight = presentation.GetHeight();
let shapeType = Asc.scope.params.shapeType || "rect";
let width = 2500000;
let height = 2500000;
let x = (slideWidth - width) / 2;
let y = (slideHeight - height) / 2;
let fill = Api.CreateSolidFill(
Api.CreateSchemeColor("accent1")
);
let stroke = Api.CreateStroke(
12700,
Api.CreateSolidFill(Api.CreateRGBColor(51, 51, 51))
);
let shape = Api.CreateShape(
shapeType,
width,
height,
fill,
stroke
);
shape.SetPosition(x, y);
if (Asc.scope.params.text) {
let docContent = shape.GetDocContent();
if (docContent) {
let paragraph = docContent.GetElement(0);
if (!paragraph) {
paragraph = Api.CreateParagraph();
docContent.Push(paragraph);
}
paragraph.SetJc("center");
paragraph.AddText(Asc.scope.params.text);
shape.SetVerticalTextAlign("center");
}
}
slide.AddObject(shape);
});
};
Methods used: GetPresentation, GetCurrentSlide, GetSlideByIndex, GetWidth, GetHeight, CreateSolidFill, CreateSchemeColor, CreateStroke, CreateRGBColor, CreateShape, SetPosition, GetDocContent, GetElement, CreateParagraph, Push, SetJc, AddText, SetVerticalTextAlign, AddObject, Asc.scope object