1

Google provides an example of optimising a custom formula to recurse over an array where there is one. It helps with the whole efficiency thing The example provided from the Apps Script Page shows the an example where there is 1 parameter, as:

function DOUBLE(input) {
  if (input.map) {            // Test whether input is an array.
    return input.map(DOUBLE); // Recurse over array if so.
  } else {
    return input * 2;
  }
}

What if there are 2 or more parameters? How can we still recurse?

3

1 Answer 1

0

Thank you for your responses. The post to which TheMaster refers is probably right, but to be honest, I don't completely understand it. I have resolved my issue by referring back to Google's pages again. To add further to Ghost's question, I'd like to have one parameter as an array and other other a single constant - sorry for the confusion, I'll be sure to put more thoughts into my future questions.

My updated prototype formula looks like this:

function DOSOMETHING(x, y) {
  return Array.isArray(x) ?
      x.map(i => i.map(cell => cell * y)) :
      x * y;
}

Happy to report it actually does what I want it to. x can be a single number or an array. y can only be a single integer in this case.

Since my original post, Google updated their pages to show the function using ternary operator, which made it easier for me to read & understand.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.