Google Docs Chapter Fixer

Hey! Do you use Google Docs? Are you writing in chapters? Do you sometimes insert or remove chapters? Then this script is for you!

To add it to your Doc, open it up, then go to Tools -> Script Editor. Copy and paste the following code (you might also need to save the script project, as something like “MyScripts”).

function FixChapterNumbers()
{
  var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
  var chapterCounter = 1;
  for(var i=0; i<pars.length; i++)
  {
    var par = pars[i];
    var parText = par.getText();
    if ((parText.length < 12) && (parText.slice(0, 7) == "Chapter"))
    {
      var fixedChapterString = "Chapter " + chapterCounter;
      par.replaceText(parText, fixedChapterString);
      chapterCounter++;
    }
  }
}

As long as your Chapters use just the text “Chapter 3”, “Chapter 4”, and so on, they will now be put in order, starting at 1. This only affects the “Chapter X” text in the title, not the body of any of the chapters.

If you’re writing your novel for NaNoWriMo, you know time is of the essence! Perhaps this can be useful for you.

This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *