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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…