【问题标题】:How to get multiple headers/footers in a document with Google Apps Script如何使用 Google Apps 脚本在文档中获取多个页眉/页脚
【发布时间】:2015-06-16 20:16:44
【问题描述】:
我试图替换“不同的首页页眉/页脚”处于活动状态的 Google 文档页眉中的文本。我成功地替换了除第一页之外的任何其他页眉中的文本。
...
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()”(复数形式),但该类不存在。
如何替换第一页页眉/页脚中的文本?
谢谢,
伊万
【问题讨论】:
标签:
javascript
google-apps-script
google-docs
【解决方案1】:
copyHeader.getParent().getChild(2); //I'm using your variable
这将指向第一页上的标题。 getChild(2) 中的“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
如果你想知道 footer 的 getChild(x) 的 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(),文件必须打开才能使函数工作。
【解决方案2】:
copyHeader.getParent().getChild(3).replaceText('current text','new text');
这将指向第一个不同页面上的标题。