Home > database >  If Else statement in Apps Script
If Else statement in Apps Script

Time:08-18

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

CodePudding user response:

Logical equal is ==.

let value = null;
if{ a1 == "text1" }
  value = "text2"
}
else {
  value = "text1" }
}

Another easier way to do this is

let value = a1 == "text1" ? "text2" : "text1";

Reference

CodePudding user response:

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()

  • Related