日历|事件更改 - 如何通知对非Google帐户的更改

时间:2018-02-01 14:30:32

标签: google-apps-script google-calendar-api

我在Google电子表格中运行了一个可以成功创建新事件的脚本;包括邀请拥有非Google帐户的人。

另一个脚本可以更新现有事件。它可以很好地重新安排事件,更改位置,添加/删除与会者。

但是,没有Google帐户的与会者不会收到通知/更新。

当我手动更改Google日历中的活动时,我询问是否要通知他人。如果我按“确定”,将通知所有与会者,包括非Google帐户。

如何通过脚本触发?

以下脚本正确运行。

 function sendInvite(calendarId, eventId, email) {
  var n = eventId.indexOf("@")
  var str = eventId.substring(0,n)
  var event = Calendar.Events.get(calendarId, str);
  if(event.attendees) {
    event.attendees.push({
      email: email
    });
  } else {
    event.attendees = new Array({email: email});
  }
  if(email[0].length > 0) {
    event = Calendar.Events.patch(event, calendarId, str, {
      sendNotifications: true
    });
  }
}

1 个答案:

答案 0 :(得分:0)

解决了它。有一些错误:

  • 电子邮件应该是一个数组
  • 只有在使用api
  • 执行事件的所有突变时,.patch才有效

这导致以下代码(可以更好地编写,但它可以工作):

function updateEvent2() {



  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Meetings to be Planned')
  var data = sheet.getDataRange().getValues()


  var t1 = 3
  var t2 = 3
  var formules = []
  for (var i = 3; i<data.length;i++) {
    formules[i-3]=[]
    formules[i-3][0] = '=text(b'+(i+1)+'+c'+(i+1)+';"yyyy-mm-ddThh:mm:ss")'
    formules[i-3][1] = '=text(b'+(i+1)+'+d'+(i+1)+';"yyyy-mm-ddThh:mm:ss")'
    if(data[i][0] != '') {t2++}
    }
  sheet.getRange(4,5,formules.length,2).setFormulas(formules)
  SpreadsheetApp.flush()

  var data = sheet.getDataRange().getValues()

  var event = []
  var calendarId = CalendarApp.getDefaultCalendar().getId();
  for(var i = t1; i<t2;i++) {
var eventTitle = data[i][0]
var eventStart = data[i][4]
var eventEnd = data[i][5]
var eventLocation = data[i][6]
var eventDescription = data[i][7]
var eventGuests = data[i][8]
var eventId = data[i][9]
var toBeUpdated = data[i][10]
if (toBeUpdated == 'x') {
  event[i-t1] = []

  var n = eventId.indexOf("@")
  var str = eventId.substring(0,n)
  var updateEvent = Calendar.Events.get(calendarId, str);
  var updated = false
  if(updateEvent.summary!=eventTitle) {
    updateEvent.summary = eventTitle
    updated = true
  }
  if(updateEvent.location!=eventLocation) {
    updateEvent.location = eventLocation
    updated = true
  }
  if(updateEvent.description!=eventDescription) {
    updateEvent.description=eventDescription
    updated = true
  }
  if(updateEvent.start!=eventStart) {
    updateEvent.end.dateTime= eventEnd
    updateEvent.end.timeZone= 'Europe/Amsterdam'
    Logger.log(updateEvent.end)
    updated = true
  }
  if(updateEvent.end!=eventEnd) {
    updateEvent.start.dateTime=eventStart
    updateEvent.end.timeZone= 'Europe/Amsterdam'
    Logger.log(updateEvent.start)
    updated = true
  }
  var currentGuests = updateEvent.attendees
  for each (var guest in currentGuests) {
    if(eventGuests.indexOf(guest.getEmail())==-1) {
      updateEvent.attendees[0].
      updateEvent.removeGuest(guest.getEmail());
      updated = true
    }
  }
  eventGuests = eventGuests.replace(/ /g,'')
  var allGuest = eventGuests.split(',')
  if(updateEvent.attendees) {
    updateEvent.attendees.push({
      email: allGuest
    });
  } else {
    updateEvent.attendees = new Array({email: allGuestl});
  }

  if (updated) {
    updateEvent = Calendar.Events.patch(updateEvent, calendarId, str, {sendNotifications: 'true'});
    var date = new Date()
    event[i-t1][0] = date
    sheet.getRange(i+1,11).setValue(date)
  }
}

} }