使用Google Apps脚本如何在文档中获取多个页眉/页脚 [英] How to get multiple headers/footers in a document with Google Apps Script

查看:163
本文介绍了使用Google Apps脚本如何在文档中获取多个页眉/页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图替换Google文档标题中的不同首页页眉/页脚活动的文本。我成功替换了除首页以外的任何其他页面标题中的文本。
...

  var something =一些字符串。; 
var copyId = DriveApp.getFileById(docTemplate).makeCopy(name).getId();
var copyDoc = DocumentApp.openById(copyId);
var copyHeader = copyDoc.getHeader();
copyHeader.replaceText('keySomething',something);

...



该文件,但没有看到如何做到这一点。我甚至尝试过使用getHeaders()(复数形式),但该类不存在。
如何替换第一页页眉/页脚中的文字?



谢谢,

Iván

解决方案

  copyHeader.getParent()。getChild(2); //我使用变量

这将指向第一页上的标题即可。 getChild(2)中的2可能会也可能不会有所不同,但我添加了一个函数, seeChildren() ,在这个答案中的第三个代码块。



如果您试图替换文档中的所有字符串实例,请使用 replaceText()

  copyHeader.getParent(); / *这指向整个文档(除非
标头的父母不是整体)* /
// getbody(),getfooter()等(即标头的兄弟)应该工作

如果您想知道的getXild(x)页脚

 函数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(),该文件必须打开才能使用。


Im trying to replace a text in a Google Document header which has "Different first page Header/Footer" active. I succesfully replace text in any other page header except on first page. ...

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

...

I looked at the documentation but didn't see how to do it. I even tried with "getHeaders()" (in plural) but that class doesn't exist. How I can replace the text in the first page header/footer?

Thanks,

Iván

解决方案

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

This will point to the header on the first page. The "2" in getChild(2) may or may not vary but I have added a function, seeChildren(), on the third block of code in this answer.

If you are trying to replace all instances of a string in the document, use this with 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

If you want to know the x of getChild(x) for the footer,

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

This will append the names of the document's children (DocumentBodySection, HeaderSection, HeaderSection, FooterSection, FooterSection, etc) and their respective indices on the body section (0,1,2,...,number of children minus 1). Also, note that this uses getActiveDocument(), the file must be open for the function to work.

这篇关于使用Google Apps脚本如何在文档中获取多个页眉/页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆