将 sheet 事件的日期和时间加入日历

Join Date and time for events from sheet to calendar

使用此代码,我可以将 google 工作表中的事件直接发送到 google 日历。开始日期和结束日期在两个不同的列(col D 和 col F)上,问题是我还需要其他两列(col E 和 col G)上的时间。 我如何加入带有日期+时间的开始日期和带有日期+时间的结束日期?

function sendToCalendar() {
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('XCALENDAR')
  var calendarID = spreadsheet.getRange("O1").getValue();
  var eventCal = CalendarApp.getCalendarById(calendarID);
  var signups = spreadsheet.getRange("A5:P5").getValues(); 
  
  for (x=0; x<signups.length;x++)
  {
    var shift = signups[x];
    var startTime = shift[3];
    var endTime = shift[5];
    var nameevent= shift[1];
    var desc= shift[13];
    var color = shift[15];
    var event = eventCal.createEvent(nameevent, startTime, endTime,{description:desc});

    if(color){
      event.setColor(CalendarApp.EventColor[color]);
    }
  }
}

当前日期和时间

function currentdatetime() {
  const second = 1000;
  const minute = 60 * second;
  const hour = minute * 60;
  const day = hour * 24;
  const dt = new Date();
  const dtv = dt.valueOf();
  const td = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate())
  const dv = td.valueOf();
  const d = dtv - dv;
  let hours = Math.floor(d % day / hour);
  let minutes = Math.floor(d % day % hour / minute);
  let seconds = Math.floor(d % day % hour % minute / second);
  Logger.log(`Date: ${td.getMonth() + 1}/${td.getDate()}/${td.getFullYear()} - Time:${hours}:${minutes}:${seconds}`)
}

Execution log
9:05:08 AM  Notice  Execution started
9:05:09 AM  Info    Date: 5/4/2022 - Time:9:5:9
9:05:09 AM  Notice  Execution completed

更简单的方法

function currentdatetime() {
  const dt = new Date();
  Logger.log(`Date: ${dt.getMonth() + 1}/${dt.getDate()}/${dt.getFullYear()} - Time:${dt.getHours()}:${dt.getMinutes()}:${dt.getSeconds()}`)
}

Execution log
9:11:19 AM  Notice  Execution started
9:11:20 AM  Info    Date: 5/4/2022 - Time:9:11:20
9:11:20 AM  Notice  Execution completed

超级简单的方法

function currentdatetime() {
  Logger.log(Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy - HH:mm:ss"))
}

Execution log
9:16:22 AM  Notice  Execution started
9:16:23 AM  Info    05/04/2022 - 09:16:23
9:16:23 AM  Notice  Execution completed

加入日期和时间

function currentdatetime(M=5,d=4,y=2022,H=9,m=28,s=0) {
  const dt = new Date(y,M-1,d,H,m,s);
  Logger.log(Utilities.formatDate(dt,Session.getScriptTimeZone(),"MM/dd/yyyy HH:mm:ss"));
  return dt;
}

Execution log
9:29:38 AM  Notice  Execution started
9:29:39 AM  Info    05/04/2022 09:28:00
9:29:39 AM  Notice  Execution completed

要连接日期 (d) 和时间 (t),您可以这样做

new Date(d.getFullYear(), d.getMonth(), d.getDate(), t.getHours(), t.getMinutes(), t.getSeconds())

试试

function createEvent() { 
  // A = title, B = description, C = location, D = begin .. E = at, F = end ... G = at
  const myCalend = CalendarApp.getCalendarById("xxxxxxxxxxxx@gmail.com");
  const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  const [headers, ...data] = sh.getRange('A1:H' + sh.getLastRow()).getValues()
  const colID = 8; // H
  data.forEach((r, index) => {
    if (r[colID - 1] == '') {
      let [title, desc, loc] = [r[0], r[1], r[2]]
      let [bd, bh, ed, eh] = [r[3], r[4], r[5], r[6]]
      let id = (myCalend.createEvent(
        title,
        new Date(bd.getFullYear(), bd.getMonth(), bd.getDate(), bh.getHours(), bh.getMinutes(), bh.getSeconds()),
        new Date(ed.getFullYear(), ed.getMonth(), ed.getDate(), eh.getHours(), eh.getMinutes(), eh.getSeconds()),
        {
          description: desc,
          location: loc
        }).getId())
      sh.getRange(index + 2, colID).setValue(id)
    }
  })
}

编辑

根据您的电子表格,假设日期为 dd/MM/yyyy,您可以使用(eventID 将存储在 M=13 列中并防止重复事件)

function sendToCal() {
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Foglio1')
  let eventCal = CalendarApp.getCalendarById("xxxxxxxxx@gmail.com");
  var signups = spreadsheet.getRange("A2:M" + spreadsheet.getLastRow()).getDisplayValues();
  var col = 13; // M
  for (x = 0; x < signups.length; x++) {
    var shift = signups[x];
    if (shift[(col - 1)] == '') {
      let [d, e, f, g] = [shift[3].split("/"), shift[4].split(":"), shift[5].split("/"), shift[6].split(":")]
      let [nameevent, desc, color] = [shift[1], shift[11], shift[12]]
      var startTime = new Date(parseInt(d[2]), parseInt(d[1])-1, parseInt(d[0]), parseInt(e[0]), parseInt(e[1]), 0)
      var endTime = new Date(parseInt(f[2]), parseInt(f[1])-1, parseInt(f[0]), parseInt(g[0]), parseInt(g[1]), 0)
      var event = eventCal.createEvent(nameevent, startTime, endTime, { description: desc });
      spreadsheet.getRange(+x + 2, col).setValue(event.getId())
      if (color) {
        event.setColor(CalendarApp.EventColor[color]);
      }
    }
  }
}