Google Script:用于创建日历事件的脚本没有错误但无效

时间:2016-05-18 04:31:35

标签: javascript google-apps-script calendar

function onOpen() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var calendarName = "CALENDAR";
    var calTimeZone =  CalendarApp.openByName(calendarName).getTimeZone();
    var menuEntries = [ {name: "Update Calendar", functionName: "cal1"} ];
    ss.addMenu("Calendar", menuEntries);
    ss.setSpreadsheetTimeZone(calTimeZone);
}

function cal1() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var dataSheet = ss.getSheetByName("Sheet1");
    var calendarName = "CALENDAR";
    var calTimeZone =  CalendarApp.openByName(calendarName).getTimeZone();
    var startRow = 3;
    var numRows = 100;
    var dataRange = dataSheet.getRange(startRow, 1, numRows, 26);
    var data = dataRange.getValues();
    var cal = CalendarApp.getCalendarsByName("CALENDAR")[0];

    for (i in data) {
        var row = data[i];
        if (row[3] == "Add") {
            var title = row[24];  // Column with Title
            var desc = row[21];       // Column with Description
            var startdate = row[12];   //Column with Date
            var enddate = row[14];    //Column with Location
            cal.createEvent(title, startdate, enddate);  
        }
    } 
}

这是一个简单的脚本,如果第3列是"添加",它应该查看我的电子表格并将其添加到我的日历中。代码运行时没有任何错误,但是当我检查我的日历时,那里什么都没有。我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

检查记录器后,我意识到索引从0开始(列C =行[2],而不是行[3])。这是一个愚蠢的错误,尽管我想删除这个问题,但我认为把它放在那里以防万一有人做同样的事情会很好。