Thank you in advance.
I am trying to multiple finds and replace google docs with a reference google sheet
Find | Replace
----------------------------
bill number one | Bill No. 1
Bill number one | Bill No. 1
Bill number 1 | Bill No. 1
Bill Number One | Bill No. 1
Bill Number 1 | Bill No. 1
function replMyText(){
var ss=SpreadsheetApp.openById('1-WblrS95VqsM5eRFkWGIHrOm_wGIPL3QnPyxN_j5cOo');
var sh=ss.getSheetByName('find and replace');
var doc=DocumentApp.getActiveDocument();
var docrange=doc.getBody()
var rgtxt=doc.getBody();//text to replace
var rgrep=sh.getRange('A2:B103');//replacement table
var txtA=rgtxt.getText();
var repA=rgrep.getValues();
for(var i=0;i<txtA.length;i++){
for(var j=0;j<repA.length;j++){
if(txtA[i][0]==repA[j][0]){
txtA[i][0]=repA[j][1];
}
}
}
rgtxt.setText(txtA);
}
I did not get the output.
I believe your goal as follows.
In this case, I thought that replaceText()
might be able to be used. When your script is modified, it becomes as follows.
function replMyText() {
var ss = SpreadsheetApp.openById('1-WblrS95VqsM5eRFkWGIHrOm_wGIPL3QnPyxN_j5cOo');
var sh = ss.getSheetByName('find and replace');
var doc = DocumentApp.getActiveDocument();
var rgtxt = doc.getBody();
var rgrep = sh.getRange('A2:B103');
var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
repA.forEach(e => rgtxt.replaceText(...e));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.