如何在文档Service / text Class中使用方法replaceText(searchPattern,replacement) [英] How to use method replaceText(searchPattern, replacement) in documents Service/ text Class

查看:84
本文介绍了如何在文档Service / text Class中使用方法replaceText(searchPattern,replacement)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换文本文档中的唯一字符串(实际上很多字符串,但每个字符串都是唯一的),所以我尝试了 doc.editAsText()。replaceText(old $,new $) ; 但没有运气...
这里是我使用的代码,它复制了一个包含应该在循环中替换的字符串的模板。

  var doc = DocumentApp.openById(docId);; 
var lib = [$ titre,$ nom,$prénom,$ rue,$ code,$ ville,$ pays]
for(nn = 1; nn <= selrange.length; ++ nn){
for(ll = 0; ll var old $ =(lib [ll] +ン)的ToString();
var new $ = selrange [nn-1] [ll] .toString();
var test = old $ .replace(old $,new $);
Logger.log(new $ +=+ test); //这确实是新值
doc.editAsText()。replaceText(old $,new $);


Logger.log(doc.getText())
}

记录器显示文件内容不变。
我错过了什么?



编辑:有关信息,在Henrique的答案之后是工作代码:

对于(page = 0; page< feuilles; ++ page){
var today = Utilities.formatDate(new Date(),FUS1,dd-MM- +_+__+ Utilities.formatDate(新日期(),FUS1,HH:mm)
var docname =IMPRESSION_page _+ Number(page + 1)+_+ today;
var docId = DocsList.copy(doctemplate,docname).getId();
var doc = DocumentApp.openById(docId);; (nn = 1; nn <= 16)的
var lib = [titre,nom,prénom,rue,code,ville,pays]
; ++ nn){
for(ll = 0; ll var olditem =(#+ lib [ll] + nn +#);
var newitem = selrange [nn-1 + page * 16] [ll]; (newitem ==){newitem =}
//Logger.log(olditem +*) );
}
}
Utilities.sleep(300); //在每个文档创建之间等待一段时间


解决方案

问题在于,即使你传递了一个字符串, Document.replaceText 函数也会创建一个正则表达式(这实际上是在 docs 示例:)。你的测试中的 String.replace 函数不会。这里是区别:

var doc = DocumentApp.openById(docID);
var old $ ='$ sample';
var new $ ='$ replaced';
var test ='foo $ sample bar';
Logger.log(test.replace(old $,new $)); //旧被视为文字
Logger.log(test.replace(new RegExp(old $),new $)); //这是replaceText的作用
doc.replaceText(old $,new $);
Logger.log(doc.getText());

由于 $ 是一个特殊字符一个正则表达式,它正在搞乱你的替换。顺便说一下,这意味着文本的结束,并且在结束后尝试匹配任何文本将永远不会工作,所以基本上,您的搜索和替换都不会做任何事情。



为了解决这个问题,要么在所有字符串中跳过 $ ,例如'\ $ sample',或者不要将此字符用作字段分隔符,这是一个不错的选择!顺便说一下,即使在你的旧$ 中,也可以使用#或任何其他在正则表达式中没有特殊含义的字符。

c $ c>和 new $ 变量看起来很丑。但这可能只是我:)

I need to replace a unique string in a text document (well actually a lot of strings but each one is unique ) so I tried doc.editAsText().replaceText(old$,new$); but with no luck... here is the code I use, it copies a template that contains strings that should be replaced in a loop.

  var doc = DocumentApp.openById(docId);;
  var lib=["$titre","$nom","$prénom","$rue","$code","$ville","$pays"]
    for(nn=1;nn<=selrange.length;++nn){
      for(ll=0;ll<lib.length;++ll){
        var old$ = (lib[ll]+nn).toString();
        var new$ = selrange[nn-1][ll].toString();
        var test = old$.replace(old$,new$);
Logger.log(new$+" = "+test);// this is indeed the new value
        doc.editAsText().replaceText(old$,new$);
         }
       }
Logger.log(doc.getText())
   }  

the Logger shows the content of the document unchanged . What am I missing ?

EDIT : For information, after Henrique's answer here is the working code :

    for(page=0;page<feuilles;++page){
      var today=Utilities.formatDate(new Date(),FUS1,"dd-MM-yyyy")+"__"+Utilities.formatDate(new Date(),FUS1,"HH:mm")
      var docname="IMPRESSION_page_"+Number(page+1)+"_"+today;
      var docId=DocsList.copy(doctemplate,docname).getId();
      var doc = DocumentApp.openById(docId);;
      var lib=["titre","nom","prénom","rue","code","ville","pays"]
        for(nn=1;nn<=16;++nn){
          for(ll=0;ll<lib.length;++ll){
            var olditem = ("#"+lib[ll]+nn+"#");
            var newitem = selrange[nn-1+page*16][ll];
              if(newitem==""){newitem="   "}
//Logger.log(olditem + "   *"+newitem+"*")
              doc.replaceText(olditem,newitem);
         }
       }
      Utilities.sleep(300); // wait a bit between each doc creation
    } 

解决方案

The problem is that the Document.replaceText function will create a regex even if you pass a string to it (that's actually in docs example :). And the String.replace function in your test won't. Here is the difference:

var doc = DocumentApp.openById(docID);
var old$ = '$sample';
var new$ = '$replaced';
var test = 'foo $sample bar';
Logger.log(test.replace(old$, new$)); //old is treated as literal
Logger.log(test.replace(new RegExp(old$), new$)); //this is what replaceText does
doc.replaceText(old$,new$);
Logger.log(doc.getText());

And since $ is a special character in a regex, it's messing your replace. By the way, it means the end of the text, and trying to match any text after the end will never work, so basically your search and replace does nothing, ever.

To fix this you got to, either escape the $ in all your strings e.g. '\$sample', or don't use this character as field delimiter, it's such a bad choice! Use #, or any other char that does not have a special meaning in regexes.

By the way, even in your old$ and new$ variables it looks ugly. But that's probably just me :)

这篇关于如何在文档Service / text Class中使用方法replaceText(searchPattern,replacement)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆