Google 应用程序脚本 - 复制脚注内容时保留链接

Google apps script - retain links when copying footnote content

背景

我有一个 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() returns the element contents as a String, not a rich text object/element 其中包含格式信息。

要到达 Text 对象:

  1. getFootnoteContents().getChild(0) returns FootnoteSection Paragraph
  2. getChild(0).getChild(0) returns 该段的 Text 对象
  3. copy() returns 要使用的文本对象的分离深层副本

注意: 如果 FootnoteSection 或其 Paragraph 子元素中还有其他子元素,您需要添加某种 type/index 检查得到正确的。但是,使用基本脚注 - 如上例 - 这是正确的路径。

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

  ...

}