當我運行代碼時,它向我詢問授權/許可。我給了必要的權限並再次運行代碼。這一次,我得到了用戶界面。但是,當我按下提交按鈕我得到一個錯誤信息說,允許有必要執行此操作:Google Apps腳本:安排約會時的授權錯誤
我怎樣才能解決呢?這段代碼(根據original google developers site在7月23日更新)過時了嗎?
function scheduleAppointment() {
var app = UiApp.createApplication().setTitle('Schedule an Appointment');
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
// Create a grid with 5 text boxes and corresponding labels
var grid = app.createGrid(5, 2);
var textApptDate = app.createTextBox();
// Text entered in the text box is passed in to apptDate
textApptDate.setName('apptDate');
var day = new Date();
day.setDate(day.getDate()+1);
textApptDate.setText(Utilities.formatDate(day, "PDT", "MM/dd/yyyy"));
grid.setWidget(0, 0, app.createLabel('Appointment Date:'));
grid.setWidget(0, 1, textApptDate);
var textStartTime = app.createTextBox();
// Text entered in the text box is passed in to startTime
textStartTime.setName('startTime');
textStartTime.setText('09:00 PDT');
grid.setWidget(1, 0, app.createLabel('Work Day Start Time:'));
grid.setWidget(1, 1, textStartTime);
var textEndTime = app.createTextBox();
// text entered in the text box is passed in to endTime.
textEndTime.setName('endTime');
textEndTime.setText('17:00 PDT');
grid.setWidget(2, 0, app.createLabel('Work Day End Time:'));
grid.setWidget(2, 1, textEndTime);
var textUser = app.createTextBox();
// Text entered in the text box is passed in to userEmail
textUser.setName('userEmail');
textUser.setText(sheet.getRange(row, getColIndexByName("Contact email")).getValue());
grid.setWidget(3, 0, app.createLabel('User\'s Email'));
grid.setWidget(3, 1, textUser);
// Create a hidden text box for storing the selected row number in the sheet
var rowValue = app.createTextBox();
rowValue.setName('rNum');
rowValue.setText(row.toString());
rowValue.setVisible(false);
grid.setWidget(4, 0, rowValue);
// Create a vertical panel..
var panel = app.createVerticalPanel();
// ...and add the grid to the panel
panel.add(grid);
// Create a button and click handler; pass in the grid object as a callback element and the handler as a click handler
// Identify the function schedule as the server click handler
var button = app.createButton('Schedule Appointment');
var handler = app.createServerClickHandler('schedule');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the panel and the panel to the application, then display the application app in the spreadsheet
panel.add(button);
app.add(panel);
doc.show(app);
}
// function that schedules the appointments and updates the spreadsheet
function schedule(e) {
var apptDate = e.parameter.apptDate;
var userEmail = e.parameter.userEmail;
var sheet = SpreadsheetApp.getActiveSheet();
var userCalendar = CalendarApp.getCalendarById(userEmail);
var helpDeskCalendar = CalendarApp.getDefaultCalendar();
// Find the first available 30 minute timeslot on the selected day
var workDayStartTime = e.parameter.startTime;
var workDayEndTime = e.parameter.endTime;
var startTime = new Date(apptDate + " " + workDayStartTime);
var endTime = new Date(startTime.getTime() + 30 * 60 * 1000);
var row = e.parameter.rNum;
while (endTime.getTime() < new Date(apptDate + " " + workDayEndTime).getTime()) {
var numUserEvents = userCalendar.getEvents(startTime, endTime).length;
var numHelpDeskEvents = helpDeskCalendar.getEvents(startTime, endTime).length;
if (numUserEvents == 0 && numHelpDeskEvents == 0) {
CalendarApp.createEvent("Help Desk appointment", startTime, endTime,
{description: "Help Desk Ticket #" + row,
guests: userEmail});
// Update Notes and Status
sheet.getRange(row, getColIndexByName("Notes")).setValue("Appointment scheduled.");
sheet.getRange(row, getColIndexByName("Status")).setValue("In Progress");
// Clean up - get the UiApp object, close it, and return
var app = UiApp.getActiveApplication();
app.close();
return app;
}
// Add 30 minutes to start and end times
startTime = endTime;
endTime = new Date(startTime.getTime() + 30 * 60 * 1000);
}
Browser.msgBox("There are no times available on " + apptDate + ". Please try another date.");
// Clean up - get the UiApp object, close it, and return
var app = UiApp.getActiveApplication();
app.close();
return app;
}
你有沒有解決這個問題? –
還是不是,Serge!我今天會處理它。 Serge,你有一個GAS網站或教程嗎? – craftApprentice
這仍然是一個項目...你有沒有試過撤銷授權並重新授權? –