57

Is there a way to automatically number headings in Google Docs?

It used to be possible with CSS but this feature isn't supported anymore in the new Google Docs version.

1

7 Answers 7

11

Well, it seems easier to do that by scripting the document like this:

var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
var counterh1 = 0;
for(var i=0; i < pars.length; i++) {
    var par = pars[i];
    var hdg = par.getHeading();
    if (hdg == DocumentApp.ParagraphHeading.HEADING1) {
          counterh1++; 
          var content = par.getText();
          var chunks = content.split('\t');
          if(chunks.length > 1) { 
              par.setText(counterh1+'.\t'+chunks[1]); 
          } else {
              par.setText(counterh1+'.\t'+chunks[0]); 
          }
    }
}
2
  • 1
    You're right, google scripting is so powerful! Jul 22, 2013 at 16:53
  • 1
    This only works in Google Workspace, not for basic users. Sep 24, 2021 at 18:07
22

Actually it is possible even without editing the HTML/CSS. I'll give you an example with a blank document but once you'll figure out how it works you will be able to do it on already existed document. Also if you are not already using the keyboard shortcuts I would suggest you to start using them (in this example I'm using Mac's shortcuts, but you can easily find the correct ones for your own OS).

  1. Create a new document
  2. Apply Heading 1 (⌘⌥1)
  3. Apply Numbered List (⌘⇧7)
  4. Type "First" and press Return
  5. Apply Heading 1 (⌘⌥1), type "Second" and press Return
  6. Apply Heading 1 (⌘⌥1), type "Third" and press Return

At that Point you should have something that looks like this:

Numbered Headers in Google Docs

  1. Get your cursor at the end of the First header
  2. Press Return twice
  3. Type "Hello World!"
  4. Repeat the procedure for the rest of the headers

Now your document should look something like this:

Numbered headers with some extra text in between

As you might noticed the automatic numbering is still there and you can easily experiment by adding more Headers as you wish and even numbered sub-headers if needed. I'm not going into more details, but here you can check an example with nested numbered lists with correct formatting for Headers:

Complete example for numbered headers

7
  • 1
    This looks nice but doesn't seem to be fully functional: how do I add a new heading 1 at the end of the document? If I set the heading 1 and then apply the number list it start numbering from 1... Feb 29, 2012 at 16:54
  • 4
    @tinmaru That's a little bit tricky.. but still doable. Go to the end of the previous header that you want to continue the numbering, press enter and then apply heading. Afterwards cut that (never mind for the number that is left behind), go to the end of the document and paste it. You'll see that the number is correct now, so simply go and fix the previous error by deleting the empty header if any.
    – Lipis
    Feb 29, 2012 at 19:04
  • 1
    Nice! That's a trick ;) Mar 1, 2012 at 19:00
  • @tinmaru if the answer is helpful don't forget to mark it ;)
    – Lipis
    Mar 2, 2012 at 13:57
  • 1
    yes i'm sorry I totally forgot! Thanks for your help :) Mar 4, 2012 at 18:05
11

Hope this helps

/*
Credits: 

https://productforums.google.com/forum/#!topic/docs/w4MXeqJaefU
http://webapps.stackexchange.com/questions/23861/header-numbering-in-google-docs

Instructions to use:

In a Google Doc

Go to Tools > Script Editor 
Select the option to create the script for Google Docs.
Replace the Content of Code.gs with the code below.
Save it and name the project as say addHeaderNumbering.
Click play icon for the function addHeaderNumbering (authorize it when asked).

*/

function addHeaderNumbering () {
  var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
  var counterHeader = [0, 0, 0, 0, 0, 0];

  for(var i=0; i<pars.length; i++) {
    var par = pars[i];
    var hdg = par.getHeading();
    if (hdg == DocumentApp.ParagraphHeading.HEADING1) {

      _addNumberingForHeaderType(DocumentApp.ParagraphHeading.HEADING1, par, 0, counterHeader);
    } else if (hdg == DocumentApp.ParagraphHeading.HEADING2) {
      _addNumberingForHeaderType(DocumentApp.ParagraphHeading.HEADING2, par, 1, counterHeader);
    } else if (hdg == DocumentApp.ParagraphHeading.HEADING3) {
      _addNumberingForHeaderType(DocumentApp.ParagraphHeading.HEADING3, par, 2, counterHeader);
    } else if (hdg == DocumentApp.ParagraphHeading.HEADING4) {
      _addNumberingForHeaderType(DocumentApp.ParagraphHeading.HEADING4, par, 3, counterHeader);
    } else if (hdg == DocumentApp.ParagraphHeading.HEADING5) {
      _addNumberingForHeaderType(DocumentApp.ParagraphHeading.HEADING5, par, 4, counterHeader);
    } else if (hdg == DocumentApp.ParagraphHeading.HEADING6) {
      _addNumberingForHeaderType(DocumentApp.ParagraphHeading.HEADING6, par, 5, counterHeader);
    }
  }
}


function _addNumberingForHeaderType(headerType, paragraph, initIndex, counterHeader) {
  counterHeader[initIndex] = counterHeader[initIndex] + 1;
  var currCounter = _getCurrenNumbering(initIndex, counterHeader);
  for(var ii = initIndex + 1; ii < counterHeader.length; ii++) {
    counterHeader[ii] = 0;
  }
  var content = paragraph.getText();
  var chunks = content.split('. ')

  var result = 'ok'
  if(chunks.length > 1) {

    paragraph.setText(currCounter+'. '+chunks[1]); 
  } else { 
    paragraph.setText(currCounter+'. '+chunks[0]);
  }
}


function _getCurrenNumbering(initIndex, counterHeader) {
  var value = '';
  for ( var i = 0; i <= initIndex; i++) {
    if (value) {
      value += '.';
    }
    value += counterHeader[i];
  }

  return value;
}
4

I know that this might not be relevant for the OP anymore, but there is now a posibility to use add-ons. There is a function in the Table of Contents add-on, where you can select the numbering scheme for the heading and it does the required:

1
1.1
2
2.1
2.1.1

I hope that it will be useful to something.

Link to the add-on

Disclaimer: This might be working only on Chrome, but hey, at least it works.

How to use: There is a widget on the right hand side and at the top of it you can select your numbering scheme and refresh the headings with the usual refresh button.

So just set the numbering scheme and update the heading after you are done with editing and you will have correct and updated section/subsection/... numbers.

3
  • Excellent. To me this is "the" answer. Works on Firefox for me. Sep 22, 2014 at 11:38
  • 2
    The app (despite more than a million installs) is dead. Jan 23, 2020 at 23:24
  • Yep, it's dead, Jim
    – guero64
    Apr 18, 2022 at 14:54
2

Now there is an add-on called Heading Numbers that you can use to add any custom numbering style. It allows you to choose/define, update and remove automatic heading numbers.

Predefined styles:

  • 1.2.3 – Digits without trailing dot
  • 1.2.3. – Digits with trailing dot
  • a.b.c – Lowercase letters
  • A.B.C – Uppercase letters
  • i.ii.ii – Lowercase roman
  • I.II.III – Uppercase roman
  • 1) a) i) – Changing style followed by a trailing parenthesis

I released this add-on a couple of weeks ago. Check it out on my homepage or in the add-on store.

1
  • The FAQ entry for How do I add automatic heading numbering? says: "If the add-on is installed, choose “Add-ons” -> “Heading numbers“, and then choose your style." But when adding it to a document, that did not show up. I had to reload the document before "Heading numbers" showed up in the menu. Probably a Google bug and not a bug in the Heading numbers app.
    – bgoodr
    Sep 20, 2015 at 15:41
2

I did a quick comparison between Table of Contents app and the Numbered Headings apps using Firefox. Admittedly, this info will become dated if things improve for both apps:

  1. There is no online help for use of the Table of Contents app (either never was or it got taken down) The help button points to a page that tries to up-sell you with a trial http://lumapps-www.appspot.com/en/ instead of providing help. Heading Numbers provides a FAQ at http://www.lorut.no/add-ons/heading-numbers-for-google-docs/faq/.
  2. The Table of Contents and Heading Numbers apps don’t play well together in terms of renumbering the headings: I created numbered headings using Heading Numbers, and applied similar headings using Table of Contents, and it added two sets of numbers into the headings. The same is the case if you reverse the order by applying the headings using Table of Contents first, then Heading Numbers.
  3. Table of Contents doesn’t have the same number of heading styles as Heading Numbers does.
  4. But having the side-bar in Table of Contents is still handy. I select As Is from the Table of Contents side bar and am able to click the refresh button to see and navigate the table of contents.
  5. Heading Numbers has a Clear operation. Table of Contents does not. The Clear operation does not fix the interaction issue described above, which is quite painful because you have to manually fix them.
  6. Neither app updates a Table of Contents that is inserted into the document (via the Insert/Table of Contents menu).
1

What I was looking for was getting the headings like so:

1
1.1
1.1.1
1.1.1.1
etc

and I could not find any, so I did my own script, I posted it here http://productforums.google.com/forum/#!topic/docs/w4MXeqJaefU

Not the answer you're looking for? Browse other questions tagged or ask your own question.