我正在尝试通过Google Apps脚本将来宾添加到日历中,该脚本通过Google表单接收来自用户的日历请求--->如果没有冲突,则它会更新Google在线Excel工作表;如果没有冲突,则会进行保留这项工作正常,但是如果用户尝试编辑预订时间,那么他们就不能修改日历,因为他们不是日历的所有者,因此我唯一想到的解决方法是在提交的请求中添加电子邮件并添加他们作为该特定活动的邀请对象,但未成功结束。
在此处阅读有关添加位置的解决方案,但未能将用户添加为来宾。
尝试添加
var body = {
'sendNotification': true,
'attendees': attendees
};
var ret = Calendar.Events.patch(body, calendarId, eventId);
但未成功
代码如下:
// Room Reservation
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
// Calendar to output reservations to
var cal145 = CalendarApp.getCalendarById('xxxxxx@resource.calendar.google.com');
// Create use data from user submission
// This function submits data from the google form and inputs it into the "Room Reservation" sheet by the user
function Submission(){
var row = lastRow;
this.timestamp = sheet.getRange(row, 1).getValue();
this.NameReason = sheet.getRange(row, 2).getValue();
this.email = sheet.getRange(row, 3).getValue();
this.date = sheet.getRange(row, 5).getValue();
this.time = sheet.getRange(row, 6).getValue();
this.duration = sheet.getRange(row, 7).getValue();
this.room = sheet.getRange(row, 8).getValue();
// Info not from spreadsheet
this.roomInt = this.room.replace(/\D+/g, '');
this.status;
this.dateString = (this.date.getMonth() + 1) + '/' + this.date.getDate() + '/' + this.date.getYear();
this.timeString = this.time.toLocaleTimeString();
this.date.setHours(this.time.getHours());
this.date.setMinutes(this.time.getMinutes());
this.calendar = eval('cal' + String(this.roomInt));
return this;
}
// Use duration to create endTime variable
function getEndTime(request){
request.endTime = new Date(request.date);
switch (request.duration){
case "1 hour":
request.endTime.setMinutes(request.date.getMinutes() + 60);
request.endTimeString = request.endTime.toLocaleTimeString();
break;
case "2 hours":
request.endTime.setMinutes(request.date.getMinutes() + 120);
request.endTimeString = request.endTime.toLocaleTimeString();
break;
case "3 hours":
request.endTime.setMinutes(request.date.getMinutes() + 180);
request.endTimeString = request.endTime.toLocaleTimeString();
break;
}
}
// Check for appointment conflicts
function getConflicts(request){
var conflicts = request.calendar.getEvents(request.date, request.endTime);
if (conflicts.length < 1) {
request.status = "Approve";
} else {
request.status = "Conflict";
}
}
function draftEmail(request){
request.buttonLink = "xxxxxxx ";
request.buttonText = "New Request";
switch (request.status) {
case "Approve":
request.subject = "Confirmation: " + request.room + " Reservation for " + request.dateString;
request.header = "Confirmation";
request.message = "Your room reservation has been scheduled.";
break;
case "Conflict":
request.subject = "Conflict with " + request.room + "Reservation for " + request.dateString;
request.header = "Conflict";
request.message = "There is a scheduling conflict. Please pick another time."
request.buttonText = "Reschedule";
break;
}
}
function updateCalendar(request){
var event = request.calendar.createEvent(
request.NameReason,
request.date,
//request.reason,
request.endTime
)
}
function sendEmail(request){
MailApp.sendEmail({
to: request.email,
subject: request.header,
htmlBody: makeEmail(request)
})
sheet.getRange(lastRow, lastColumn).setValue("Sent: " + request.status);
}
function sendinvite(request)
{
request.addGuest(email);
}
// --------------- main --------------------
function main(){
var request = new Submission();
getEndTime(request);
getConflicts(request);
draftEmail(request);
Logger.log(request);
sendEmail(request);
if (request.status == "Approve") updateCalendar(request);
if (request.status == "Approve")
{
sendinvite(request);
}
}
最终结果应使用户可以编辑创建事件的时间,尽管他不是日历的所有者。该功能应将用户添加为客人到他请求的特定事件,以便他可以编辑时间。
此脚本当前可以正常运行,而无需添加来宾sendinvite函数。
谢谢