0

I am new to Google Sheet App Script and trying to to create a IF condition with it but receiving an error.

I want to populate This "Right" result in Cell AO3

Your help will be appreciated

function myFunction() {

 var sheet = SpreadsheetApp.getActive().getSheetByName('DATA')
 var ss = sheet.getRange()

   if(ss.getRange("i3").getValue()!='' 
   && 
   ss.getRange("h3").getValue()!='')
   
   {

console.log("Right!");
  
}
}

enter image description here

8
  • If you get an error, always post the error message. It's the key to knowing what's wrong. Commented Jan 28, 2021 at 16:35
  • updated please have a look @Thomas Commented Jan 28, 2021 at 16:36
  • You didn't pass any parameters when calling getRange() (see the red squiggly line). It requires a row and column. Commented Jan 28, 2021 at 16:40
  • How to make a code with this var range = ss.getRange(3, 35); @Thomas Commented Jan 28, 2021 at 16:48
  • @Learning there are 4 ways of requesting a range. All of them are in this page: developers.google.com/apps-script/reference/spreadsheet/… . There are examples for each of them so you can play around. Commented Jan 28, 2021 at 16:55

1 Answer 1

1

Issues:

  1. You didn't pass any parameters in the getRange() method.

  2. Even if you did the first part correctly, in your code, ss is already a range object because you already used getRange(..). Therefore you can't use getRange(..).getRange('i3') twice.

Solution:

function myFunction() {
 var ss = SpreadsheetApp.getActive().getSheetByName('DATA');
 if(ss.getRange('i3').getValue()!='' && ss.getRange("h3").getValue()!='') { 
      console.log("Right!");
     ss.getRange('ao3').setValue("Right!");
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much @Marios for the solution but how to paste console.log result in range AO3
@Learning answer updated. The value will be pasted only if it passes the if condition.
Yes @Marios well noted.

Your Answer

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