Google Apps Script Error: ‘Script function onSubmitOpening could not be found’ Despite Correct Setup

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?

Error Message
Excel structure

`
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.

trigger set up

On submission

New contributor

Jenny Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật