【发布时间】: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。