安排约会时的Google Apps脚本:授权错误

时间:2013-11-15 21:45:16

标签: google-apps-script google-calendar-api

当我运行下面的代码时,它会询问我的授权/权限。我提供必要的权限并再次运行代码。这一次,我得到了UI。但是当我按下提交按钮时,我收到一条错误消息,说明执行此操作需要权限:

enter image description here

我该如何解决?此代码(根据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;
}

2 个答案:

答案 0 :(得分:0)

也可以从调试器运行schedule函数。气体可能无法检测到另一个的所有范围。 如果它仍然无法工作写一个函数,调用它调度它与良好的参数,以便它实际上从调试器创建一个事件。

答案 1 :(得分:0)

您可以尝试撤销脚本授权并重新授权,在初始授权过程中可能出错。

有关如何撤销所有授权的说明,请参阅显示多种方式的this other post