Home > Mobile >  Google sheets app script pass values as range or individual cells
Google sheets app script pass values as range or individual cells

Time:01-09

I am creating a custom function which processes strings held in cells on a sheet. How can I access the strings from within the function in a common way even if the function is given a range or a list of cells or a combination?

e.g. calling the function myFunction(A1:B2) or myFunction(A1,A2,B1,B2) or even myFunction(A1:A2,B1,B2) etc.

Do I need to determine inside the function how it has been called and combine all the different cell/range contents or is there a Google sheets api that can resolve all possible calls into a single range?

Thanks

CodePudding user response:

Just use .flat:

function myFunction(...args){
  const input = args.flat(2);
  //do stuff with input
}

CodePudding user response:

For each argument, determine the type and do according to them

function myfunction(...args) { 
  var types=[]
  args.forEach(x => {
    if (typeof x == "string"){
      types.push([typeof x,x])
      // do something
    }
    else if (typeof x == "number"){
      types.push([typeof x,x])
      // do something else
    }
    else if (typeof x == "object"){
      x.flat().forEach(function(y){
        if (typeof y == "string"){
          types.push([typeof y,y])
          // do something
        }
        else if (typeof y == "number"){
          types.push([typeof y,y])
          // do something else
        }
      })
    }
  })
  return types
}
  •  Tags:  
  • Related