1

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");
          
  });
 }

1 Answers1

1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Awesome. Thanks, again @Tanaike I always get your quick response and help. In just a minute I post question, you solved my problem. It's 100% run well. – PSAB Plalangan May 11 '23 at 00:38
  • @PSAB Plalangan Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike May 11 '23 at 00:39
  • @PSAB Plalangan I have to apologize for my poor English skill. Unfortunately, I cannot understand `Hello @Tanaike, maybe you have time to help me, again. :) . Instead of writing a list of sentences to be replaced in App Script, I want the list of words to be replaced in a list in a spreadsheet, and I think it's more easier to anyone use this script without open and edit Apps Script, when they want to add word to be replaced. For this question, I modified the script, in my Edited Question above. Thanks`. Is this your new question? – Tanaike May 21 '23 at 11:52
  • @PSAB Plalangan Thank you for replying. I understood it. – Tanaike May 21 '23 at 12:01