3

I currently use

SUBSTITUTE(text, search_text, new text, occurrence)

to replace text, but I am starting to find that I have a list of things to replace, the above method will become something like

SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(...)...)...)...

very messy, whats a better method of making this work?

4

1 回答 1

3

您也许可以使用该REGEXREPLACE(text, regular_expression, replacement)函数来使用正则表达式。

如果这不起作用,可以使用 Google Apps 脚本编写自定义函数。也许你可以写一个可以接受多个要替换的字符串。

编辑:我刚刚写了这样一个函数:

function SubstituteAll(text) {
  arguments = Array.prototype.slice.apply(arguments);
  arguments.shift();
  var replacement = arguments.pop();
  for(var i=0; i<arguments.length; i++)
  {
    text = text.replace(arguments[i], replacement);
  }
  return text;
}

用法:

SubstituteAll(text, search_text1, search_text2, ..., replacement_text)
于 2011-04-06T03:16:27.127 回答