Can I add Heading text to the footer in a Google Docs file automatically?

Viewed 127

I have a document that consists of approximately 20 chapters, divided over 60 pages. Each new chapter starts at the top of a new page. What I would like to do, is to automatically add the active Chapter title to the footer of that page. I know this behavior is possible in Microsoft Word, but I can not find it in Google Docs.

It can be done manually by inserting section breaks, but that is inconvenient for me, since I want to use this process in over 1.000 different documents.

Example:

Chapter 1 is called "Test chapter" and starts at page 1 Chapter 2 is called "Another chapter" and starts at page 4

Then on page 1, 2 and 3 the footer of the page should contain the text "Test Chapter". On page 4, the footer should contain the text "Another chapter".

Thank you in advance!

1 Answers

Unfortunately, this still isn't available as of the moment and has no update since the issue of the bug.

I have been trying to circumvent this issue via Apps Script and never had a proper solution as the footers and headers are being treated as one.

By default, changing a footer section will apply to all footer sections, and if they were separated by section break, they are not ordered by page but by the order of them being added on the document so it would be tricky and would require you to add them in this order for my code to work.

Object Order:

order

After BODY_SECTION, next children of the document should be alternating HEADER_SECTION then FOOTER_SECTION.

NOTE:

  • Logger.log part of the code shows what is the object order of your document. If it looks like the above order, then the code below will work.

Code:

function setHeaderAsFooter() {
  var d = DocumentApp.getActiveDocument();
  var p = d.getBody().getParent(); 
  
  for ( var i = 0; i < p.getNumChildren(); i += 1 ) {
    var c = p.getChild(i);
    var t = c.getType();
    // Check what type is the object
    // Comment out if-block below to see if what is the object order in your document
    Logger.log(t); 
    // Every header you encounter, set it as footer value of the next object
    if ( t === DocumentApp.ElementType.HEADER_SECTION) {
      var f = c.getNextSibling(); 
      f.asFooterSection().setText(c.asHeaderSection().getText());
    }
  }
}

If they aren't in HEADER -> FOOTER alternating order after the BODY, then the code above won't work.

Most likely, this is not the answer you were hoping for, so I do sincerely apologize. I hope that this answer will help you in any way at least.

Related