gpt4 book ai didi

javascript - Google Apps 脚本 - 复制脚注内容时保留链接

转载 作者:行者123 更新时间:2023-11-29 15:17:46 25 4
gpt4 key购买 nike

背景

我有一个 Google Apps 脚本,我们用它来解析脚注内容,用双括号括起来,代替脚注编号上标。预期的结果应该是:

脚本之前

This is my footie index.1


1This is my footie content with a link and emphasis.

脚本之后

This is my footie index. (( This is my footie content with a link and emphasis.)

问题

一切正常,除了当我解析双括号中的脚注时,它们丢失了所有链接和格式:

This is my footie index. (( This is my footie content with a link and emphasis.)

如果有人可以帮助我修复下面的代码,我将不胜感激 :)

解决方案:

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 而不是 rich text object/element 返回其中包含格式信息。

获取 Text 对象:

  1. getFootnoteContents().getChild(0) 返回 FootnoteSection 段落
  2. getChild(0).getChild(0) 返回该段落的 Text 对象
  3. copy()返回要使用的文本对象的分离深拷贝

注意:如果 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
})

...

}

关于javascript - Google Apps 脚本 - 复制脚注内容时保留链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47423880/

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