从电子表格添加到日历时防止重复事件

时间:2016-01-25 01:58:18

标签: javascript google-calendar-api

此问题与问题here有关,但此示例不使用日历应用,但是advanced calendar services

我被困在如何防止出现重复项,因为getEventSeriesById在这里不起作用。

function onOpen() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var entries = [{
        name : "Export Events",
        functionName : "exportEvents"
    }];
    sheet.addMenu("Calendar Actions", entries);
};

function exportEvents() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calendar");
    var headerRows = 1;  
    var range = sheet.getDataRange();
    var data = range.getValues();
    var calId = 'MY_ID';
    for (i=0; i<data.length; i++) {
        if (i < headerRows) continue; 

        var row = data[i];
        var date = row[0];
        var endDate = row[1];
        var title = row[2];           
        var desc = row[3];
        var id = row[4];
        var color = row[5];

        var newEvent = {
            summary: title,
            description: desc,
            "start": {
                "date": date
            },
            "end": {
                "date": endDate
            },
            colorId: color
        };

        newEvent = Calendar.Events.insert(newEvent, calId);

        row[4] = newEvent.id

        /* //this bit does not work
        if (id === 1) {
            var delEvent = Calendar.Events.remove(calId, id);
            delEvent.execute;
        }
        */
        range.setValues(data);
    }

}

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};

function exportEvents() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calendar");
var headerRows = 1;  
var range = sheet.getDataRange();
var data = range.getValues();
var calId = 'insert cal id';
for (i=0; i<data.length; i++) {
if (i < headerRows) continue; 
var row = data[i];
var date = row[0];
var endDate = row[1];
var title = row[2];           
var desc = row[3];
var id = row[4];
var color = row[5];
var check = row[6];

var newEvent = {
summary: title,
description: desc,
"start": {
"date": date
},
"end": {
"date": endDate
},
colorId: color
};

newEvent = Calendar.Events.insert(newEvent, calId);

row[4] = newEvent.id

if (check == 1) {
var delEvent = Calendar.Events.remove(calId, id);
} else {
newEvent.execute;
}

range.setValues(data);
}

}