问题
I'm attempting to set up yearly recurring events from a Google spreadsheet, and although I've followed this answer, I still can't get the ID to write to each row and thus allow me to write and check entries.
The column layout is
RenDate | Domain | Client | Type | Registrar | ID
Code:
/**
* 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);
}
Debugging seems to be fine, but no events are created. I was able to create events using the CreateAllDayEventSeries
function, but that seems to be impossible to work out with the ID approach, so that's why I'm trying this method.
Edit: Made change to continue function as caught by Serge insas.
Now the script is not finding the method for createEvent
on the catch
command.
回答1:
I found that you were making a few errors (minimal) that prevented it to work.
One detail : you tried to create events with a single date, this works only for allDay Events so I guess that's what you wanted to do.
below is the full working code :
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);
}
回答2:
I was able to resolve the issue on my own (although thanks again to Serge for some excellent assistance initially and a solid answer, even though I didn't see it before I fixed the issue). Here's the code I eventually settled with:
/**
* 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);
}
来源:https://stackoverflow.com/questions/25895337/creating-recurring-google-calendar-events-from-google-sheet