5

I have a simple little apps script that refreshes our dynamic logo on request. The problem is I can't target the header if the designer checks "Different first page header/footer" checkbox. Is there away to target the different header if it's checked?

Here is the code I'm currently using:

function onOpen() {
  DocumentApp.getUi().createMenu('Branding')
    .addItem('Update Branding', 'updateLogo')
    .addToUi();
}

function updateLogo() {
  var doc = DocumentApp.getActiveDocument();
  var header = doc.getHeader();
  if (header) {
    var images = header.getImages();
    var logoWidth = 250;
    if (images.length > 0) {
      var image = images[0];
      logoWidth = image.getWidth(); // pixels
      image.removeFromParent();
    }

    var freshLogo = UrlFetchApp.fetch("http://example.com/logo.jpg").getBlob();
    var newImage = header.insertImage(0, freshLogo);

    var logoRatio = newImage.getHeight() / newImage.getWidth();

    newImage.setWidth(logoWidth);
    newImage.setHeight(newImage.getWidth() * logoRatio);
  }
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Ian Davis
  • 63
  • 1
  • 4

3 Answers3

4

I discovered the following after going through a few of the open issues in Eric's Google Issues link.

Here's what I found, and I'll use a clean example to illustrate.

  1. Create a new Google Doc
  2. Insert the text "Common header" into the header section
  3. Insert the text "Common footer" down in the footer section
  4. Tick the Different first page header/footer option
  5. In the now blank header section enter the text "Different first header"
  6. In the now blank footer section enter the text "Different first footer"

Open up the Script Editor and run the function below:

function getSectionText() {
  var d = DocumentApp.getActiveDocument();
  var p = d.getBody().getParent(); 
  // let's loop through all the child elements in the document
  for ( var i = 0; i < p.getNumChildren(); i += 1 ) {
    var t = p.getChild(i).getType();
    if ( t === DocumentApp.ElementType.BODY_SECTION ) continue; // not interested in the body
    if ( t === DocumentApp.ElementType.HEADER_SECTION ) {
      var h = p.getChild(i).asHeaderSection().getText();
      Logger.log( 'Child index number: ' + i );
      Logger.log( h );
    } else if ( t === DocumentApp.ElementType.FOOTER_SECTION ) {
      var f = p.getChild(i).asFooterSection().getText();
      Logger.log( 'Child index number: ' + i );
      Logger.log( f );
    }
  }  
}

When you view your log file your should see the following output:

[17-09-22 16:33:03:628 AEST] Child index number: 1
[17-09-22 16:33:03:629 AEST] Common header
[17-09-22 16:33:03:633 AEST] Child index number: 2
[17-09-22 16:33:03:633 AEST] Common footer
[17-09-22 16:33:03:636 AEST] Child index number: 3
[17-09-22 16:33:03:637 AEST] Different first header
[17-09-22 16:33:03:659 AEST] Child index number: 4
[17-09-22 16:33:03:660 AEST] Different first footer

This should help you with interacting with the common and first page headers and footers.

rs77
  • 8,737
  • 2
  • 19
  • 19
0

Unfortunately Apps Script's DocumentApp isn't aware of the different first page header/footer setting. Please file a feature request here: https://code.google.com/p/google-apps-script-issues/issues/list

Eric Koleda
  • 12,420
  • 1
  • 33
  • 51
0

This one should work. You may have to use doc.getHeader()(the first page header) along with this, the DHeader (the header of the 2nd, 3rd, 4th... pages.).

var doc = DocumentApp.getActiveDocument();
var header = doc.getHeader();
var DHeader = header.getParent().getChild(2).asHeaderSection();

    /*
      Some may seem pointless as seeing that getParent().getChild(2)
          may be replaced somehow with getNextSibling(), however,
          googlescript does not allow
          the use of getNextSibling() for a HeaderSection
    */
  • This works!, but you need to find wich child is your header. In my case was 3. var DHeader = header.getParent().getChild(3).asHeaderSection(); – Mario Zamora Oct 25 '18 at 20:17