Implementing a Two-Step Approval Email Notification System in Google Sheets

Implementing a Two-Step Approval Email Notification System in Google Sheets
Google Sheets

Automating Approval Notifications in Spreadsheet Workflows

In today's fast-paced business environments, the efficiency of approval processes can significantly impact operational workflows. Many organizations rely on Google Sheets to manage tasks such as approval requests due to its flexibility and accessibility. A common challenge arises when implementing an automated system for these processes, especially when it involves a two-step approval mechanism. This system necessitates sending an automated email to the IT department once both preliminary and final approvals are granted, under the condition that the request status transitions to "approved."

However, automating this process via Google Apps Script presents a peculiar challenge. The built-in "onEdit" trigger, crucial for initiating the email dispatch, does not activate for changes made programmatically—only for those made through direct user interaction. This limitation poses a significant hurdle in cases where the status update from "pending" to "approved" is performed by a script. This introduction lays the groundwork for exploring solutions to seamlessly integrate automated email notifications within a Google Sheets-based approval workflow, ensuring timely communication and process efficiency.

Command Description
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Approvals") Accesses the active spreadsheet and retrieves a sheet named "Approvals".
getDataRange() Gets all the data in the sheet as a range.
getValues() Returns the values of the cells in the range as a two-dimensional array.
MailApp.sendEmail(email, subject, body) Sends an email with the specified recipient, subject, and body.
sheet.getRange(i + 1, emailSentColumn + 1).setValue("sent") Sets the value of a specific cell to "sent", indicating an email has been sent.
google.script.run Calls a Google Apps Script function from a web app.
withSuccessHandler(function()) Specifies a function to run if the google.script.run call succeeds.
withFailureHandler(function(err)) Specifies a function to run if the google.script.run call fails, passing the error as an argument.
updateStatusInSheet(approvalId, status) A custom Google Apps Script function (not shown in code snippet) that would update the status of an approval request in the spreadsheet.

Deciphering the Automated Email Mechanism

The automated email trigger system I designed for Google Sheets primarily aims to streamline the approval process within organizations, particularly for cases requiring consent from multiple approvers before proceeding. The first part of the solution, crafted in Google Apps Script, directly interacts with the Google Sheets where approval statuses are recorded. The script checks the entire "Approvals" sheet for rows where both approver 1 and approver 2 have marked their approval as "approved." This is crucial because the script is intended to act only when both approvals are granted, reflecting a fully authorized request. To achieve this, the script iterates through each row, examining the specific columns designated for each approver's decision and the overall status of the request. When a row meets the criteria—both approvers have approved, and the status is set to "approved"—the script triggers an email to the IT department. This email notification is sent using the MailApp service, a part of Google Apps Script that facilitates email sending directly from the script. It ensures that the IT department is promptly informed about the approved request, allowing for swift action.

The mechanism for updating the approval status through a web application serves as the frontend counterpart to the automated email system. This component is especially significant because the "onEdit" trigger in Google Sheets only responds to manual edits, not programmatic changes. To circumvent this limitation, a simple web interface allows users to update the status of an approval request. Upon interaction, such as clicking a button to mark a request as "approved," the web app calls a Google Apps Script function through the `google.script.run` command. This command is powerful because it enables the script to perform actions in the Google Sheet based on inputs received from the web interface, effectively mimicking manual edits. The script can then proceed to check for changes and send emails as designed, bridging the gap created by the "onEdit" trigger's limitations. This dual-component solution ensures that the approval process is both efficient and adaptable, accommodating the need for both manual and automated interventions in the workflow.

Streamlining Email Notifications for Approval Stages in Spreadsheet Applications

Google Apps Script for Backend Processing

function checkApprovalsAndSendEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Approvals");
  var range = sheet.getDataRange();
  var values = range.getValues();
  var emailSentColumn = 5; // Assuming the fifth column tracks email sending status
  var approver1Column = 2; // Column for approver 1's status
  var approver2Column = 3; // Column for approver 2's status
  var statusColumn = 4; // Column for the overall status
  for (var i = 1; i < values.length; i++) {
    var row = values[i];
    if (row[statusColumn] == "approved" && row[emailSentColumn] != "sent") {
      if (row[approver1Column] == "approved" && row[approver2Column] == "approved") {
        var email = "it@domain.com";
        var subject = "Approval Request Completed";
        var body = "The approval request for " + row[0] + " has been fully approved.";
        MailApp.sendEmail(email, subject, body);
        sheet.getRange(i + 1, emailSentColumn + 1).setValue("sent");
      }
    }
  }
}

Automatically Updating Approval Status via Web App

HTML & JavaScript for Frontend Interaction

<!DOCTYPE html>
<html>
<head>
<title>Approval Status Updater</title>
</head>
<body>
<script>
function updateApprovalStatus(approvalId, status) {
  google.script.run
    .withSuccessHandler(function() {
      alert('Status updated successfully.');
    })
    .withFailureHandler(function(err) {
      alert('Failed to update status: ' + err.message);
    })
    .updateStatusInSheet(approvalId, status);
}
</script>
<input type="button" value="Update Status" onclick="updateApprovalStatus('123', 'approved');" />
</body>
</html>

Enhancing Workflow Efficiency Through Spreadsheet Automation

The concept of automating email notifications in Google Sheets as part of a two-step approval process introduces a sophisticated method to streamline organizational workflows. Traditionally, manual interventions in approval sequences have been a staple, requiring human actions to push processes forward. However, by leveraging Google Apps Script, we pivot towards a model where such interventions are minimized, leading to enhanced efficiency and error reduction. This shift not only accelerates the overall approval process but also ensures that notifications are dispatched at the correct juncture, specifically when both approval parties have sanctioned a request, marked by the transition of status to "approved."

This approach underscores the significance of programmatically managed status updates within the spreadsheet, a method that sidesteps the limitations of the "onEdit" trigger. By employing a custom script that listens for status changes and sends email notifications accordingly, organizations can circumvent the manual bottleneck, thereby automating a critical component of their operational workflow. This methodological pivot not only refines the approval process but also introduces a level of scalability and adaptability previously unattainable through manual processes, opening the door to a more dynamic and responsive workflow management system.

Frequently Asked Questions on Spreadsheet Automation

  1. Question: Can the automation process work for any Google Sheets document?
  2. Answer: Yes, the automation can be applied to any Google Sheets document, provided the script is correctly configured for that specific document's structure.
  3. Question: Is coding knowledge required to implement these scripts?
  4. Answer: Basic coding knowledge in JavaScript is beneficial for customizing and implementing the scripts in Google Apps Script.
  5. Question: Can the automated email trigger handle multiple approval requests simultaneously?
  6. Answer: Yes, the script can handle multiple requests by iterating through rows of data and checking the approval status for each request.
  7. Question: How secure is the automated process?
  8. Answer: The process is as secure as any Google Sheets and Google Apps Script operation, utilizing Google's standard security protocols to protect data.
  9. Question: Can the script send notifications to multiple email addresses?
  10. Answer: Yes, the script can be modified to send notifications to multiple email addresses by adjusting the recipient parameter in the MailApp.sendEmail function.

Summarizing Insights and Forward Steps

The exploration of automated email triggers within Google Sheets for a two-step approval process reveals critical insights into the limitations and potential solutions for streamlining such workflows. The default onEdit trigger's inability to recognize programmatic changes necessitates creative scripting approaches to ensure that notifications are sent only when approvals are fully confirmed. This scenario underscores the importance of customized Google Apps Script solutions to bridge gaps in Google Sheets' native functionalities, enabling the development of more dynamic and responsive approval processes. By leveraging Google Apps Script to create specialized triggers and functions, organizations can enhance their operational efficiency and communication flow, ensuring that key stakeholders are promptly informed upon the completion of approval stages. The discussion highlights the necessity for adaptability in the face of platform limitations, encouraging a proactive approach to problem-solving within automated systems.