gpt4 book ai didi

javascript - 如何使用 Google Apps 脚本在文档中获取多个页眉/页脚

转载 作者:行者123 更新时间:2023-11-30 12:21:29 24 4
gpt4 key购买 nike

我正在尝试替换 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()”(复数),但该类不存在。如何替换第一页页眉/页脚中的文本?

谢谢,

伊万

最佳答案

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

如果你想知道 getChild(x) 的 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));
}
}

这将附加文档子项的名称(DocumentBodySection、HeaderSection、HeaderSection、FooterSection、FooterSection 等)及其在正文部分的各自索引(0、1、2、...、子项数减 1) .另请注意,这使用了 getActiveDocument(),文件必须打开才能使函数工作。

关于javascript - 如何使用 Google Apps 脚本在文档中获取多个页眉/页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com