跳到主要内容

Traverse

遍历创建访问者时所基于的对象,并为每个节点调用已指定的钩子函数。

备注

此功能仅在 ONLYOFFICE Docs 付费版本中可用。

语法​

expression.Traverse(options);

expression - 表示 ApiPresentationVisitor 类的变量。

参数​

名称必需/可选数据类型默认值描述
options可选Object | boolean遍历选项。传递 true 等同于 {selection: true}。
options.selection可选booleanfalse仅遍历所选绘图对象或所选文本,而不是整个对象。
options.notes可选booleanfalse同时遍历所访问幻灯片的演讲者备注。
options.masters可选booleanfalse同时遍历幻灯片母版(仅当访问器是从演示文稿创建时)。
options.layouts可选booleanfalse同时遍历幻灯片版式(仅当访问器是从演示文稿或母版创建时)。

返回值​

boolean

示例​

在演示文稿的所有位置(包括备注和母版)替换某个词并保留格式。

// How do I replace text on slides, speaker notes, layouts and masters without losing run formatting?

// Traverse with all options enabled and rewrite each run that contains the word being replaced.

const presentation = Api.GetPresentation();
const slide = presentation.GetSlideByIndex(0);
slide.RemoveAllObjects();

const fill = Api.CreateSolidFill(Api.RGB(51, 51, 51));
const stroke = Api.CreateStroke(0, Api.CreateNoFill());
const shape = Api.CreateShape("rect", 300 * 36000, 60 * 36000, fill, stroke);
shape.SetPosition(608400, 1267200);
shape.GetDocContent().GetElement(0).AddText("Draft agenda");
slide.AddObject(shape);
slide.AddNotesText("Draft speaker notes");

const visitor = presentation.GetVisitor();
visitor.Run = function (run) {
const text = run.GetText();
if (text.indexOf("Draft") === -1)
return true;
run.ClearContent();
run.AddText(text.split("Draft").join("Final"));
return true;
};
visitor.Traverse({notes: true, masters: true, layouts: true});