Google Apps Script targeting header on doc with different first page headers

橙三吉。 提交于 2020-07-06 11:31:49

问题


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);
  }
}

回答1:


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.




回答2:


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




回答3:


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
    */


来源:https://stackoverflow.com/questions/31598708/google-apps-script-targeting-header-on-doc-with-different-first-page-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!