我们正在尝试使用Google表单创建一个在线表单,以接收约会请求。这就是过程应该是这样的:
奇怪的是,如果我们手动运行该功能,它就可以工作。它占据最后一行并创建一个事件。如果我们尝试通过直接调用函数直接通过我们的代码运行函数,它不会创建一个事件。这是我们的moverow.gs代码
(请注意,所有机密的识别信息已被删除,例如日历ID):
function onEdit(e) {
try { moveRow(e) } catch (error) { Browser.msgBox(error) }
}
function moveRow(e) {
var rowStart = e.range.rowStart;
var colStart = e.range.columnStart;
var calendarStaff1Id = "<link to calendar>";
var calendarStaff2Id = "<link to calendar>"
var calendarStaff3Id = "<link to calendar>"
var calendarStaff4Id = "<link to calendar>"
if ( rowStart == e.range.rowEnd && colStart == e.range.columnEnd ) {
var active = e.source.getActiveSheet();
var name = active.getName();
if ( name == "Requests" && colStart == 10 ) {
var value = e.value;
if ( value == "ACCEPTED (S1)" ) {
var rowValues = active.getRange( rowStart + ':' + rowStart ).getValues();
e.source.getSheetByName("Accepted").appendRow(rowValues[0])
active.deleteRow(rowStart);
var calendarId = calendarStaff1Id;
addEvent();
}
if ( value == "ACCEPTED (S2)" ) {
var rowValues = active.getRange( rowStart + ':' + rowStart ).getValues();
e.source.getSheetByName("Accepted").appendRow(rowValues[0])
active.deleteRow(rowStart);
var id = calendarStaff2Id;
addEvent();
}
if ( value == "ACCEPTED (S3)" ) {
var rowValues = active.getRange( rowStart + ':' + rowStart ).getValues();
e.source.getSheetByName("Accepted").appendRow(rowValues[0])
active.deleteRow(rowStart);
var calendarId = calendarStaff3Id;
addEvent();
}
if ( value == "ACCEPTED (S4)" ) {
var rowValues = active.getRange( rowStart + ':' + rowStart ).getValues();
e.source.getSheetByName("Accepted").appendRow(rowValues[0])
active.deleteRow(rowStart);
var calendarId = calendarStaff4Id;
addEvent(calendarId);
}
if ( value == "REJECTED" ) { // Change to the value that will trigger the move
var rowValues = active.getRange( rowStart + ':' + rowStart ).getValues(); // Entire row
e.source.getSheetByName("Rejected").appendRow(rowValues[0]) // Change to your "move to" sheet name
active.deleteRow(rowStart);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Rejected").activate();
Browser.msgBox("Please contact client to let them know that their request cannot be fulfilled");
}
}
}
}
function addEvent(calendarId) {
var startDtId = 7;
var titleId = 2;
var titleId2 = 6;
var descId = 8;
var formTimeStampId = 1;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Accepted").activate();
// Switch to sheet Accepted and start the calendar event creation
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Accepted");
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var lr = rows.getLastRow();
var startDt = sheet.getRange(lr,startDtId,1,1).getValue();
var subOn = "Added :"+sheet.getRange(lr,formTimeStampId,1,1).getValue()+" by: "+sheet.getRange(lr,titleId,1,1).getValue();
var desc = "Comments :"+sheet.getRange(lr,8,1,1);
var title = sheet.getRange(lr,titleId,1,1).getValue()+" - "+sheet.getRange(lr,titleId2,1,1).getValue();
var start = new Date(startDt);
var end = new Date(startDt.valueOf()+60*60*1000);
var loc = 'Central Library';
var cal = CalendarApp.getCalendarById(calendarId);
//Browser.msgBox("Please add the following event into your calendar: " + title + " Start Time: " + start + " End Time: " + end);
var event = cal.createEvent(title, start, end);
};
请帮忙!!我们的斗智斗勇。提前谢谢。
答案 0 :(得分:2)
简单的onEdit()
触发器无法执行任何需要授权的操作。谁拥有脚本所在的文档并不重要;如果它是一个简单的触发器,那么它就是匿名的。
相反,请使用installable trigger。 (您可以简单地重命名您的功能,然后按照Managing Triggers Manually中的说明进行设置。)