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.
Web Applications Stack Exchange is a question and answer site for power users of web applications. It only takes a minute to sign up.
Sign up to join this communityWell, 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]);
}
}
}
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
)⌘⇧7
)Return
⌘⌥1
), type "Second" and press Return
⌘⌥1
), type "Third" and press Return
At that Point you should have something that looks like this:
Return
twiceNow your document should look something like this:
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:
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;
}
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.
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:
I released this add-on a couple of weeks ago. Check it out on my homepage or in the add-on store.
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:
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