0

I frequently write research papers that require in-text citations as numbers in brackets - for example: "This is a quote [1]."

As I write the papers, I use letters instead of numbers so I can find & replace later, since I'm likely to add in citations here and there, I don't want to have to renumber every citation to keep them in order.

How do I search through an entire Google doc in a loop, and replace each letter with a number? E.G. replace [a] with [1], [b] with [2] etc. I tried this code, but it replaced every single character in the doc with a random number in brackets. I'm not clear on the rules for escaping brackets, so I've also tried this with a \ before every bracket:

function myFunction() {

    var doc = DocumentApp.getActiveDocument();
    body = doc.getBody();

     var alphabet = ["[a]", "[b]", "[c]", "[d]", "[e]", "[f]", "[g]", "[h]", "[i]", "[j]", "[k]", "[l]", "[m]", "[n]", "[o]", "[p]", "[q]", "[r]", "[s]", "[t]", "[u]", "[v]", "[w]", "[x]", "[y]", "[z]", "[aa]", "[bb]", "[cc]", "[dd]", "[ee]", "[ff]", "[gg]", "[hh]", "[ii]", "[jj]", "[kk]", "[ll]", "[mm]", "[nn]", "[oo]", "[pp]", "[qq]", "[rr]", "[ss]", "[tt]", "[uu]", "[vv]", "[ww]", "[xx]", "[yy]", "[zz]"]; 
      
      for (i = 0; i < alphabet.length; i++) { 
       num = i+1;
       newText = "["+num+"]";
    
    body.replaceText(alphabet[i], newText);
    }
}
1
  • For anyone noticing that replacing [a] with [1], etc all the way down the document doesn't make sense, I should point out that after the research is complete, I'll rearrange the 'alphabet' array based on the order of letters as they appear in my paper. Commented Feb 24, 2018 at 21:43

4 Answers 4

1

Looks like you need to double-escape the brackets:

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


  var alphabet = ["\\[a\\]", "\\[b\\]", "[c]"]; // c is a test-case


  for (i = 0; i < alphabet.length; i++) { 
    num = i+1;
    newText = "["+num+"]";

    body.replaceText(alphabet[i], newText);
  }
}

Before:

A a [a]

B b [b]

C c [c]

After:

A a [1]

B b [2]

C [3] [[3]]
Sign up to request clarification or add additional context in comments.

Comments

0

The .replaceText method matches using regular expressions, so you need to change your search structure to:

body.replaceText("\[" + alphabet[i] + "\]", num + "]")

This will replace every instance of bracketed notes in your document.

Comments

0

Paperpile.com integrates with google docs and will do this all for you.

Comments

0

Have you considered using empty brackets and then placing the numbers when you are done?

e.g. This is a quote[].

When you are done writing the paper, use Find & Replace and search for "[]" and then add in the numbers. Using Find & Replace will ensure that you don't accidentally miss any brackets.

If you don't want to use Find & Replace, would empty brackets help you number them more easily with Apps Script?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.