Number the Headings in Google Docs using Google App Script

Sarmad Gardezi

Is it possible to number the headings in a Google Docs/Drive document? Yes you can automatically place the numbers in heading in Google Docs using Google App Scripts.

I modified the script mentioned by Mikko Ohtamaa and created a Google Apps Script that adds aHeadings toolsDocument menu that allows you to:

  • auto number Heading
  • clear Headings numbers

How to auto number Google Documents Headings:

  1. Open your document > Tools > Script editor…
  2. Start a blank project
  3. Paste the the code below and save with your preffered name
  4. Select Run > onOpen and authorize the script for the first time
  5. Select Run > onOpen
  6. Change to your Document and try the functions on theHeadings toolscustom menu created.

~~Disclaimer:you may have issues with empty Headings.. But you can always fix them and run action again.~~

Code to copy and paste:

function onOpen() {
  DocumentApp.getUi().createMenu('Headings Tools')
  .addItem('Auto Number Headings', 'numberHeadingsAdd')
  .addItem('Clear Heading Numbers', 'numberHeadingsClear')
  .addToUi();
}

function numberHeadingsAdd(){
  numberHeadings(true);
}

function numberHeadingsClear(){
  numberHeadings(false);
}

function numberHeadings(add){
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();
  var paragraphs = document.getParagraphs();
  var numbers = [0,0,0,0,0,0,0];
  for (var i in paragraphs) {
    var element = paragraphs[i];
    var text = element.getText()+'';
    var type = element.getHeading()+'';

    // exclude everything but headings
    if (!type.match(/Heading \d/)) {
      continue;
    }

    // exclude empty headings (e.g. page breaks generate these)
    if( text.match(/^\s*$/)){
      continue;
    }

    if (add == true) {
      var level = new RegExp(/Heading (\d)/).exec(type)[1];  
      var numbering = '';

      numbers[level]++;
      for (var currentLevel = 1; currentLevel <= 6; currentLevel++) {
        if (currentLevel <= level) {
          numbering += numbers[currentLevel] + '.';
        } else {
          numbers[currentLevel] = 0;
        }
      }
      Logger.log(text);
      var newText = numbering + ' ' + text.replace(/^[0-9\.\s]+/, '');
      element.setText(newText);
      Logger.log([newText]);
    } else {
      Logger.log(text);
      element.setText(text.replace(/^[0-9\.\s]+/, ''));
    }
  }

}

If you copy and paste any item of numbered list, then it retains its numbering, and automatically changes the number in relevant cases.

More great articles

Free Programming Books for Pakistani Developers

In this article i'll share a collection of interesting books from different programming spheres like web and mobile app development.…

Read Story

How to Disable Jetpack image Sitemaps from WordPress

Sitemaps are files that list each post and page that should be indexed by search engines like Google or Bing.…

Read Story

Use Yoast SEO Meta Tags in Static html and weebly website

It’s clear that a homepage serves a number of different purposes. Among others, it is your welcoming page and your main…

Read Story
Arrow-up