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

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

问题描述

我需要替换文本文档中的唯一字符串(实际上有很多字符串,但每个字符串都是唯一的)所以我尝试了 doc.editAsText().replaceText(old$,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 ?

有关信息,在 Henrique 的回答之后,这里是工作代码:

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
    } 

推荐答案

问题在于 Document.replaceText 函数会创建一个正则表达式,即使你向它传递一个字符串(实际上是在 Document.replaceText一个 href="https://developers.google.com/apps-script/class_document#replaceText" rel="noreferrer">docs 示例 :).并且测试中的 String.replace 函数不会.区别如下:

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.

要解决此问题,请在所有字符串中转义 $,例如'$sample',或者不使用这个字符作为字段分隔符,这是一个糟糕的选择!使用 # 或任何其他在正则表达式中没有特殊含义的字符.

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.

顺便说一下,即使在您的 old$new$ 变量中,它看起来也很丑陋.但这可能只是我 :)

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

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

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