跳到主要内容

GetElement

使用指定的位置返回段落元素。

语法

expression.GetElement(nPos);

expression - 表示 ApiParagraph 类的变量。

参数

名称必需/可选数据类型默认值描述
nPos必需number要获取其内容的元素必须位于的位置。

返回值

ParagraphContent

示例

通过索引访问文档中段落内的特定元素。

// How do I retrieve a single run from a paragraph by position in a document?

// Target one text run out of many to apply selective formatting in a document.

let doc = Api.GetDocument();
let paragraph = Api.CreateParagraph();
paragraph.RemoveAllElements();
let run = Api.CreateRun();
run.AddText("This is the text for the first text run. Do not forget a space at its end to separate from the second one. ");
paragraph.AddElement(run);
run = Api.CreateRun();
run.AddText("This is the text for the second run. We will set it bold afterwards. It also needs space at its end. ");
paragraph.AddElement(run);
run = Api.CreateRun();
run.AddText("This is the text for the third run. It ends the paragraph.");
paragraph.AddElement(run);
run = paragraph.GetElement(1);
run.SetBold(true);
doc.Push(paragraph);