We are trying to create an online form using Google Forms to take requests for appointments. This is what the process should look like:
- User fills form and submits their information (this works)
- An email gets sent to our Gmail to notify us of a new incoming request (this works)
- We go into the Google spreadsheet. There are 3 sheets. One sheet called Requests. Another called Accepted. And another called Rejected (these were created)
- Last column in the Requests sheet has a dropdown menu with values of Accepted or Rejected. Depending on what we select, it will move the entire row into the appropriate sheet -- either Accepted or Rejected. (This works up until step 5 below)
- If the row gets moved into Accepted, it should create a new calendar event. This is the part that doesn't work. The row gets moved properly into the Accepted sheet but doesn't create the new Google calendar event.
The weird thing is if we run the function manually, it works. It takes the last row and creates an event. If we try to run the function directly via our code by calling the function directly, it doesn't create an event. This is our code for the moverow.gs
(please note that all confidential, identifying information has been removed such as the calendar id's):
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);
};
Please help!! We are at our wits end. Thanks in advance.
A simple
onEdit()
trigger cannot do anything that requires authorization. It doesn't matter who owns the document the script is in; if it's a simple trigger, then it's anonymous.Instead, use an installable trigger. (You can simply rename your function, then go through the instructions in Managing Triggers Manually to set it up.)