0

Is it possible to use DocumentApp to find the location of footnote references in the body?

Searching the body or an element using editAsText() or findText() does not show the superscript footnote markers.

For example, in the following document:

This is a riveting story with statistics!1 You can see other stuff here too.

body.getText() returns 'This is a riveting story with statistics! You can see other stuff here too.' No reference, no 1

If I want to replace, edit, or manipulate text around the footnote reference (e.g. 1 ), how can I find its location?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Joseph Fraley
  • 1,360
  • 1
  • 10
  • 26

2 Answers2

2

It turns out that the footnote reference is indexed as a child in the Doc. So you can get the index of the footnote reference, insert some text at that index, and then remove the footnote from its parent.

function performConversion (docu) {

  var footnotes = docu.getFootnotes() // get the footnote

  var noteText = footnotes.map(function (note) {
    return '((' + note.getFootnoteContents() + ' ))' // reformat text with parens and save in array
  })

  footnotes.forEach(function (note, index) {
    var paragraph = note.getParent() // get the paragraph

    var noteIndex = paragraph.getChildIndex(note) // get the footnote's "child index"

    paragraph.insertText(noteIndex, noteText[index]) // insert formatted text before footnote child index in paragraph

    note.removeFromParent() // delete the original footnote
  })
}
Joseph Fraley
  • 1,360
  • 1
  • 10
  • 26
  • Good! if it is the answer to your question, then mark this answer of **yours** as correct, it helps other users to find an answer. – Br. Sayan Mar 06 '16 at 10:33
0

You can use getFootnotes() to edit footnotes. getFootnotes() returns an arrays of objects , you need to iterate over them.

You can list the locations (i.e Parent Paragraphs) of footnotes in Logger.log(), in the following fashion:

    function getFootnotes(){
      var doc = DocumentApp.openById('...');
      var footnotes = doc.getFootnotes();
      var textLocation = {};

  for(var i in footnotes ){
      textLocation = footnotes[i].getParent().getText();
      Logger.log(textLocation);    

  }    
}

To get the paragraph truncated right upto the footnote superscript. You can use:

textLocation = footnotes[i].getPreviousSibling().getText();

in your case it should return: This is a riveting story with statistics! only this portion, because [1] is just after the word statistics!

Br. Sayan
  • 486
  • 1
  • 7
  • 22
  • Thanks Br. Sayan. I think my question is confusing because I didn't know how to format superscript in Markdown. I've revised my question to better reflect my intended meaning. – Joseph Fraley Mar 06 '16 at 09:30