Applying an array formula to a cell using an app script

1,044 views
Skip to first unread message

Mister D

unread,
Apr 14, 2021, 11:33:50 AM4/14/21
to Google Apps Script Community

Howdy, 

I am trying to pos the the below array formula to a specific cell via an appscript

=ARRAYFORMULA(IFERROR( VALUE(SUBSTITUTE(REGEXEXTRACT(TO_TEXT(B2:B); "\d+,\d+|\d+.\d+|\d+"); "."; ",")); ))

The closest I have been able to come up with is 

  function reset() {
  var keys = ['1jK3AEfVcLR-vk6tpAC0lw4A6OWWkQXXXXXXXXXXX']; // list of spreadsheet ids
  var formula = ['=ARRAYFORMULA(IFERROR( VALUE(SUBSTITUTE(REGEXEXTRACT(TO_TEXT(B2:B); ""\"d+,"\"d+|"\"d+."\"d+|"\"d+"); "."; ",")); ))']
  for (var i = 0; i < keys.length; i++) {
    var ss = SpreadsheetApp.openById(keys[i]);
    var sheet = ss.getSheetByName('Dataincoming');
    sheet.getRange('D2').setFormula(formula)
  }
}

However, what I actually get in the cell is =ARRAYFORMULA(IFERROR( VALUE(SUBSTITUTE(REGEXEXTRACT(TO_TEXT(B9:B); "d+,d+|d+.d+|d+"); "."; ",")); ))

Note all the \ have been removed. I've a wall and I am hoping someone would be able to point me in the right direction to retain the \ within the formula. 

Any assistance much appreciated. 

xse

unread,
Apr 17, 2021, 4:42:55 PM4/17/21
to Google Apps Script Community

Hey, backslashes need to be escaped

let formula = `=ARRAYFORMULA(IFERROR( VALUE(SUBSTITUTE(REGEXEXTRACT(TO_TEXT(B2:B); "\\d+,\\d+|\\d+.\\d+|\\d+"); "."; ",")); ))`;

Kim Nilsson

unread,
Apr 18, 2021, 3:21:58 AM4/18/21
to Google Apps Script Community
Why the use of slanted quotation marks?

I see you use ` and not regular '.

xse

unread,
Apr 18, 2021, 3:53:15 AM4/18/21
to Google Apps Script Community
It's an habit, i'm used to making strings with template literals i find it cleaner when you have to put variables inside and of the 3 different type of quotation marks they're the one you're less likely to have to escape mid-string.
Doesn't matter at all in this case thought.

Kim Nilsson

unread,
Apr 18, 2021, 3:54:55 AM4/18/21
to Google Apps Script Community
Ok, so GAS accepts it as another (third) type of quotation?

Kim

xse

unread,
Apr 18, 2021, 4:07:05 AM4/18/21
to Google Apps Script Community
Totally yeah they're treated as strings with the ability to embed  placeholders instead of having to do string concatenation with a lot of + signs and so on.
here's a screenshot:
app_script_template_literals.png

Kim Nilsson

unread,
Apr 18, 2021, 4:50:46 AM4/18/21
to Google Apps Script Community
Oh, that's great. I do use + concatenation in some scripts.

Kim

Adam Morris

unread,
Apr 18, 2021, 5:37:10 AM4/18/21
to google-apps-sc...@googlegroups.com
It’s not a GAS feature, it’s really just JavaScript and the new V8 engine.

It’s incredibly useful.

On Sun, Apr 18, 2021 at 3:54 PM 'Kim Nilsson' via Google Apps Script Community <google-apps-sc...@googlegroups.com> wrote:
Ok, so GAS accepts it as another (third) type of quotation?

Kim

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CACK38a5fu9T2S3ZxB5ccPAgT8tcruxuVhrc8rH81Z3PUHFdurA%40mail.gmail.com.
--

Bruce Mcpherson

unread,
Apr 18, 2021, 5:53:17 AM4/18/21
to google-apps-sc...@googlegroups.com
JavaScript introduced ‘templates literals’ to allow variable substitution with strings, and it became  available via v8

Here’s a write up



On Sun, Apr 18, 2021 at 8:22 AM 'Kim Nilsson' via Google Apps Script Community <google-apps-sc...@googlegroups.com> wrote:
Why the use of slanted quotation marks?

I see you use ` and not regular '.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages