0

i am trying to make a simple automation.

But i am struggling with the basics.

Please help me.

The values in the cell b3 will be manually updated.

It will have either add or sub

If it is add, then b1 and b2 should make addition operation. If it is sub, then b1 and b2 should make subtraction operation.

The cell b5 displays the operation types in b3 - this is for cross check.

I have used if and else if functions. But i am not getting the results as expected.

Can you please help me with correcting the codes.

Image attached for reference.enter image description here

Thanks, Prabhu

1 Answers1

1

In your if/else statements, you want to use == instead of =. You use == to do a comparison (asking, are these equal?) and = to do an assignment (set these equal).

Also, you want to enclose the 'add' and 'sub' in quotation marks, since you want to know if c is the same as the string 'add' and not if c is the same as the numeric value of the variable add.

So I would suggest

function myFunction() {
  
  var ss=SpreadsheetApp.getActive();

  var a=ss.getRange('b1').getValue();
  var b=ss.getRange('b2').getValue();
  var c=ss.getRange('b3').getValue();

  ss.getRange('b5').setValue(c);

  var add = a+b;
  var sub = a-b;

  if(c=='add'){
    ss.getRange('b7').activate().setValue(add);
  }
  else if (c=='sub'){
    ss.getRange('b8').activate().setValue(sub);
  }
}