-1

I have come across custom functions in Google apps script. It involves writing a function which return value after process.

How do I write the below IFS function using apps script

function ifs(args)
{
????
}

I now want to understand how the variable argumnets enabled custom functions to be created?

For example the ifs function takes variable number of arguments

IFS function
Evaluates multiple conditions and returns a value that corresponds to the first true condition.

Sample Usage
IFS(A1>90, "A", A1>80, "B", A1>70, "C")

IFS({A1:A5} > 3, "bigger", {A1:A5} = 3, "equal")

Syntax
IFS(condition1, value1, [condition2, value2, …])

condition1 - The first condition to be evaluated. This can be a boolean, a number, an array, or a reference to any of those.

value1 - The returned value if condition1 is TRUE.

condition2, value2, … - Additional conditions and values if the first one is evaluated to be false.

1

2 Answers 2

1

Use rest parameter:

function DOUBLE(...input) {
  return input.map(arg => arg * 2);
}
console.log(DOUBLE(5,10,15))
console.log(DOUBLE(1, -1, 0.5))

For a custom ifs, try

const customIFS = (...args) => {
 for (let i = 0; i < args.length; ++i){
    if (i % 2 === 0 && typeof args[i] === "boolean" && args[i]) return args[i+1];
 }
}
console.log(customIFS(1>2, "A", 2>1,"B"));
const big = 10, test=1000;
console.log(customIFS(big>test, "less",test===big," equal to big", test>big,"Bigger than big"))

Sign up to request clarification or add additional context in comments.

2 Comments

I dont need a DOUBLE function to be written, I want the IFS function to be implemented not DOUBLE
@CodeGuy Let me know if the latest code works out for you.
0

From the question

I now want to understand how the variable argumnets (sic) enabled custom functions to be created?

Short answer:

Use the arguments object

Extended answer:

Google Sheets custom function doesn't work exactly as built-in functions like IFS. I.E. when writing a function, a function pop-up help might be shown like this

To create a custom function pop-up help we could use JSDOC

While JSDOC syntax has a way to set optional parameters, they are not shown on the custom function pop-up help, but it's possible to get all the custom function arguments by using the arguments object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.