我试图通过Google电子表格设置年度重复活动,虽然我已经关注this answer,但仍然无法获取ID以写入每一行,因此允许我写和检查条目。
列布局为
RenDate | Domain | Client | Type | Registrar | ID
代码:
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the exportEvents() function.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "[id removed]@group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[0]); // First column - Renewal Date
var domain = row[1]; // Second column - Domain
var client = row[2]; // Third column - Client
var type = row[3]; // Fourth column - Type
var source = row[4]; // Fifth column - Registrar
var id = row[5]; // Sixth column - ID
if(row[1]=="") continue; // Skip rows if no date exists
var title = type + " renewal for " + domain;
var desc = "Renew with " + source + " for " + client;
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you'); // this is to "force error" if the event does not exist
}catch (e) {
var newEvent = cal.createEvent(title, date, {description:desc});
row[5] = newEvent.getId(); // Update the data array with event ID
Logger.log('event created'); // For debugging
var event = cal.getEventSeriesById(row[5]);
}
event.setTitle(title);
event.setDescription(desc);
var recurrence = CalendarApp.newRecurrence().addYearlyRule();
event.setRecurrence(recurrence, date);
debugger;
data[i] = row ;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
调试似乎很好,但没有创建任何事件。我能够使用CreateAllDayEventSeries
函数创建事件,但似乎无法使用ID方法,因此我尝试使用此方法。
编辑:进行更改以继续执行Serge insas捕获的功能。
现在脚本没有在createEvent
命令上找到catch
的方法。
答案 0 :(得分:1)
我发现你犯了一些错误(最小),导致它无法正常工作。
一个细节:您尝试使用单个日期创建活动,这仅适用于allDay活动,所以我想这就是您想要做的事情。
下面的是完整的工作代码:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var data = range.getValues();
var headers = data.shift();
Logger.log(data);
var calId = "h22nevo15tm0nojb6ul4hu7ft8@group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
var row = data[i];
Logger.log(row);
var date = new Date(row[0]); // First column - Renewal Date
var domain = row[1]; // Second column - Domain
var client = row[2]; // Third column - Client
var type = row[3]; // Fourth column - Type
var source = row[4]; // Fifth column - Registrar
var id = row[5]; // Sixth column - ID
if(row[0]=="") continue; // Skip rows if no date exists
var title = type + " renewal for " + domain;
var desc = "Renew with " + source + " for " + client;
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you'); // this is to "force error" if the event does not exist
}catch (e) {
var newEvent = cal.createAllDayEvent(title, date, {description:desc});
row[5] = newEvent.getId(); // Update the data array with event ID
Logger.log('event created'); // For debugging
var event = cal.getEventSeriesById(row[5]);
}
event.setTitle(title);
event.setDescription(desc);
var recurrence = CalendarApp.newRecurrence().addYearlyRule();
event.setRecurrence(recurrence, date);
debugger;
data[i] = row ;
}
// Record all event IDs to spreadsheet
sheet.getRange(2,1,data.length,data[0].length).setValues(data);
}
答案 1 :(得分:0)
我能够自己解决这个问题(尽管最初再次感谢Serge提供了一些出色的帮助和一个可靠的答案,即使我在修复问题之前没有看到它)。这是我最终解决的代码:
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the exportEvents() function.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "[id withheld]@group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[0]); // First column - Renewal Date
var domain = row[1]; // Second column - Domain
var client = row[2]; // Third column - Client
var type = row[3]; // Fourth column - Type
var source = row[4]; // Fifth column - Registrar
var id = row[5]; // Sixth column - ID
if(row[0]=="") continue; // Check to see if date exists
var title = type + " renewal for " + domain;
var desc = "Renew with " + source + " for " + client;
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
}catch(e){
//Do nothing
}
if(!event){
var newEvent = cal.createAllDayEventSeries(title, date, CalendarApp.newRecurrence().addYearlyRule(), {description:desc});
row[5] = newEvent.getId(); // Update the data array with event ID
Logger.log('Event ID ' + row[5] + ' created'); // For debugging
}
else{
var event = cal.getEventSeriesById(row[5]);
event.setTitle(title);
event.setDescription(desc);
Logger.log('Event ID ' + row[5] + ' already exists'); //For debugging
}
debugger;
data[i] = row ;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}