11

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)?

9

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.

| improve this answer | |
  • The above script has a bug in it. It changes all existing Heading 1 to Heading 2 and it changes all existing heading 2 to Heading 1, when it should be Heading 3. This version just swaps Heading 1 and Heading two instead of pushing everything down a level. – August Mohr Sep 22 '19 at 23:04
  • @AugustMohr I thought the OP meant heading 1 or 3. You can alter the script anyway you like to see for yourself. – Jacob Jan Tuinstra Sep 23 '19 at 5:33
6

After a couple of days of more 'normal' activity levels no better offer yet so I suggest:

Change "Heading 2" to the required style and then "Update 'Heading 2' to match.

This does not add or subtract from the Heading reference number but at least allows easy propagation across the 100 headings to be changed, by altering one only.

| improve this answer | |
1

I figured out a work-around when I was copying text from another Google Doc that already had h1, h2, etc that didn't fit into the target document.

If you highlight text, in the right-click menu, there is "Select all matching text". This selects all text with the same formatting in the document. For instance, if you select an H2 text, it will select all H2 texts. If you select bolded, italicized text, it will select all bolded, italicized text.

First, what I needed to do was cut the copied text and paste it into a new document. That's because I didn't want to select H1s in the whole document; I just want the H1 in the copied text. So I needed the copied text alone in another document.

Then, in that new document, I selected an H1, right-clicked, and selected "Select all matching text". This highlighted all the H1s in the copied text. I changed them to H3 with ctrl+alt+3, which is what I needed them to be in the new document. Same deal with H2s, demoting them to H4s.

Then I cut and pasted the demoted text back into the target document. Done!

| improve this answer | |
0

You can use the following two scripts to increase or decrease the heading levels.

function decreaseHeadingLevel() {
  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.TITLE:
        p.setHeading(d.HEADING1);
        break;
      case h.HEADING1:
        p.setHeading(d.HEADING2);
        break;
      case h.HEADING2:
        p.setHeading(d.HEADING3);
        break;
      case h.HEADING3:
        p.setHeading(d.HEADING4);
        break;
      case h.HEADING4:
        p.setHeading(d.HEADING5);
        break;
      case h.HEADING5:
        p.setHeading(d.HEADING6);
        break;
    }
  }
}
function increaseHeadingLevel() {
  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.TITLE);
        break;
      case h.HEADING2:
        p.setHeading(d.HEADING1);
        break;
      case h.HEADING3:
        p.setHeading(d.HEADING2);
        break;
      case h.HEADING4:
        p.setHeading(d.HEADING3);
        break;
      case h.HEADING5:
        p.setHeading(d.HEADING4);
        break;
      case h.HEADING6:
        p.setHeading(d.HEADING5);
        break;
    }
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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