Google-docs – Convert all headings X into headings X + 1 or heading X – 1 in Google Documents

google docs

Is a way to convert all headings X into headings X + 1 or heading X – 1 in Google Documents?

E.g. I have a Google document with 100 headings 2: is there any way to convert them into headings 1 or 3 in a convenient fashion (i.e. without having to manually change the heading for each 100 of them)?

Best Answer

With this little script you can change the headings automatically. Especially useful when the different headings are convoluted.

Code

function changeHeading() {
  var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();  
  for(var i in pars) {
    var p = pars[i], h = p.getHeading(), d = DocumentApp.ParagraphHeading;
    switch(h) {
      case h.HEADING1:
        p.setHeading(d.HEADING2);
        break;
      case h.HEADING2:
        p.setHeading(d.HEADING1);
        break;
    }
  }
}

Explained

First all paragraph types are collected. Then these are checked for headings. If a heading matches Heading 1, then it's changed to Heading 2 etc.

Add this script in your Google Document under Tools > Script editor.... Press the "bug" button to authenticate the script.