How To Replace Text in Multiple Document With Google APP Script

Advertisements

I have multiple document as Master Document. If I want to re-use this multiple files, I must edit it one by one, with every document has some text to replace.
I try to replace multiple Text at single document with Google APP Script, and work.
How to replace multiple text in multiple document with Document ID in the Google Spreadsheet List?
There is the script I Try to built.

 function replaceText() {

 const spreadsheet   = SpreadsheetApp.getActiveSpreadsheet();
 const sheetIDList   = spreadsheet.getSheetByName('DOC ID');
 const colId         =  3 ; 
 const firstRowID    =  2 ;
 const lastRowId     = 17 ;
 const n             = lastRowId-firstRowID ;

      let startRow = firstRowID     // Baris awal data DOC ID
      for (let i = 0 ; i < n; i++)

 var   docId = sheetIDList.getRange(startRow+i,colId).getValue();
 var   doc = DocumentApp.openById(docId);  
 var   body = doc.getBody();

       body.replaceText("Kepala SMK Negeri 7 Semarang","Kepala SMA Negeri 13 Semarang");
       body.replaceText("SMK Negeri 7 Semarang", "SMA Negeri 13 Semarang");
       body.replaceText("Haris Wahyudi, S.Pd. M.Pd", "Rusmiyanto, S.Pd., M.Pd.");
       body.replaceText("19751222 200003 1 002", "19690812 199803 1 013");
       body.replaceText("Pembina Tk. I", "Pembina");
 }

I hope the script can run to replace text for every document in the list (document id).

>Solution :

Modification points:

  • In your script, for (let i = 0 ; i < n; i++) is used just under the line of for (let i = 0 ; i < n; i++). By this, the loop doesn’t correctly work. Please be careful about this.
  • getValue is used in a loop. In this case, the process cost will become high.

When these points are reflected in your script, how about the following modification?

Modified script:

function replaceText() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheetIDList = spreadsheet.getSheetByName('DOC ID');
  const colId = 3;
  const firstRowID = 2;
  const lastRowId = 17;
  const n = lastRowId - firstRowID;
  let startRow = firstRowID     // Baris awal data DOC ID

  // I modified the below script.
  var values = sheetIDList.getRange(startRow, colId, n).getDisplayValues();
  values.forEach(([docId]) => {
    if (!docId) return;
    var doc = DocumentApp.openById(docId);
    var body = doc.getBody();
    body.replaceText("Kepala SMK Negeri 7 Semarang", "Kepala SMA Negeri 13 Semarang");
    body.replaceText("SMK Negeri 7 Semarang", "SMA Negeri 13 Semarang");
    body.replaceText("Haris Wahyudi, S.Pd. M.Pd", "Rusmiyanto, S.Pd., M.Pd.");
    body.replaceText("19751222 200003 1 002", "19690812 199803 1 013");
    body.replaceText("Pembina Tk. I", "Pembina");
  });
}
  • When this script is run, the document IDs are retrieved from "C2:C16". And, your replaceText is run to each document.

Note:

  • In this case, when the document ID is invalid ID, an error occurs. Please be careful about this.

References:

Leave a ReplyCancel reply