Bob Umadbro Bob Umadbro - 3 months ago 27
Javascript Question

Google Apps Script---Replace Straight Quotation Marks with Curly Ones

When I was typing a novel on my Google Docs iPad app, it used straight up-and-down quotation marks, like this: ". Now, I want to change all of these quotes to the curly kind, without having to change all of them by hand.

I wrote a simple Google Apps Script file to deal with the issue, but when I run it, it seems to say "Running function myFunction..." indefinitely.

Here is my code. The first few lines deal with quotes in the middle of the sentence, using a simple replaceText method. Meanwhile, the while statement tests if there is a line break (\n) before the quote, and uses that to determine whether to put a beginning or end quote.

function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();

//Replace quotes that are not at beginning or end of paragraph
body.replaceText(' "', ' “');
body.replaceText('" ', '” ');

var bodyString = body.getText();
var x = bodyString.indexOf('"');
var bodyText = body.editAsText();

while (x != -1) {
var testForLineBreaks = bodyString.slice(x-2, x);
if (testForLineBreaks == '\n') { //testForLineBreaks determines whether it is the beginning of the paragraph
//Replace quotes at beginning of paragraph
bodyText.deleteText(x, x);
bodyText.insertText(x, '“');
} else {
//Replace quotes at end of paragraph
bodyText.deleteText(x, x);
bodyText.insertText(x, '”');
}
x = bodyString.indexOf('"');
}
}


I can't seem to find what's wrong with it. And to confuse things more, when I click the debugger, it says


Too many changes applied before saving document. Please save changes in smaller batches using Document.saveAndClose(), then reopen the document with Document.openById().


I appreciate all help with this. Thank you in advance!

Answer

some1 was right about the error message, but unfortunately that did not get to the root of the problem:

At the end of my while loop, the variable bodyString was being used to find the location of quotation marks to change. The problem was that bodyString was just that, a string, and so I needed to update it each time I made a change to the document.

Another problem, albeit more basic, was that Google counts \n as one character, so I had to change the parameter in var testForLineBreaks = bodyString.slice(x-2, x); to x-1, x.

After tinkering with these issues, my finished code looked like this:

function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();

  //Replace quotes that are not at beginning or end of paragraph
  body.replaceText(' "', ' “');
  body.replaceText('" ', '” ');

  var bodyString = body.getText();
  var x = bodyString.indexOf('"');
  var bodyText = body.editAsText();

  while (x != -1) {
    var testForLineBreaks = bodyString.slice(x-1, x);

    if (testForLineBreaks == '\n') { //testForLineBreaks determines whether it is the beginning of the paragraph
      //Replace quotes at beginning of paragraph
      bodyText.deleteText(x, x);
      bodyText.insertText(x, '“');
    } else {
      //Replace quotes at end of paragraph
      bodyText.deleteText(x, x);
      bodyText.insertText(x, '”');
    }

    body = DocumentApp.getActiveDocument().getBody();
    bodyString = body.getText();
    x = bodyString.indexOf('"');
    bodyText = body.editAsText();
  }
}

There is one remaining problem with the code. If the quote is at the very beginning of the document, as in the first character, then the script will insert the wrong quote style. However, I plan on fixing that manually.