-1
我已經創建了一個谷歌表單,它轉到谷歌工作表從中自動創建日曆條目。不過,我無法弄清楚如何邀請來自我的工作表的訪客。我已經徹底搜索了它,發現了一些類似的解決方案here和here,但他們的代碼與我的完全不同,所以我不知道如何將其應用於我的代碼。谷歌腳本 - 添加客人從Google工作表中的日曆事件
這是我最新的嘗試:
//insert your google calendar ID
var calendarId = "CALENDARID";
//index (starting from 1) of each column in the sheet
var titleIndex = 4;
var descriptionIndex = 7;
var startDateIndex = 5;
var endDateIndex = 6;
var googleCalendarIndex = 10;
var locationIndex = 8;
var mainGuestIndex = 2;
/*
find the row where the Google Calendar Event ID is blank or null
The data of this row will be used to create a new calendar event
*/
function findRow(sheet)
{
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var loopCounter = 0;
loopCounter < values.length;
loopCounter++)
{
if (values[ loopCounter ][ googleCalendarIndex - 1 ] == "" ||
values[ loopCounter ][ googleCalendarIndex - 1 ] == null)
newEvent(i + 1);
}
};
/*
get the data of the new row by calling getSheetData() and
create a new Calendar event by calling submitToGoogleCalendar()
*/
function newEvent(row)
{
var sheet = SpreadsheetApp.getActiveSheet();
var eventId = submitToGoogleCalendar(getSheetData(sheet,row),null)
if (eventId != null)
sheet.getRange(row,
googleCalendarIndex,
1,
1).setValue(eventId);
};
/*
Store the data of a row in an Array
*/
function getSheetData(sheet,row)
{
var data = new Array();
data.title = sheet.getRange(row,
titleIndex,
1,
1).getValue();
data.description = sheet.getRange(row,
descriptionIndex,
1,
1).getValue();
data.startDate = sheet.getRange(row,
startDateIndex,
1,
1).getValue();
data.endDate = sheet.getRange(row,
endDateIndex,
1,
1).getValue();
data.location = sheet.getRange(row,
locationIndex,
1,
1).getValue();
data.mainGuest = sheet.getRange(row,
mainGuestIndex,
1,
1).getValue();
return data;
};
/*
if a cell is edited in the sheet, get all the data of the corresponding row and
create a new calendar event (after deleting the old event) by calling submitToGoogleCalendar()
*/
function dataChanged(event)
{
var sheet = SpreadsheetApp.getActiveSheet();
var row = event.range.getRow();
var eventId = sheet.getRange(row,
googleCalendarIndex,
1,
1).getValue();
var eventId = submitToGoogleCalendar(getSheetData(sheet,
row),
eventId);
if (eventId != null)
sheet.getRange(row,
googleCalendarIndex,
1,
1).setValue(eventId);
};
/*
This function creates an event in the Google Calendar and returns the calendar event ID
which is stored in the last column of the sheet
*/
function submitToGoogleCalendar(sheetData,
eventId)
{
// some simple validations ;-)
if (sheetData.title == "" ||
sheetData.startDate == "" ||
sheetData.startDate == null)
return null;
var cal = CalendarApp.getCalendarById(calendarId);
var start = new Date(sheetData.startDate);
var end = new Date(sheetData.endDate);
// some simple date validations
if (start > end)
return null;
var event = null;
//if eventId is null (when called by newEvent()) create a new calendar event
if (eventId == null)
{
event = cal.createEvent(sheetData.title,
start,
end,
{
description : sheetData.description,
location : sheetData.location,
}).addGuest(sheetData.mainGuest);
return event.getId();
}
/*
else if the eventid is not null (when called by dataChanged()), delete the calendar event
and create a new event with the modified data by calling this function again
*/
else
{
event = cal.getEventSeriesById(eventId);
event.deleteEventSeries();
return submitToGoogleCalendar(sheetData,
null);
}
return event.getId();
};
然而,當我運行這個它使一個事件,但仍然沒有客人在日曆事件出現。從here我知道我需要使用.addGuest(email)
,它似乎應該在我的submitToGoogleCalendar
函數中,因爲這是事件中所有其他信息的地方,但目前還沒有運氣。我也嘗試添加event.addGuest(sheetData.mainGuest);
,而不是將其添加到事件變量的事件中,但這也不起作用。
如何在不完全重寫我的腳本的情況下添加它?我在正確的軌道上嗎?
嘗試使用['addGuest(電子郵件)'](https://developers.google.com/apps-script/reference/calendar/calendar-事件#addGuest(String))參數和一個有效的電子郵件地址。例如。 'addGuest([email protected])'並檢查它是否有效。因爲這是通過使用Apps腳本添加訪客的唯一方式。只需查看此[鏈接](https://developers.google.com/apps-script/reference/calendar/)瞭解如何在Apps腳本中使用日曆。此外,請查看[SO問題](http://stackoverflow.com/questions/40842714),瞭解用戶如何將客人添加到他的代碼中。 – KENdi