Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

I want to use an If statement that checks the cell value for "TRUE" before proceeding


I want to use the If statement to check that the cells value is equal to "TRUE" before proceeding. I have created the below but there is one problematic line I cannot figure out. I have inserted a comment above the problematic line of script below.

Some notes:

  • The below macro runs onEdit.

  • I have separated it out into a separate if statement because there is a specific function I want to run if the cell value is not "TRUE".

  • The cell that the "TRUE" value is taken from is a Checkbox. Not sure if this is important.

I am completely new to google script. Thank you for your help :)

function onEdit(e) {

//This If statement is to ensure my macro only runs when a particular cell is edited:

if(
e.source.getSheetName() == "Daily Data" &&
e.range.columnStart == 3 &&
e.range.columnEnd == 3 &&
e.range.rowStart >= 3 &&
e.range.rowEnd <= 52 
){ 


var checkboxtest = e.range.getValue()

/*
*
*    THIS NEXT LINE IS WHERE MY ISSUE LIES
*
*/

if(checkboxtest == "TRUE"){


//This is the main section of my macro that works when using a different line above:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var daily_data = ss.getSheetByName("Daily Data");
var date_cellRow = e.range.rowStart      
daily_data.getRange(date_cellRow,4).setFormula("=NOW()").activate();      
SpreadsheetApp.flush();     
daily_data.getRange(date_cellRow,4).copyTo(daily_data.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)      
}
else 
{
//Here I will put some script to run when the cell value is not equal to "TRUE"
}
}
}
;

Solution

  • Your code is perfect except for 2 things :)

    1. You need to wrap this within a onEdit(e) function as that way, e gets defined (i'm assuming you're already doing that but your current code doesn't reflect that)
    2. In the case of a checkbox, TRUE is not a string but a boolean value (need to be in lowercase) i.e. it doesn't require quotes

    This is final piece of code that should work as desired -

    function onEdit(e) { // wrapping it within an onEdit(e) function
      //This If statement is to ensure my macro only runs when a particular cell is edited:
    
      if(
        e.source.getSheetName() == "Daily Data" &&
        e.range.columnStart == 3 &&
        e.range.columnEnd == 3 &&
        e.range.rowStart >= 3 &&
        e.range.rowEnd <= 52 
      ){ 
    
    
        var checkboxtest = e.range.getValue()
    
        /*
        *
        *    THIS NEXT LINE IS WHERE MY ISSUE LIES
        *
        */
    
        if(checkboxtest == true){ // replace "TRUE" with true
    
    
          //This is the main section of my macro that works when using a different line above:
    
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var daily_data = ss.getSheetByName("Daily Data");
          var date_cellRow = e.range.rowStart      
          daily_data.getRange(date_cellRow,4).setFormula("=NOW()").activate();      
          SpreadsheetApp.flush();     
          daily_data.getRange(date_cellRow,4).copyTo(daily_data.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)      
        }
        else 
        {
          //Here I will put some script to run when the cell value is not equal to "TRUE"
        }
      }
    
    }