I am working on a little project for a restaurant, where I am using Chat GPT to write a code for an opening and closing task list on Google Apps Script. I have a submit button on the sheet triggers notification, my code on the “employer” side are as follow. The code works fine on the “employer side”, where I can get notif and stuff, but when I use a dummy account to be an “employee”, it states “Script function onSubmitOpening could not be found”. What’s wrong with the code?
`
function onSubmitOpening() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Opening”);
// Define the range for the "CHECK" and "INITIALS" columns
var checkColIndex = 3; // Index for the "CHECK" column (assuming it's the 5th column)
var initialsColIndex = 4; // Index for the "INITIALS" column (assuming it's the 6th column)
var itemColIndex = 1; // Index for the "ITEM" column (assuming it's the 2nd column)
// Find the cell next to "Submitted by:"
var submittedByRange = sheet.createTextFinder("Submitted by:").findNext();
if (!submittedByRange) {
SpreadsheetApp.getUi().alert("Could not find the 'Submitted by:' cell.");
return;
}
var submittedByName = sheet.getRange(submittedByRange.getRow(), submittedByRange.getColumn() + 1).getValue().toString().trim();
// Check completion and notify
checkCompletionAndNotify(sheet, itemColIndex, checkColIndex, initialsColIndex, "Opening Checklist", submittedByName);
// Protect the sheet, allowing only "CHECK" and "INITIALS" columns to be edited
protectSheet(sheet, checkColIndex, initialsColIndex);
}
function checkCompletionAndNotify(sheet, itemColIndex, checkColIndex, initialsColIndex, sheetName, submittedByName) {
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
var incompleteTasks = [];
var allTasksCompleted = true;
// Ensure that all "CHECK" and "INITIALS" columns are filled only for rows where ITEM exists
for (var i = 2; i < data.length; i++) { // Start from row 3 (index 2)
var itemValue = (data[i][itemColIndex] || "").toString().trim();
var checkValue = (data[i][checkColIndex] || "").toString().trim().toLowerCase();
var initialsValue = (data[i][initialsColIndex] || "").toString().trim();
// Skip rows where ITEM is empty
if (itemValue === "") {
Logger.log("Row " + (i + 1) + " skipped because ITEM is empty.");
continue;
}
// Log the values being checked
Logger.log("Row " + (i + 1) + " - ITEM: " + itemValue + ", CHECK: " + checkValue + ", INITIALS: " + initialsValue);
if (checkValue === "" || initialsValue === "") {
allTasksCompleted = false;
SpreadsheetApp.getUi().alert("Please complete all required fields then submit.");
return;
}
if (checkValue !== "yes") {
incompleteTasks.push(itemValue); // Use itemValue directly for logging incomplete tasks
allTasksCompleted = false;
}
}
// Send an email notification and generate a PDF based on the completion status
var emailAddress = "[email protected]";
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var emailSubject = sheetName + " Submission Notification";
var emailBody;
if (allTasksCompleted) {
emailBody = "All the items have been successfully submitted at " + year + "/" + month + "/" + day + " at " + hour + ":" + minute + " by " + submittedByName;
} else {
// Format incomplete tasks as a list, each item on a new line
var itemList = incompleteTasks.map(function(task) {
return task;
}).join("n");
emailBody = "Good day! Kindly find attached the " + sheetName + " report, submitted at " + year + "/" + month + "/" + day + " at " + hour + ":" + minute + ". The following items in the " + sheetName + " still need to be completed by " + submittedByName + ":n" + itemList;
}
// Generate the PDF
var pdfBlob = createPDF(sheet);
// Send the email with the attached PDF
MailApp.sendEmail({
to: emailAddress,
subject: emailSubject,
body: emailBody,
attachments: [pdfBlob]
});
// Clear the "CHECK" and "INITIALS" columns after submission, leaving the headings intact
clearColumns(sheet, checkColIndex, initialsColIndex);
}
function createPDF(sheet) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = sheet.getSheetId();
var url = "https://docs.google.com/spreadsheets/d/" + spreadsheet.getId() + "/export?format=pdf&size=A4&portrait=true&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=true&fzr=false&gid=" + sheetId;
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
return response.getBlob().setName(sheet.getName() + ".pdf");
}
function clearColumns(sheet, checkColIndex, initialsColIndex) {
var lastRow = sheet.getLastRow();
sheet.getRange(3, checkColIndex + 1, lastRow - 2, 2).clearContent(); // Clears "CHECK" and "INITIALS" columns from row 3 downwards
}
function protectSheet(sheet, checkColIndex, initialsColIndex) {
// Protect the entire sheet
var protection = sheet.protect().setDescription('Protect sheet except CHECK and INITIALS columns');
// Unprotect the "CHECK" and "INITIALS" columns
var unprotectedRange = sheet.getRange(3, checkColIndex + 1, sheet.getLastRow() - 2, 2);
protection.setUnprotectedRanges([unprotectedRange]);
// Set the owner of the sheet
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}`
I followed Chat GPT’s instruction to set up a trigger but it doesn’t seem to work.