Riku
Riku

Reputation: 2253

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();
        ...
   }

Upvotes: 4

Views: 7226

Answers (3)

Wicket
Wicket

Reputation: 38160

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

Upvotes: 3

Arun Nagarajan
Arun Nagarajan

Reputation: 5645

Riku - Did you try calling your function as arrayToList(G2:j30)? I believe Apps Script will convert that selected range to a comma separated string (going left -> right and then top->down).

It wont be the same as a the Range object, but at least you can get a CSV string representation of the selected range and perhaps that is sufficient?

Upvotes: 1

megabyte1024
megabyte1024

Reputation: 8660

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.

Upvotes: 2

Related Questions