Search code examples
google-apps-scriptgoogle-sheetsgoogle-docs

How To Replace Text in Multiple Document With Google APP Script


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('RENAME');
 const colId = 4;
 const firstRowID = 2;
 const lastRowId = 17;
 const n = lastRowId - firstRowID;
 let startRow = firstRowID     // Baris awal data DOC ID

 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 SMK Negeri 3 Jepara");
 body.replaceText("SMK Negeri 7 Semarang", "SMK Negeri 3 Jepara");
 body.replaceText("Haris Wahyudi, S.Pd., M.Pd.", "SUSWANTO DJONY PURNAWAN, S.Pd., M.Pd");
 body.replaceText("19751222 200003 1 002", "19700531 200401 1 001");
 body.replaceText("Pembina Tk. I", "Pembina");
 body.replaceText("SEKOLAH MENENGAH KEJURUAN NEGERI 7 SEMARANG", "SEKOLAH MENENGAH KEJURUAN NEGERI 3 JEPARA");
          
  });
 }

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: