从Google表格发送提醒电子邮件和日历

时间:2019-02-27 20:50:14

标签: google-apps-script google-sheets

Google Sheets Data

我写了一个代码,其中脚本通过电子邮件向我发送提醒。但是日历功能不起作用。请指导我。

function checkReminder() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
var calendar = CalendarApp.getCalendarById('email@email.com');
var lastRow = sheet.getLastRow();
var startRow = 2;

var range = sheet.getRange(2,3,lastRow-startRow+1,1 );
var numRows = range.getNumRows();

var days_left_values = range.getValues();
var range = sheet.getRange(2,1,lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();
var warning_count = 0;
var msg = "";

for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left <= 3) {
  var reminder_name = reminder_info_values[i][0];

  msg = msg + "Reminder: "+reminder_name+" is due in "+days_left+" days.\n";
  warning_count++;
}
}

  if(warning_count) {
MailApp.sendEmail("email@email.com","Reminder email", msg);
calendar.createEvent(reminder_name, new Date('March 3, 2019 09:00:00 IST'), new Date('March 3, 2019 10:00:00 IST'));
}
Logger.log(msg);   
};

此外,如果在提醒日期之后我未将D2列中的状态更改为“完成”,则应发送电子邮件给我的兄弟。

1 个答案:

答案 0 :(得分:0)

这是我用来在DailyEvents日历上创建事件的代码。 eA只是一个数组。 第一个元素是事件标题,第二个是小时数,其余时间是分钟数。

function createEventWithReminders(eA)
{
  var minute=60*1000;
  var hour=60*minute;
  var cal=CalendarApp.getCalendarById(getGlobal('DailyEventsId'));
  var now=new Date();
  var startValue=now.valueOf() + (Number(eA[1]) * hour ) + (Number(eA[2]) * minute);
  var start=new Date(startValue);
  var endValue=start.valueOf() + (10 * minute);  
  var end=new Date(endValue);
  var event=cal.createEvent(eA[0], start, end);
  if(eA.length>3)
  {
    for(var i=3;i<eA.length;i++)
    {
      event.addEmailReminder(eA[i]);
    }
  }
  else
  {
    event.addEmailReminder(0);
    event.addEmailReminder(15);
    event.addEmailReminder(30);
  }
  return true;
}