跳到主要内容

GetElementsCount

返回当前文档中的元素数量。

语法

expression.GetElementsCount();

expression - 表示 ApiDocumentContent 类的变量。

参数

此方法没有任何参数。

返回值

number

示例

此示例展示如何获取当前文档内容中的元素数量。

// How to get elements count and push it to the paragraph.

// Count the number of elements in the document.

let doc = Api.GetDocument();
let paragraph = doc.GetElement(0);
let fill = Api.CreateSolidFill(Api.RGB(255, 111, 61));
let stroke = Api.CreateStroke(0, Api.CreateNoFill());
let drawing = Api.CreateShape("rect", 3212465, 963295, fill, stroke);
paragraph.AddDrawing(drawing);
let docContent = drawing.GetDocContent();
docContent.RemoveAllElements();
paragraph = docContent.GetElement(0);
paragraph.AddText("We removed all elements from the shape and added a new paragraph inside it.");
paragraph = Api.CreateParagraph();
paragraph.AddText("Number of elements inside the shape: " + docContent.GetElementsCount());
doc.Push(paragraph);