我正在研究一個附加到Google Spreadsheet的腳本,該腳本可幫助我爲員工生成一個輪班計劃,創建一個Google日曆並在人員交換時更新日曆轉變。我在電子表格中附加了一個onEdit()可安裝觸發器,用於檢查用戶是否更改了特定範圍內的某個單元格(以交換換班),然後在電子表格的日曆中找到單元格,將它們交換並調用並更新GoogleCalendar。Google表格可安裝onEdit()觸發器未被授權添加日曆事件
它從腳本編輯器執行時運行完美。但是從電子表格上的觸發執行時,我得到這個錯誤:
"Execution failed: You do not have permission to call getOwnedCalendarsByName (line 204, file "Code") [1.021 seconds total runtime]"
在谷歌應用程式的權限,它說本作的劇本:「谷歌日曆 管理您的日曆」,因此它應該被授權。
這裏的OnEdit
function onEdit(e) {
var sheets = SpreadsheetApp.getActive();
var dutySwitchSheet = sheets.getSheetByName('Duty Switches');
if(!e){
var e = {};
e.range = dutySwitchSheet.getRange(30, 9);
}
Logger.log(e.range.getValue());
var changedRow = e.range.getRow();
var changedCol = e.range.getColumn();
var RAInitials = sheets.getRangeByName("dutySwitchRAInitials");
var rangeTop = RAInitials.getRow();
var rangeBottom = RAInitials.getLastRow();
var rangeLeft = RAInitials.getColumn();
var rangeRight = RAInitials.getLastColumn();
Logger.log("changedRow: %s, changedCol: %s, rangeTop: %s, rangeBottom: %s, rangeLeft: %s, rangeRight: %s", changedRow, changedCol, rangeTop, rangeBottom, rangeLeft, rangeRight);
if(changedRow >= rangeTop && changedRow <= rangeBottom && changedCol >= rangeLeft && changedCol <= rangeRight && e.range.getSheet().getName().toUpperCase() === dutySwitchSheet.getName().toUpperCase()){
Logger.log("event sheet name: %s, Duty Switch Sheet Name: %S", e.range.getSheet().getName().toUpperCase(), dutySwitchSheet.getName().toUpperCase());
var relRow = changedRow - rangeTop + 1;
Logger.log("Checking if both initials are present");
if(RAInitials.getCell(relRow, 1).getValue() && RAInitials.getCell(relRow,2).getValue()){
var ui = SpreadsheetApp.getUi()
ui.showModalDialog(HtmlService.createHtmlOutput(""), "Checking Your input, please wait..");
Logger.log("updating");
var RA1Range = sheets.getRangeByName("dutySwitchRA1");
var RA2Range = sheets.getRangeByName("dutySwitchRA2");
var calendarRange = sheets.getRangeByName("dutyDays");
var startDate = new Date(sheets.getRangeByName("startDate").getValue());
var startDay = startDate.getDay();
var RA1 = {name: RA1Range.getCell(relRow, 1).getValue(), shiftNum: RA1Range.getCell(relRow, 2).getValue(), date: new Date(RA1Range.getCell(relRow, 3).getValue()) };
if(RA1.shiftNum != "N/A"){
RA1.day = Math.floor((RA1.date.getTime() - startDate.getTime())/(24*60*60*1000)) + startDay;
RA1.SwappedCell = calendarRange.getCell(Math.floor(RA1.day/7) * 3 + Number(RA1.shiftNum) + 1, (RA1.day % 7) + 1);
RA1.cellValue = String(RA1.SwappedCell.getValue());
}
var RA2 = {name: RA2Range.getCell(relRow, 1).getValue(), shiftNum: RA2Range.getCell(relRow, 2).getValue(), date: new Date(RA2Range.getCell(relRow, 3).getValue()) };
if(RA2.shiftNum != "N/A"){
RA2.day = Math.floor((RA2.date.getTime() - startDate.getTime())/(24*60*60*1000)) + startDay;
RA2.SwappedCell = calendarRange.getCell(Math.floor(RA2.day/7) * 3 + Number(RA2.shiftNum) + 1, (RA2.day % 7) + 1);
RA2.cellValue = String(RA2.SwappedCell.getValue());
}
Logger.log("RA1 name: %s, date: %s, cellValue: %s; RA2 name: %s, date: %s, cellValue: %s" , RA1.name, RA1.date, RA1.cellValue, RA2.name, RA2.date, RA2.cellValue);
Logger.log("RA1 name matches: %s, RA2 name matches: %s", RA1.cellValue.indexOf(RA1.name) != -1, RA2.cellValue.indexOf(RA2.name) != -1);
if(RA1.cellValue.indexOf(RA1.name) != -1 || RA1.shiftNum == "N/A" && RA2.cellValue.indexOf(RA2.name) != -1 || RA2.shiftNum == "N/A"){
Logger.log("Swapping Cells");
if(RA1.shiftNum != "N/A"){
RA1.SwappedCell.setValue(String(RA1.SwappedCell.getValue()).replace(RA1.name, RA2.name));
}
if(RA2.shiftNum != "N/A"){
RA2.SwappedCell.setValue(String(RA2.SwappedCell.getValue()).replace(RA2.name, RA1.name));
}
Logger.log("Syncing Google Calendar");
syncGoogleCalendar();
ui.showModalDialog(HtmlService.createHtmlOutput(""), "The Google Calendar has been updated! :)");
Logger.log("Done Updating!");
}
else {
Logger.log("Did Not Swap Cells");
ui.showModalDialog(HtmlService.createHtmlOutput(""), "The info input does not match the spreadsheet. Couldn't update :(");
}
}
}
}
由安裝觸發調用的函數而這裏的updateGoogleCalendar()函數,直到它引發錯誤
function syncGoogleCalendar(){
var dutyCalendars = CalendarApp.getOwnedCalendarsByName("RA Duty Calendar");
謝謝!我將它改爲OnDutySwitch()。我曾嘗試過,但它沒有奏效。我擺弄它,並得知我無法通過更改當前可安裝的觸發器來實現。相反,我必須刪除舊的並創建新的 –