Combining runs from "on edit" or "on change" triggers in Google Apps Script

I've been working on a script to alert me about low stock items in my inventory tracking spreadsheet, but the script works a little too well.

The issue is that each time I update the inventory for 10 items, the script runs and sends out 10 email notifications due to Google's "On Edit" trigger. To try and consolidate all these emails into one, I attempted to use Utilities.sleep(300000) to create a delay before the script runs again. However, this just resulted in the same 10 emails being delayed by 5 minutes.

It seems like Google processes the script in parallel with every trigger activated, causing multiple notifications to be sent out instead of just one consolidated email.

Any suggestions on how I can modify my script to only send a single email notification after all edits have been made?

This is my first coding project, so I'm still learning. Any guidance or help would be greatly appreciated!

Answer №1

Before anything else, take a look at the resources provided in this link. My suggestion would be to utilize the update(recipient, subject, body) function to configure the emails for continuous updating of the same email. Following this, you can incorporate an "On Edit" feature to activate a timer that will send the email after each edit and a designated delay period.

Answer №2

Concern:

A situation arises where the onEdit trigger activates every time the script undergoes editing. To avoid triggering an email with each edit, using the onEdit trigger may not be the most suitable choice.

Solution #1. Utilize a button or custom menu:

To address this issue, consider executing the function through a button or a custom menu. This way, you have control over when to send an email.

Solution #2. Time-driven approach:

If manual triggering is not preferred, another option is setting up a time-based trigger (refer to available triggers here). This trigger can run at specified intervals such as hourly, daily, or every few minutes to check for changes in low stock items and send emails accordingly.

To track changes effectively, consider using PropertiesService to store low stock items during each email alert and compare them with current data upon trigger execution.

In essence, the script operated by the time-driven trigger should perform the following tasks:

function sendEmailIfChanges() {
  // Retrieve low stock items from spreadsheet
  // Fetch stored properties from previous run
  // Compare current and stored low stock items
  // If differences are found (e.g., item IDs or quantities), 
  // send email and update low stock items using PropertiesService
}

Resources:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is it that styling <div> and <img> with a URL doesn't seem to work, even when applying the same styles?

In the example I have, I apply the same styling to an image and a div. Interestingly, the styling on the div makes the image look significantly better, while on the image itself it appears distorted. What could be causing this discrepancy? Both elements a ...

Select state and city options similar to the national breakdown page

I'm attempting to replicate a state and city selection box similar to the one on the NTTS Breakdown website. You can see it in action here: When you select a state on the left side of the webpage, the city selection box displays "loading data" and th ...

Strategies for avoiding sudden movements in the navigation bar?

I've encountered a small issue while working on my personal project and I'm seeking some guidance. The technologies I'm using include Bootstrap4, ejs templating engine, and express. In my project, the Navbar display differs based on whether ...

Tips on changing tabs by simply clicking the save button or linking the save button to tab-switching functionality

import { Component, OnInit, OnDestroy } from '@angular/core'; import { FormGroup, FormControl, Validators, FormArray } from '@angular/forms'; import { CustomValidators } from 'ng2-validation'; import { Busi ...

retrieving the value of an object key based on changing information

console.log(x, obj.fares) //return undefined output adultFare Object {adultFare: "9.00", childFare: null, seniorCitizenFare: null, disabledFare: null,} How do I retrieve the adultFare value from the object? Is looping through the keys necessary? I expec ...

Retrieving MongoDB Records in Meteor

Currently, I am attempting to wrap my head around Meteor's method of returning database records. Here is the code snippet that I have been working with: Template.body.helpers({ items(){ return Items.find({}); }, json(){ console.log(this ...

Express.js never terminates a session

I have a Backbone View that makes an Ajax call to the server to delete a session. Upon triggering the following event on the server: app.delete('/session', function(req, res) { if (req.session) { req.session.destroy(function() { ...

Traverse through the nested JSON array using a loop

Exploring a JSON object: { "date_price": [ { "date": "Jan 2000", "price": 1394.46 }, { "date": "Feb 2000", "price": 1366.42 }, { "date": "Mar 2000", "price": 1498.58 }, { "date": "Apr ...

Placing emphasis on an object that appears following a period of waiting

I'm currently working on enhancing the accessibility of a slider that will be featured on my website. The goal is to create an effect where, after clicking (or pressing enter) on the first slide, the focus shifts to the second slide element, allowing ...

Determining the best application of guards vs middlewares in NestJs

In my pursuit to develop a NestJs application, I aim to implement a middleware that validates the token in the request object and an authentication guard that verifies the user within the token payload. Separating these components allows for a more organi ...

Vue automatically refreshes momentjs dates prior to making changes to the array

I am dealing with a situation where my child component receives data from its parent and, upon button click, sends an event to the parent via an event bus. Upon receiving the event, I trigger a method that fetches data using a Swagger client. The goal is ...

Ensure that video controls remain visible for 3 seconds after hovering over them

Is there a way to make video controls only appear when hovering over the video and then disappear after three seconds when the cursor is moved away? The current behavior works correctly when the video is at the beginning, but if I hover while it's pla ...

Retrieve the nth element from an array using a function that requires 2 arguments

During my coding journey, I encountered a challenge that has proven to be quite tricky. The task in question goes as follows: Create a function that accepts an array (a) and a value (n) as parameters Identify and store every nth element from the array in ...

Returning a JSON representation of a JavaScript object

In the process of working on a script, I encountered a situation where an $.ajax call had to invoke a function upon success, returning a JavaScript object in JSON format [object object]. However, despite being able to return a well-formatted string, access ...

elimination of nonexistent object

How can I prevent releasing data if two attributes are empty? const fork = [ { from: 'client', msg: null, for: null }, { from: 'client', msg: '2222222222222', for: null }, { from: 'server', msg: 'wqqqqqqqq ...

Issue alert before running tests on component that includes a Material UI Tooltip

This is a follow-up regarding an issue on the Material-UI GitHub page. You can find more information here. Within my Registration component, there is a button that is initially disabled and should only be enabled after accepting terms and conditions by ch ...

Enhancing webpage performance by updating CSS properties dynamically based on mouse movement using JavaScript

My jQuery function is changing the background-position property of three elements when the user moves their mouse, but it's causing some performance problems. It's worth mentioning that the background images of these elements are SVGs. Here&apo ...

"Encountered a Json error with a bizarre character causing the string

After extracting some json data from a website, I encountered an issue when trying to parse it using vscode. I kept getting an 'unexpected end of string' error on the "content" line: Here is the json: { "name": "Anna Vergnas", "date" ...

Stubborn boolean logic that refuses to work together

Seeking guidance on resolving a persistent issue with my website that has been causing me headaches for the past few weeks. This website was created as my capstone project during a recent graduate program, where I unfortunately did not achieve all the desi ...

Running globally installed node modules on Windows 7 is not supported

Note: Despite scouring various posts on this issue, I have not found a solution that works for me. That's why I am reaching out here. Problem: I am facing an issue while trying to set up the http-server package on my Windows 7 system using the comman ...