Francesco Santoro
Francesco Santoro

Reputation: 11

Streamline multiple IF / ranges script

I wondered if this script below:

function provaIf() {
 var spreadsheet = SpreadsheetApp.getActive();
   if(spreadsheet.getRange('A1').getValue()  == 1){
   spreadsheet.getRange('B1').activate(); }
   else {
   spreadsheet.getRange('C1').activate();
   }
  }

you can streamline by making a shorter script. I write you an example below to help you understand what I have in mind:

if(A1=1;B1;C1)

I await your answers.

Upvotes: 0

Views: 52

Answers (2)

Andres Duarte
Andres Duarte

Reputation: 3340

The code as you have it's in the simplest way. The only change you could do, is to use a ternary operator [1] instead of an if statement, like this:

var spreadsheet = SpreadsheetApp.getActive();
(spreadsheet.getRange('A1').getValue()==1)?spreadsheet.getRange('B1').activate():spreadsheet.getRange('C1').activate();

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Upvotes: 0

Cooper
Cooper

Reputation: 64062

function provaIf() {
  var dA=SpreadsheetApp.getActiveSheet().getRange('A1:C1').getValues();
  return (dA[0][0]==1)?dA[0][1]:dA[0][2];
}

I hope you realize that you can't run this as a cell function since there are no inputs so it doesn't change when you change A1. Unless you rerun the function.

Upvotes: 1

Related Questions