Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

google apps script - How to use method replaceText(searchPattern, replacement) in documents Service/ text Class

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
    } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...