我想使用 If 语句在继续之前检查 "TRUE" 的单元格值

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

我想在继续之前使用 If 语句检查单元格值是否等于 "TRUE"。我创建了下面的内容,但有一行我无法弄清楚。我在下面有问题的脚本行上方插入了一条评论。

一些注意事项:

我对 google 脚本完全陌生。 感谢您的帮助:)

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"
}
}
}
;

除了两点,你的代码是完美的:)

  1. 你需要像这样将它包装在 onEdit(e) 函数中,e 被定义(我假设你已经这样做了,但你当前的代码没有反映)
  2. 对于复选框,TRUE 不是字符串而是布尔值(需要小写)即不需要引号

这是最后一段代码,应该可以正常工作 -

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"
    }
  }

}

删除 TRUE 值上的引号