Google Docs Script Find and Replace Array Issue

Viewed 57

I am writing a novel in a Google Doc. When complete, it will have probably have in excess of 75 chapters. I am not writing those chapters sequentially. Consequently, I am not attempting to use Chapter numbers, but rather a placeholder of "Chapter_" for each chapter heading (to be used as a search term when I am ready to replace the placeholders).

However, as I get further into the process, I will want to periodically number the chapters sequentially. Rather than do this manually, I would like to be able to automate the process with a Google script.

The following script is almost there, except that it numbers all of the chapters as "Chapter 1." I suspect I need to load an array with the chapter numbers, but despite much research, I can not figure out how to do that.

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Custom')
      .addItem('Renumber Chapters', 'reChapter')
      .addToUi();
}
function reChapter(){
  var doc = DocumentApp.getActiveDocument();
  body = doc.getBody();
  var n = 30;
      Logger.log(n);
  for (i = 0; i < n; i++) { 
    num = i+1
    newText = "Chapter "+num+"";
    body.replaceText("Chapter_", newText);
  }
}

At first, I thought perhaps the loop was not running, but upon checking my log, it is:

2:46:16 PM  Notice  Execution started
2:46:16 PM  Info    30.0
2:46:16 PM  Info    0.0
2:46:16 PM  Info    1.0
2:46:16 PM  Info    2.0
2:46:16 PM  Info    3.0 

Any help would be much appreciated.

Note: My starting point for this code was something close, but not quite on point at: another stackoverflow post/query

Just to be clear, rather than the chapters being renumbered as:

Chapter 1
Chapter 2
Chapter 3
and so on

They are coming out:
Chapter 1
Chapter 1
Chapter 1
etc.
1 Answers

Based upon @andrewjames on-point guidance, I rewrote the script as follows and it works. The key bit of code that made it work came from another discussion here at stackoverflow that @andrewjames brought to my attention (adapted herein in the end of the "reChapter" function and fully in the "replaceIt" function).

  function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Custom')
      .addItem('Renumber Chapters', 'reChapter')
      .addToUi();
}
function reChapter(){
  var n = 30;
  var replaceTerm = "Chapter_"
  var body = DocumentApp.getActiveDocument().getBody();
  for (i = 0; i < n; i++) { 
    num = i+1
    Logger.log(i);
    newText = "Chapter "+num+"";
    replaceIt(replaceTerm, body);
  }
}
// replaces the occurrences
function replaceIt(replaceTerm, body) {    
  var found = body.findText(replaceTerm);
  if (found) {
   var start = found.getStartOffset();
    var end = found.getEndOffsetInclusive();
    var text = found.getElement().asText();
  text.deleteText(start, end);
  text.insertText(start, newText);
  }
}
Related