Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If Else statement in Apps Script

I have "Text1" in A1 and I want to say

if a1= "text1" then "text2"
else "text1"

How do I write this in Apps Script from scratch? I am lost with defining my variables so basically the beginning

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this simple implementation in App Script:

 function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //Gets the active spreadsheet. 
  var range = ss.getRange('A1'); //Reference A1 Notation see references below
  var value = range.getValue(); //Gets the cell value and assigned it to the variable
  if(value = 'Text1'){
    range.setValue('Text2') //Sets the value on the range 
  }
  else{
    range.setValue('Text1')
  }
}

This should get you started on your journey to app script.

References:

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
https://developers.google.com/apps-script/reference/spreadsheet/range?hl=en#getValue()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading