0

Google Docs: How to get Automatic Header Numbering

We all know Google Docs is a great cloud based document editor, however it can always be improved! Luckily it allows you to extend its functionality by writing custom scripts. In this tutorial we’ll show the power of these scripts and teach you how to get automatic header numbering.

 

  1. Open your Google doc, ensure you have your headings labelled with the correct style e.g. Header 1, Header 2 etc.
  2. Select tools -> script editor
  3. A new window will open and you may be asked for script access to be allowed.
  4. Select create script for document
  5. Replace all the code from code.gs with the following:

    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(‘.\t’)
    var result = ‘ok’
    if(chunks.length > 1) {
    paragraph.setText(currCounter+’.\t’+chunks[1]);
    } else {
    paragraph.setText(currCounter+’.\t’+chunks[0]);
    }
    }


    function _getCurrenNumbering(initIndex, counterHeader) {
    var value = ”;
    for ( var i = 0; i <= initIndex; i++) {
    if (value) {
    value += ‘.’;
    }

    value += counterHeader[i];
    }
    return value;
    }
  6. Press Save and give the file a name.
  7. Select Run -> addHeaderNumbering
  8. Return to the document and you will see that numbers have been added to the headings.

If you want to rerun this script do the following:

  1. Open your Google document
  2. Select tools -> Script Editor
  3. Select the script file that contains the header numbering script
  4. Select Run -> addHeaderNumbering
  5. Return to the document and you will see that numbers have been added to the headings.

Thanks to User: Keyjote from Google Product forums. https://productforums.google.com/forum/#!topic/docs/w4MXeqJaefU

About Paul Thomas

Paul is an expert Google Search Consultant and a key member of the Information Governance Business Unit.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>