我想在继续操作之前使用If语句检查单元格值是否等于“ TRUE”。我创建了以下内容,但是有一条我无法弄清楚的问题。我在下面有问题的脚本行上方插入了注释。
一些注意事项:
下面的宏在onEdit上运行。
我将其分隔为单独的if语句,因为如果单元格值不是“ TRUE”,则有一个特定的函数要运行。
从中获取“ 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"
}
}
}
;
答案 0 :(得分:4)
您的代码很完美,除了两件事:)
onEdit(e)
函数中,以定义 e (我假设您已经在执行此操作,但是您当前的代码并未反映出这一点) )这是应该按需工作的最终代码-
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"
}
}
}
答案 1 :(得分:1)
删除TRUE值上的引号