ZehnVon12
ZehnVon12

Reputation: 4176

How to number the headings in a Google Docs/Drive document?

Is it possible to number the headings in a Google Docs/Drive document?

Upvotes: 176

Views: 157673

Answers (7)

BriceM
BriceM

Reputation: 1

Firsly thanks to Luciano for this share.

I modified its code to also keep the number when the title starts with a number.

For example:

Title: 1000 euros, taxable threshold ?

Old script :

  1. euros, taxable threshold ?

New script

  1. 1000 euros, taxable threshold ?

Code:

function onOpen() {
  DocumentApp.getUi().createMenu('Numérotation des titres')
  .addItem('Ajouter / Mettre à jours Numérotation', 'numberHeadingsAdd')
  .addItem('Supprimer Numérotation', '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]+[.]/, ''));
    }
  }
}

Upvotes: 0

Aitor
Aitor

Reputation: 3429

If you want something more easy, there is a Google Add-On called "Table of Contents" that will allow you to number your headings.

To install this add-on:

  1. Click on the Add-Ons > Get Add-Ons.
  2. Click on the "Table of Contents" icon or search for this addon to install it

Then your Table of Contents should appear in your sidebar. Click on Heading Numbers Format menu, and choose 1.2.3

You have to reformat your document if you have an old one in order to 'refresh' the numbers, but actually the addon works very well.

I've seen the answer in this forum.

Upvotes: 147

Michael ten Den
Michael ten Den

Reputation: 401

Since writing (April 2020), a lot of proposed apps are obsolete or do not working anymore.

The method for numbered headings I've found working is the following:

  1. Go to Get add-ons
  2. Search and install Markdown Tools

Markdown Tools has an option to use numbered headings. Works like a charm with the built-in Table of Contents of Google.

To create a table of contents:

  1. Go to menu item Insert and select Table of Contents

Upvotes: 30

jordan2175
jordan2175

Reputation: 938

I wrote a version for doing markdown headings, but it also supports plain heading numbers as well. The source is here https://github.com/jordan2175/markdown-tools and is available via G Suite Marketplace as "Markdown Tools".

Upvotes: 5

Luciano
Luciano

Reputation: 1072

Update: now available in github.

Update 2: now handling empty headings and blank lines thanks to 2 pull requests on github.

Update 3: github and code below fixed to handle new Docs HEADING identification.


I modified the script mentioned by Mikko Ohtamaa and created a Google Apps Script that adds a Headings tools Document 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 the Headings tools custom 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]+/, ''));
    }
  }

}

Upvotes: 60

David L&#243;pez
David L&#243;pez

Reputation: 403

The previously mentioned add-on "Table of contents" is not available as of today. I installed the one called "Markdown Tools" Once installed, you must apply native heading styles and then go to Add-Ons>Markdown Tools >Heading numbers and choose the desired numbering style which will be appplied to all headings in the doc.

Upvotes: 1

alexkovelsky
alexkovelsky

Reputation: 4198

All you have to do is copy/paste previous headers.

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

Upvotes: 23

Related Questions