1

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
    } 
Serge insas
  • 45,904
  • 7
  • 105
  • 131

1 Answers1

7

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

Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65
  • ha! not even Stackoverflow's syntax highlight liked the $ after the new variable! So, it's not just me :D – Henrique G. Abreu May 17 '12 at 01:11
  • Henrique, I wish I knew only one tenth of what you know and my life would be so simpler !! Anyway, thanks to your help I managed to get all of it working nicely. Cheers, Serge – Serge insas May 17 '12 at 12:56
  • Thanks Serge, but you surely know much more than that. I probably just have more free time to play with this. By the way, please check my answer as "accepted". Cheers, Henrique – Henrique G. Abreu May 17 '12 at 14:59