Google apps script for text documents: regexp replace to footnote / link

Posted on

Google apps script for text documents: regexp replace to footnote / link – Whether you’re just starting out with Google Sheets or are a seasoned pro, sooner or later one of your formulas will give you a formula or script parse error message rather than the result you want. However, every Formula user, irrespective of whether it’s a beginner or expert, has invariably come across a formula parse error in Google Sheets at least once in their life (and if you haven’t. you soon will). It can be frustrating, especially if it’s a longer formula where the formula parse error may not be obvious. In this article, we will talk about some common script or formula parse errors in Google Sheets, like Google apps script for text documents: regexp replace to footnote / link, and how to fix it.

Using Google apps script, in a google text document (i.e. just a regular documenet), I would like to convert the following string

{ some text | footnote | link }

to

some text

with a footnote attached, and a link added to the text.

So it would be something like this:

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var myRegexp = new RegExp('{(.*)|(.*)|(.*)}','g');
body.replaceText(myRegexp, "$1");

followed by adding the footnote and link to the result. I can’t get the replaceText to work (and there’s a note about capture group support in the documentation).

Would really appreciate some help with a complete recipe on how to do this!

Answer :

Leave a Reply

Your email address will not be published.