如何获得多个页眉/页脚与谷歌Apps脚本在文档中(How to get multiple heade

2019-10-23 09:32发布

我试着去取代谷歌的文档标题文本具有“不同的第一页页眉/页脚”活动。 我成功地取代其他任何页眉文字除了第一页上。 ...

var something = "Some string.";
var copyId = DriveApp.getFileById(docTemplate).makeCopy(name).getId();
var copyDoc = DocumentApp.openById(copyId);
var copyHeader = copyDoc.getHeader();
copyHeader.replaceText('keySomething', something);

...

我看了看文档,但没有看到如何做到这一点。 我甚至有“getHeaders()”(复数形式)尝试,但该类不存在。 我怎么能代替在第一页的页眉/页脚文本?

谢谢,

伊万

Answer 1:

copyHeader.getParent().getChild(2); //I'm using your variable

指向第一页上标题 。 “2”在getChild(2)可以或可以不变化,但我添加了一个函数, seeChildren()的代码在此答案第三块上。

如果你想替换字符串的所有实例文档中,使用与replaceText()

copyHeader.getParent(); /*this points to the whole document (unless the 
                          header's parents is not the whole)*/
//getbody(), getfooter(), etc (i.e. siblings of header) should work

如果你想知道的x getChild(x) 页脚

function seeChildren(){
  var doc = DocumentApp.getActiveDocument();
  var bod = doc.getBody();
  var fol = bod.getParent();
  for (i = 0; i<fol.getNumChildren(); i++){
    var child = fol.getChild(i);
    bod.appendParagraph(child + ": Index " + fol.getChildIndex(child));
  }
}

这将追加对身体部分文档的儿童(DocumentBodySection,HeaderSection,HeaderSection,FooterSection,FooterSection等)和它们各自的指数的名称(0,1,2,...,子女数减1)。 此外,请注意,这里使用getActiveDocument()文件必须是开放的功能工作。



Answer 2:

copyHeader.getParent().getChild(3).replaceText('current text','new text');

这将指向第一个不同的页面上的标题。



文章来源: How to get multiple headers/footers in a document with Google Apps Script