function onEdit(evt)
{
var sheet = evt.source.getActiveSheet();
var range = evt.range;
var column = evt.range.getColumn();
var row = evt.range.getRow();
var sheetName = sheet.getName();
if(range.getRow() >= 3 && range.getColumn() >= 2 && range.getRow() <= 15 && range.getColumn() <= 2 )
{
if(sheetName == "Emails")
{
Logger.log(evt.value);//current value
//Logger.log(evt.oldValue);//old value
}
}
}
我不想将上述代码用于旧值,还有其他方法可以实现这一目标吗?
答案 0 :(得分:0)
根据您评论中的解释
您好ziganotschka,我只是想为电子邮件创建逻辑 通过比较旧值和新值来进行通知。我正在尝试创建一个 功能,如果在2天内未在一定范围内进行修改,请发送 通知电子邮件。你能帮我解释一下逻辑吗?
Select event source
设置为Time-driven
和type
和interval
,例如Hour timer
,Every hour
示例:
function checkUpdateStatus()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails");
//Selects a range of 13 rows and one column starting with the third row, second column - adapt to your needs if necessary
var range = sheet.getRange(3,2,13,1);
var values=range.getValues();
var now=new Date().getTime();
for(var i=0;i<values.length;i++){
for(var j=0;j<values[0].length;j++){
//the following lines will set-up script properties the first time you use them:
if(PropertiesService.getScriptProperties().getKeys().length==0){
PropertiesService.getScriptProperties().setProperty('values '+i+"-"+j,values[i][j]);
PropertiesService.getScriptProperties().setProperty('timestamp '+i+"-"+j,now);
//if script properties have been set-up already:
}else{
//use parseInt() if your values are integers, otherwise ommit it
var currentValue=parseInt(PropertiesService.getScriptProperties().getProperty('values '+i+"-"+j));
//if the value is still the same - check how long it has been the same
if(currentValue==values[i][j]){
var timeDifference=now-(PropertiesService.getScriptProperties().getProperty('timestamp '+i+"-"+j));
var days=timeDifference/1000/3600/24;
if(days>2){
//send email
}
//if the value has been changed, update the script properties with the new value and timestamp
}else{
PropertiesService.getScriptProperties().setProperty('values '+i+"-"+j,values[i][j]);
PropertiesService.getScriptProperties().setProperty('timestamp '+i+"-"+j,now);
}
}
}
}
}