[英]Google apps script - retain links when copying footnote content
我有一个Google Apps脚本,用于解析脚注内容,用双括号括起来代替脚注编号上标。 预期结果应该是:
在脚本之前
这是我的足球指数。 1
1这是我的内容,内容与链接和重点 。
脚本之后
这是我的足球指数。 ((这是我的内容, 链接和重点 。)
一切正常,除非我用双括号解析脚注,它们正在丢失所有的链接和格式:
这是我的足球指数。 ((这是我的内容,链接和重点。)
如果有人可以帮我修复下面的代码,我真的很感激帮助:)
function convertFootNotes () {
var doc = DocumentApp.getActiveDocument()
var copy = generateCopy(doc) // make a copy to avoid damaging the original
var openCopy = doc; //DocumentApp.openById(copy.getId()) // you have to use the App API to copy, but the Doc API to manipulate
performConversion(openCopy); // perform formatting on the copy
}
function performConversion (docu) {
var footnotes = docu.getFootnotes(); // get the footnotes
footnotes.forEach(function (note) {
// Traverse the child elements to get to the `Text` object
// and make a deep copy
var paragraph = note.getParent(); // get the paragraph
var noteIndex = paragraph.getChildIndex(note); // get the footnote's "child index"
insertFootnote(note.getFootnoteContents(),true, paragraph, noteIndex);
note.removeFromParent();
})
}
function insertFootnote(note, recurse, paragraph, noteIndex){
var numC = note.getNumChildren(); //find the # of children
paragraph.insertText(noteIndex," ((");
noteIndex++;
for (var i=0; i<numC; i++){
var C = note.getChild(i).getChild(0).copy();
if (i==0){
var temp = C.getText();
var char1 = temp[0];
var char2 = temp[1];
if (C.getText()[0]==" "){
C = C.deleteText(0,0);
}
}
if (i>0){
paragraph.insertText(noteIndex,"\n");
noteIndex++;
}
paragraph.insertText(noteIndex,C);
noteIndex++;
} //end of looping through children
paragraph.insertText(noteIndex,"))");
}
function generateCopy (doc) {
var name = doc.getName() + ' #PARSED_COPY' // rename copy for easy visibility in Drive
var id = doc.getId()
return DriveApp.getFileById(id).makeCopy(name)
}
比其他的在那里的代码做任何改动添加))
使其无法正常工作? 删除((
& ))
仍然没有在测试时应用格式; getText()以String
返回元素内容,而不是包含格式信息的富文本对象/元素 。
要获取Text
对象:
getFootnoteContents().getChild(0)
返回FootnoteSection Paragraph
getChild(0).getChild(0)
返回该段落的Text
对象 注意:如果FootnoteSection
或其Paragraph
子项中有其他子元素,您将需要添加某种类型/索引检查以获取正确的子元素。 但是,使用基本脚注 - 如上例所示 - 这是正确的路径。
function performConversion (docu) {
var footnotes = docu.getFootnotes() // get the footnotes
var noteText = footnotes.map(function (note) {
// Traverse the child elements to get to the `Text` object
// and make a deep copy
var note_text_obj = note.getFootnoteContents().getChild(0).getChild(0).copy();
// Add the `((` & `))` to the start and end of the text object
note_text_obj.insertText(0, " ((");
note_text_obj.appendText(")) ");
return note_text_obj // reformat text with parens and save in array
})
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.