Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use an array as argument for my custom function in Google Apps Script in a spreadsheet

I'm trying to create a custom function in my google spreadsheet with the script editor.

I can't seem to allow the user to give the function an array as the argument.

My script works when I hardcode like this:

    var values = SpreadsheetApp.getActiveSheet().getRange("G2:j30").getValues();

What I want to do is pass the array as an argument:

   function arrayToList(chosenArray) {
        ...
        var values = SpreadsheetApp.getActiveSheet().getRange(chosenArray).getValues();
        ...
   }
like image 581
Riku Avatar asked Oct 22 '25 16:10

Riku


2 Answers

There are two ways to pass an array as argument to a Google Sheets custom function

  1. By using a range reference, like A1:D5. Usage: =myCustomFunction(A1:D5).
  2. By using the Google Sheets array notation like {1,2;"Yellow","Orange"}. Usage: =myCustomFunction({1,2;"Yellow","Orange"}). Note: If your spreadsheet is set to use comma as decimal separator use / as column separator

References

  • https://developers.google.com/apps-script/guides/sheets/functions
  • Using arrays in Google Sheets
like image 55
Rubén Avatar answered Oct 24 '25 05:10

Rubén


Now this functionality is not implemented in GAS. There are similar questions in SO (here and here). You can post a feature request to the issue tracker.

like image 35
megabyte1024 Avatar answered Oct 24 '25 04:10

megabyte1024