问题
I have this spreadsheet Example Sheet
As people sign in an automatic time stamp goes into column A, column L then calculates a date/time 12 hours in the future.
What I would like to do is have the sheet generate an automated e-mail to the address in column K at the designated time in Column L -ONLY IF- the person hasn't entered a time in the "Time Signed Out" of Column I.
Basically, people are good at signing in, but ALWAYS forget to sign out. I'm hoping to automate a reminder e-mail for them to go back in to the sheet and put a sign out time. If not they will get an e-mail reminder.
The e-mail would have a standard subject line of: "Please Remember to Sign Out" The e-mail would have a standard body of: "It appears you may not have signed out, please click this link to sign out now"
Can I use the time or minute trigger to make the script occur even when the sheet isn't open?
I'm okay at writing very A-B-C scripts, but this one is beyond me and the examples I am getting when I search don't cover this scenario.
Thanks in advance for any help.
回答1:
Yet Another Email Question
This is totally untested but written to your specification. I hope it works okay for you. You will need to run setup my trigger and the script itself to authorize it. You should also create a column that is normally empty until you send an email and then you fill it with something. Put something in the if statement like && vA[i][column number - 1]
and after the sendEmail put in something like. sh.getRange(i+1,column number).setValue('Dont send again');
and then the next time you run the script it won't send emails to people who already received them.
function sendEmailToThoseWhoHaveNotSignedOut()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var rg=sh.getDataRange();
var vA=rg.getValues();
var lastHeaderRow=1;
var now=new Date();
var rdq=MailApp.getRemainingDailyQuota();
for(var i=lastHeaderRow;i<vA.length;i++)
{
if(new Date(vA[i][11]).valueOf()<now.valueOf() && !vA[i][8] && vA[i][10] && rdq>0)
{
MailApp.sendEmail(vA[i][10], 'Please Remember to Sign Out', 'It appears you may not have signed out, please click this link to sign out now.')
}
}
}
function setUpMyTrigger()
{
if(!isTrigger('sendEmailToThoseWhoHaveNotSignedOut'))//This prevents you from setting up more than one trigger at a time for the same function.
{
ScriptApp.newTrigger('sendEmailToThoseWhoHaveNotSignedOut').timeBased().everyHours(1).create();
}
}
function isTrigger(funcName)
{
var r=false;
if(funcName)
{
var allTriggers=ScriptApp.getProjectTriggers();
var allHandlers=[];
for(var i=0;i<allTriggers.length;i++)
{
allHandlers.push(allTriggers[i].getHandlerFunction());
}
if(allHandlers.indexOf(funcName)>-1)
{
r=true;
}
}
return r;
}
来源:https://stackoverflow.com/questions/46085574/automatically-generate-an-e-mail-in-a-google-sheet-based-of-a-date-time-in-a-cel