问题
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.
- Create a new Google Doc
- Insert the text "Common header" into the header section
- Insert the text "Common footer" down in the footer section
- Tick the Different first page header/footer option
- In the now blank header section enter the text "Different first header"
- 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