0

I'm trying to define a new Google Sheet formula using Google Script (which have Javascript grammar if I understand it well).

so, given myformula

=MYFORMULA(A1;A2;A3;A4;A5;A6;B7)

I found how to extract all the arguments using this answer

function myFormula(arguments)
{
  var activeRange = SpreadsheetApp.getActiveRange();
  //var activeSheet = activeRange.getSheet();
  var formula = activeRange.getFormula();

  var extr = formula.match(/\(([^()]+)\)/).pop();
  return extr;
}

--> extr = "A1;A2;A3;A4;A5;A6;B7"

i would like to extract the list of all arguments (assuming ";" as separator) and eventually discard the last one, obtaining something like

extr = ["A1", "A2", "A3", "A4", "A5", "A6"];

How can I do with a regex? Single regex or with iteration? (number of items is variable)

marcoresk
  • 1,837
  • 2
  • 17
  • 29
  • 2
    Try replacing your return statement with`return extr.split(';').slice(0, -1)` - `extr.split(';')` gets you `["A1", "A2", "A3", "A4", "A5", "A6", "B7"]` and `.slice(0, -1)` ditches the last element. – Hao Wu Jun 23 '23 at 08:25

1 Answers1

0

I'm writing this answer as a community wiki, since the answer was taken from the comments section, in order to provide a proper response to the question.

As mentioned by @Hao Wu, the return statement can contain an operation that would split the value to get the desired output:

function myFormula(arguments)
{
...

  var extr = formula.match(/\(([^()]+)\)/).pop();
  return extr.split(',').slice(0, -1);
}

enter image description here

Bryan Monterrosa
  • 1,385
  • 1
  • 3
  • 13