我在运行房间预订脚本时遇到一些问题。我正在使用 Google Apps 脚本编辑器,并且已直接从我尝试使用的电子表格打开/链接该项目,但仍然出现错误。在项目详细信息中,它列出了容器中正确的电子表格。
我收到的具体错误是:
ReferenceError:ss 未定义(第 6 行,文件“Code”)
完整代码如下:
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('[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 脚本类。这可能会使代码难以调试。
我被困住了 this.calendar = eval('cal' + String(this.roomInt));
调试结果是:
错误
ReferenceError:未定义 cal
评估
提交@Code.gs:33
主要@Code.gs:79
你能帮我检查一下,cal 是如何没有定义的吗