问题
I have a list of dates in column A (starting at A2) paired with text for a title in column B (starting at B2). I have my calendar ID listed in cell E2.
I would like to send this data to Google Calendar to create recurring, all-day calendar events. This calendar should update when the spreadsheet is changed.
回答1:
Here is what I found for you. it's based on my understanding, may be I'm wrong. Just want to help you.
https://www.quora.com/How-do-I-automatically-add-events-to-a-Google-Calendar-from-a-Google-Sheet
or you can use app to do this task for you here is step by step guide
https://zapier.com/apps/google-calendar/tutorials/how-to-create-calendar-events-from-spreadsheet-tutorial
回答2:
I wrote this small piece of code that creates recurring events using the data in sheets.
I didn't write this in a trigger, so you would have to run this manually. It could be written in an onEdit trigger, but I don't think it would be the best idea, since you would soon end up having mountains of duplicate events, even though this could be avoided by adding some condition that checks whether an event with those characteristics already exists:
function createEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var firstRow = 2;
var firstCol = 1;
var numRows = lastRow - firstRow + 1;
var numCols = 2;
var data = sheet.getRange(firstRow, firstCol, numRows, numCols).getValues();
var calId = sheet.getRange("E2").getValue();
var cal = CalendarApp.getCalendarById(calId);
var recurrence = CalendarApp.newRecurrence().addYearlyRule();
for(var i = 0; i < data.length; i++) {
var title = data[i][1];
var date = new Date(data[i][0]);
var event = cal.createAllDayEventSeries(title, date, recurrence);
}
}
Also, if you wanted to delete previously created events when you create new events, you should keep track of all old events and edit this code a bit, but I'm not sure you want to delete them.
Update:
In case you want to create events when the sheet is edited, without having to run the function manually, I'd recommend using an onEdit trigger that creates an event corresponding to the row that has been written. Additionally, a condition can be added to create the event only if the data in the row is valid (columns A and B are not empty, and the value in column A is a valid Date).
The following function accomplishes all previous actions:
function createEvent(e) {
var sheet = e.source.getActiveSheet();
var range = e.range; // Edited range
var rowIndex = range.getRow(); // Edited row index
var firstCol = 1;
var numCols = 2;
var data = sheet.getRange(rowIndex, firstCol, 1, numCols).getValues()[0];
var title = data[1];
var date = data[0];
// Check whether column A is a valid Date and column B is not empty:
if(Object.prototype.toString.call(date) === '[object Date]' && title != "") {
var calId = sheet.getRange("E2").getValue(); // Get calendar id from cell 'E2'
var cal = CalendarApp.getCalendarById(calId);
var recurrence = CalendarApp.newRecurrence().addYearlyRule();
var event = cal.createAllDayEventSeries(title, date, recurrence); // Create event
}
}
In order to run on edit, this function needs an onEdit
trigger. This trigger has to be installed, because a simple trigger cannot access services that require authorization.
You can install this trigger manually by following these steps (check this screenshot if you have problems when configuring the type of trigger).
You can also install this trigger programmatically, as explained here.
Please let me know if that works for you now. I hope this is of any help.
来源:https://stackoverflow.com/questions/58654019/create-recurring-all-day-calendar-event-from-list-of-dates-in-google-sheet