簡體   English   中英

Google應用腳本 - 在復制腳注內容時保留鏈接

[英]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對象:

  1. getFootnoteContents().getChild(0)返回FootnoteSection Paragraph
  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
  })

  ...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM