KirklandBrown373
KirklandBrown373

Reputation: 105

Google Scripts Custom Function - Array of Ranges as a Parameter

I am relatively new to creating custom functions in Google Scripts.

I need to be able to pass in an array of ranges into a function.

Please let me know if I am approaching this incorrectly.

I am getting a parse error when I reference it in Google Sheets as =ConcatEach(["A1:A2","B1:B2"],"$"]) (Without the quotes.)

(Also, the values are arbitrary. I know dollar signs go before the value.)

Anyway, here is what I have:

 function ConcatEach(rangeString, concatString)
 {
  //var rangeString = ["A1:A2","B1:B2"];
  //var concatString = "$";

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var rangeValues=[];
  for(i=0;i<rangeString.length;i++)
  {
    var range = sheet.getRange(rangeString[i]);
    rangeValues.push(range.getValues());
  }

  //Logger.log(rangeValues);

  for(i=0;i<rangeValues.length;i++)
  {
    for(j=0;j<rangeValues[i].length;j++)
    {
      if(rangeValues[i][j] != "")
      {
        rangeValues[i][j] = rangeValues[i][j] + concatString;
      }
    }
  }
  return rangeValues;
}

Any help would be super appreciated! Thank you!

Upvotes: 1

Views: 241

Answers (1)

Max Makhrov
Max Makhrov

Reputation: 18717

you may pass rangeString as string with commas:

"A1:A2,B1:B2"

and then use JavaScript String split() Method:

var array =  rangeString.split(",");
Logger.log(array); // you'll get array ['A1:A2', 'B1:B2']

Upvotes: 1

Related Questions