Mastering Development

How to pass a mix of multiple Arrays and individual arg to another function and return them (GAS/JS)?

I have a main function which do bunch of operations and the output is pushed into arrays along with other multiple values declared in individual variables. Then I need to pass them to another sub function for other bunch of operations then return the updated arrays. I realized that it will not work if I pass the array name only like I pass individual variables as arguments to another function.

function mainfn(){
var sheet=SpreadsheetApp.openById("THIS_SHEET_ID").getSheetByName("wshop");
var sheetdata=sheet.getDataRange().getValues();
var lrow=sheet.getLastRow();

var var1=sheetdata[lrow-1][2];
var var2=sheetdata[lrow-1][3];
var arr1=[],arr2=[];

//some operationsA
arr1.push(1,"str1","3%",5,6,"str2"); //output OpsA being stored in array named arr1

//some operationsB
arr2.push(3,5,6,8,0,1); //output OpsB being stored in array named arr2

mysubfunction(var1,var2,arr1,arr2); // calling subfunction and passing arguments

var arr3=[],var arr4=[];

var arr3=mysubfunction()[0]; // supposedly getting the arr1 updated elements and put into arr3
var arr4=mysubfunction()[1]; // supposedly getting the arr2 updated elements and put into arr4

// do some other stuff over the array elements

function mysubfunction(var1,var2,arr1,arr2){

//some operationsC that updates the elements in both arrays

var result=[arr1,arr2];
return result;

Please help to correct my code on the passing and returning multiple arrays. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *