Skip to main content

Replace

Replaces all occurrences of the specified text within this range.

Syntax​

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

expression - A variable that represents an ApiTextRange class.

Parameters​

NameRequired/OptionalData typeDefaultDescription
findWhatRequiredstringText to search for.
replaceWithRequiredstringReplacement text.
matchCaseOptionalbooleanfalseCase-sensitive search.
wholeWordsOptionalbooleanfalseMatch whole words only.

Returns​

ApiTextRange

Example​

Replace every occurrence of a word inside a text range in a shape on a worksheet.

// The replacement keeps the formatting of the text it replaces.

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

let worksheet = Api.GetActiveSheet();
let fill = Api.CreateSolidFill(Api.RGB(255, 111, 61));
let stroke = Api.CreateStroke(0, Api.CreateNoFill());
let shape = worksheet.AddShape("rect", 200 * 36000, 80 * 36000, fill, stroke, 3, 0, 1, 0);

let docContent = shape.GetContent();
let paragraph = docContent.GetElement(0);
paragraph.AddText("foo bar foo");

let range = shape.GetTextRange();

range.Replace("foo", "bar");

worksheet.GetRange("A1").SetValue("Replace: " + shape.GetTextRange().GetText());