【问题标题】:How can I perform a straight search and replace in a Google Doc?如何在 Google Doc 中执行直接搜索和替换?
【发布时间】:2015-03-24 01:03:09
【问题描述】:

我正在尝试使用 Google 脚本在 Google Doc 中进行搜索和替换。通过网络界面提供的直接文本搜索和替换。

源文档包含 [schoolname] 作为地标,我想复制该原始文档,然后在新文档中搜索并替换 [schoolname]。

我在阅读此主题后尝试了 replaceText("([schoolname])","sometext"):How to use method replaceText(searchPattern, replacement) in documents Service/ text Class

我通过http://www.regexr.com/ 仔细检查了正则表达式,但无济于事

我应该使用一些替代方法来替换 replaceText() 还是只是以某种方式修复正则表达式?提前致谢。

编辑:代码段

var newfile = sourcefile.makeCopy();
newfile.setName(sourcefile.getName().replace("[schoolname]","replacementtext"));
var newdoc = DocumentApp.openById(newfile.getId());
var paras = newdoc.getBody().getParagraphs();
newdoc.getBody().replaceText("(\[schoolname\])","replacementtext");

【问题讨论】:

  • 你能把代码贴在replaceText周围吗?
  • 我添加了一个代码sn-p。

标签: google-apps-script


【解决方案1】:

我发现 ReplaceText 非常不稳定,而不是 ([]) 作为分隔符,使用 %%,所以你不必转义。

【讨论】:

  • 哇。那绝对有效。我使用了replaceText("%schoolname%","replacementtext"),它按预期工作。谢谢@Kriggs
猜你喜欢
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2017-01-05
  • 2015-07-11
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 2012-01-03
相关资源
最近更新 更多