Google Docs – How to Enable Header Numbering

google docsgoogle-apps-script

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.

Best Answer

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]); 
          }
    }
}