Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

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.

share|improve this question

migrated from superuser.com Feb 14 '12 at 10:14

This question came from our site for computer enthusiasts and power users.

5 Answers 5

up vote 3 down vote accepted

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]); 
          }
    }
}
share|improve this answer
1  
You're right, google scripting is so powerful! –  Martin Delille Jul 22 '13 at 16:53

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

share|improve this answer
    
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... –  Martin Delille Feb 29 '12 at 16:54
2  
@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 '12 at 19:04
1  
Nice! That's a trick ;) –  Martin Delille Mar 1 '12 at 19:00
    
@tinmaru if the answer is helpful don't forget to mark it ;) –  Lipis Mar 2 '12 at 13:57
1  
yes i'm sorry I totally forgot! Thanks for your help :) –  Martin Delille Mar 4 '12 at 18:05

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

share|improve this answer

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.

share|improve this answer

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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