创建房间预订系统时出现“ ReferenceError:ss未定义”

问题描述

运行客房预订脚本时遇到一些问题。我正在Google Apps脚本编辑器中工作,并且已经直接从尝试使用的电子表格中打开/链接了该项目,但仍然会收到错误消息。在项目详细信息中,它在“容器”中列出了正确的电子表格。

我得到的具体错误是:

ReferenceError:未定义ss(第6行,文件代码”)

完整代码如下:

var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();

// Calendars to output appointments to
var calHY15 = Calendarapp.getCalendarById('[email protected]');
var calHY19 = Calendarapp.getCalendarById('[email protected]');
var calHY209 = Calendarapp.getCalendarById('[email protected]');
var calHY210 = Calendarapp.getCalendarById('[email protected]');
var calHY211 = Calendarapp.getCalendarById('c_mpse3jk6vsod5brkgnsll[email protected]');
var calHY212 = Calendarapp.getCalendarById('[email protected]');
var calDenison158 = Calendarapp.getCalendarById('[email protected]');
var calDenison159 = Calendarapp.getCalendarById('[email protected]');
var calDenison180 = Calendarapp.getCalendarById('[email protected]');
var calDenison182 = Calendarapp.getCalendarById('[email protected]');
var calDenison184 = Calendarapp.getCalendarById('[email protected]');
var calDenison186 = Calendarapp.getCalendarById('[email protected]');
var calPercussionStudio = Calendarapp.getCalendarById('[email protected]');

// Create an object from user submission
function Submission(){
  var row = lastRow;
  this.timestamp = sheet.getRange(row,1).getValue();
  this.name = sheet.getRange(row,3).getValue();
  this.email = sheet.getRange(row,4).getValue();
  this.reason = sheet.getRange(row,5).getValue();
  this.date = sheet.getRange(row,6).getValue();
  this.time = sheet.getRange(row,7).getValue();
  this.duration = sheet.getRange(row,8).getValue();
  this.room = sheet.getRange(row,9).getValue();
  // Info not from spreadsheet
  this.roomInt = this.room.replace(/\D+/g,'');
  this.status;
  this.dateString = (this.date.getMonth() + 1) + '/' + this.date.getDate() + '/' + this.date.getYear();
  this.timeString = this.time.toLocaleTimeString();
  this.date.setHours(this.time.getHours());
  this.date.setMinutes(this.time.getMinutes());
  this.calendar = eval('cal' + String(this.roomInt));
  return this;
}

// Use duration to create endTime variable
function getEndTime(request){
  request.endTime = new Date(request.date);
  switch (request.duration){
    case "30 minutes":
      request.endTime.setMinutes(request.date.getMinutes() + 45);
      request.endTimeString = request.endTime.toLocaleTimeString();
      break;
    case "1 hour":
      request.endTime.setMinutes(request.date.getMinutes() + 75);
      request.endTimeString = request.endTime.toLocaleTimeString();
      break;
    
  }
}

// Check for appointment conflicts
function getConflicts(request){
  var conflicts = request.calendar.getEvents(request.date,request.endTime);
  if (conflicts.length < 1) {
    request.status = "Approve";
  } else {
    request.status = "Conflict";
  }
}

function draftEmail(request){
  request.buttonLink = "https://forms.gle/Eq8qHWGVgzpahbFE9";
  request.buttonText = "New Request";
  switch (request.status) {
    case "Approve":
      request.subject = "Confirmation: " + request.room + " Reservation for " + request.dateString;
      request.header = "Confirmation";
      request.message = "Your room reservation has been scheduled.";
      break;
    case "Conflict":
      request.subject = "Conflict with " + request.room + "Reservation for " + request.dateString;
      request.header = "Conflict";
      request.message = "There is a scheduling conflict. Please pick another room or time."
      request.buttonText = "Reschedule";
      break;
  }
}

function updateCalendar(request){
  var event = request.calendar.createEvent(
    request.name,request.date,request.endTime
    )
}

function sendEmail(request){
  MailApp.sendEmail({
    to: request.email,subject: request.header,htmlBody: makeEmail(request)
  })
  sheet.getRange(lastRow,lastColumn).setValue("Sent: " + request.status);
}

// --------------- main --------------------

function main(){
  var request = new Submission();
  getEndTime(request);
  getConflicts(request);
  draftEmail(request);
  if (request.status == "Approve") updateCalendar(request);
  sendEmail(request);
}

为了节省空间,我没有在其中包含一个单独的电子邮件脚本。所有范围都已批准,我是所涉及的电子表格和日历的所有者,并且设置了触发器,以便在提交表单时运行主表单。

解决方法

要修复错误,请替换

var sheet = SpreadsheetApp.getActiveSpreadsheet();

作者

var ss = SpreadsheetApp.getActiveSpreadsheet();

注意:

Google Apps脚本编辑器使用JavaScript,而不是Java。

您的代码可能还有其他错误。如果您自己找不到上述修复程序,则可能应该花一些时间来学习变量在JavaScript中的工作方式以及如何在Google Apps脚本编辑器中调试代码。

您的代码正在使用全局范围来调用Google Apps脚本类。这可能会使代码难以调试。