0

NB This is Google Docs not Google Sheets.

My add-in interfaces with an external server storing bibliographic data which can then be inserted in the Google Document. Formatting of these data is managed on the external server (APA, MLA, Harvard etc.) so the intention is that the user, through the add-in, can select another format and the server sends the newly formatted references to the document which then replace the old references (and a bibliography is added or amended to the new style). This is how I've got it working on my MS Word add-in.

I'm struggling to replicate this on Google Docs. On a previous question I was directed to use namedRange as an equivalent to Word's content container – some encapsulation in which I could place the bibliographic reference, keeping it separate from other text in the document, and then manipulate the reference in it as I wish. Here's how I insert the original reference from the external server and then encapsulate it in a named range:

  var document = DocumentApp.getActiveDocument();
  var element = cursor.insertText(ref);
  var rangeBuilder = document.newRange();
  rangeBuilder.addElement(element);
  var tag = 'wikindxW!K!NDXidW!K!NDX' + JSON.stringify([url, id]);
  document.addNamedRange(tag, rangeBuilder.build());

where ref is the bibliographic reference pulled from the external server (url and id likewise are variables relating to the external server and the reference stored there).

This might produce something like: enter image description here

in the document. If I then use something like:

  var document = DocumentApp.getActiveDocument();
  var ranges = document.getNamedRanges();
  for (i = 0; i < ranges.length; i++) {
    rangeElements = ranges[i].getRange().getRangeElements();
    for (j = 0; j < rangeElements.length; j++) {
      text = rangeElements[j].getElement().getText();
      debug.push(["rangeNo. " + i + ': ' + "elementNo. " + j + ': ' + text]);
    }
  }

to interrogate the namedRanges in the document and do a console log of the debug array, I get: "rangeNo. 0: elementNo. 0: (Czyzewski, Rostek, Odya, & Zielinski 2001)" So far so good.

The problems start when I add other text on the same line. For example: enter image description here produces: "rangeNo. 0: elementNo. 0: Initial text (Czyzewski, Rostek, Odya, & Zielinski 2001) final text."

Even another reference added on the same line (using the addNamedRange() code above) simply adds itself to the existing range on the line: enter image description here produces: "rangeNo. 0: elementNo. 0: Initial text (Czyzewski,…a, & Zielinski 2001) final text. (Järvinen 2005)" and: "rangeNo. 1: elementNo. 0: Initial text (Czyzewski,…a, & Zielinski 2001) final text. (Järvinen 2005)"

Apparently two named ranges now exist but with the same content.

If I insert a reference on a new line in the document: enter image description here I get the two debugging lines above and the third range: "rangeNo. 2: elementNo. 0: (Crisinel & Spence 2010)"

This unexpected/misunderstood behaviour extends into replacing the text within my namedRange. If I have a namedRange with the following text: enter image description here then replace the namedRange's text with the reference formatted differently using:

  var document = DocumentApp.getActiveDocument();
  var ranges = document.getNamedRanges();
  tag = 'wikindxW!K!NDXidW!K!NDX' + JSON.stringify([url, id);
    for (j = 0; j < ranges.length; j++) {
      if (tag == ranges[j].getName()) {
        rangeElements = ranges[j].getRange().getRangeElements();
        for (k = 0; k < rangeElements.length; k++) {
          element = rangeElements[k].getElement();
        }
      }
    }
    element.setText(text);

the namedRange I want is found, the text is replaced, and I end up as expected with: enter image description here But then the namedRange itself disappears.

Either namedRange does not do what I want (keep the references encapsulated from other text and other references so I can manipulate them as I want) or I misunderstand how to use them.

Any ideas?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.