跳到主要内容

Replace

替换此范围内所有出现的指定文本。

语法​

expression.Replace(findWhat, replaceWith, matchCase, wholeWords);

expression - 表示 ApiTextRange 类的变量。

参数​

名称必需/可选数据类型默认值描述
findWhat必需string要搜索的文本。
replaceWith必需string替换文本。
matchCase可选booleanfalse区分大小写搜索。
wholeWords可选booleanfalse仅匹配整个单词。

返回值​

ApiTextRange

示例​

替换文本范围内某个单词的所有匹配项。

// Useful for bulk text substitution inside a shape.

// Replace every occurrence of "foo" with "bar".

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

const fill = Api.CreateSolidFill(Api.RGB(255, 111, 61));
const stroke = Api.CreateStroke(0, Api.CreateNoFill());
const shape = Api.CreateShape("rect", 300 * 36000, 130 * 36000, fill, stroke);
shape.SetPosition(608400, 1267200);

const docContent = shape.GetDocContent();
docContent.GetElement(0).AddText("foo bar foo");
slide.AddObject(shape);

const range = shape.GetTextRange();
range.Replace("foo", "bar");