跳到主要内容

SearchAndReplace

在演示文稿中查找并替换文本。

语法​

expression.SearchAndReplace(properties);

expression - 表示 ApiPresentation 类的变量。

参数​

名称必需/可选数据类型默认值描述
properties必需Object查找和替换的属性。
properties.searchString必需string搜索字符串。
properties.replaceString必需string替换字符串。
properties.matchCase可选booleanfalse是否区分大小写。
properties.wholeWords可选booleanfalse是否仅搜索全字匹配。

返回值​

boolean

示例​

在整个演示文稿中替换某个单词,同时保留源形状的未修改副本以供比较。

// The copy is duplicated before the replacement and added to the slide afterwards, so it keeps the original text.

// Add a shape, copy it aside, replace "first" with "final", then place the untouched copy above the replaced result.

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

const shape = Api.CreateShape("rect", 8000000, 900000);
shape.SetPosition(600000, 3200000);
slide.AddObject(shape);
shape.GetTextRange().SetText("The first draft. The first review. The first release.");

const original = shape.Copy();

presentation.SearchAndReplace({"searchString": "first", "replaceString": "final"});

original.SetPosition(600000, 1000000);
slide.AddObject(original);