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

标签 javascript google-apps-script google-docs footnotes

背景

我有一个 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/

相关文章:

javascript - 基于组件的架构 [Javascript]

google-apps-script - 如何在 Google Apps Script 提供的 HTML 站点中使用 Angular.js?

regex - 是否可以在 CalendarApp.getEvents() 中使用带有搜索参数的正则表达式?

pdf - OCR 如何在 Google Drive 中工作?

javascript - 单击时 Vue.js 交换类

javascript - 如何保存 Asp :Image Element Generated on the Client Side

javascript - Google 电子表格和工作表

c# - Google API Ver 2 中的可恢复文件上传

google-apps-script - 从 Google 文档中的两个不同表中删除行很困难

javascript - 尝试使用嵌套对象数据循环对象并使用我的卡中的数据进行渲染