Email notification in google apps script - google-apps-script

https://support.google.com/docs/answer/91588?hl=en
I have a Sheet where the the cells are filled in with data that is pulled via Apps Script, which they get from a website. Sometimes they pull in the exact same data, sometimes not. On the old Sheets version I use to be able to get emails when the data only "changes" to a different value. Now with the new Sheets, I get an email when new data is pasted into the cells, even though its the exact same data......and, I can't set it to look at ranges anymore, only the entire document. Is there an easy way to watch cells for when the data changes, and then email myself?

This looks a lot like what you're trying to do.
For checking whether the value has truly changed after the edit, this gives you all the necessary information about it. Hence, you would know whether the new value is the same one or not. Implementing the "workaround" talked about in the second link, I was able to compare and send an email to myself if the value changed. Initiate an onEdit trigger that goes off for your onEdit function which uses these lines after all the checks in place have passed.
var email = "xyz#whatever.com";
MailApp.sendEmail(email, "TriggerMail", "A change has occurred in the Sheet");
NOTE: This only sends you a "notification" sort of an email but you can even include the changed values in the email easily by just following this tutorial.

Related

Pulling data from Sheets into Gmail template - one email at a time, not a bulk mail merge

I'm trying to come up with a solution that will allow me to (1) pull data from Sheets for only ONE of the rows into a Gmail template and (2) allow for modifying the template with additional information that's not in the spreadsheet before sending it off.
Context: My organization uses an intake/referral log to track key information about referrals. Once info is in the Sheet, they manually send an email assigning the referral to an employee for follow-up. Much of the info in the Sheet is retyped...which initially made me think of automating this fully with AppsScript, so that all the standard info (case #, case type, due dates, etc) is included in that email. However, the emails sometimes have a paragraph or three of contextual information that we don't want to store in the spreadsheets and have pulled into the automated email.
So my goal is to pull the data from the spreadsheet into the email where I can (vs having to retype it) AND allow the addition of contextual info before sending it off to the assigned employee.
Any thoughts on how to accomplish this? I haven't found a mail merge add-on or Apps Script solution that allows doing this one row at a time and adding in extra custom info into the email.
Thanks in advance!
So this question, as currently written, could clearly be accomplished a whole bunch of different ways. With that in mind, here is one quick and dirty way in which it might be accomplished:
On the spreadsheet where the data is stored, create a script via tools>script editor.
Using that script, create a menu which runs a function that does the following, perhaps in an endless loop:
a. Prompt the user for the email address of where to send the email if it isn't in the spreadsheet or a token value, such as an empty string, to end the script.
b. Prompt the user for the number of the row where the info is stored.
c. Prompt the user for any extra contextual information to add to the email.
d. Get the info from the applicable row in the spreadsheet.
e. Create a nicely formatted email and send it off.
From there, the person in charge of doing the referrals would simply run the script and feed it the required information. Clearly human error could fowl things up here, but without more details of exactly how you want this to work, it isn't really possible to define exactly how to best protect against human error. Reading up on Data Validation might give you a start on that front.
And if you are still completely lost, I happen to be a Google Apps Script tutor/consultant who often gives away free tutoring/consulting. See tutoringbyroger.com for more on that.
I hope that helps.

How to roll-back change events to a Google sheet or disallow only change events but allow edit rights to users?

Trying to build a dashboard wherein, in certain sheets users need to be able to add rows, but shouldn't be able to change values of the preset cells. If I protect those cells, they cant add rows, if I dont protect the sheet, and revert changes to preset cells using a Google Apps Script onEdit trigger, they are able to insert rows, and cant change values, but that leaves my sheet open to unforseen modifications, like inserting columns, inserting more rows than required, changing formatting, etc.
Is there a way to let them add rows but not do anything else to the sheet except the limited cells they need to edit?
I see this issue/request often, you are trying to use Google Sheets in a form-like manner where users should only be permitted to do X actions (some people use Google Forms to feed into the spreadsheet for similar problems).
One idea here is to create a button to add a row. By default you can make everything protected. When the button is pressed, Google Apps Script can unprotect and create a row at that location. One issue with this solution is that buttons in Google Apps Script are like floating images - so you would need to create function names like "addRowAtSheet4". Here is what that button could look like, where ADD CITATION would create a new row on click:
Another issue is that a user that wants to damage your spreadsheet will be able to since the protections are done from the user's bound script and run from the user's account, which brings me to the second solution:
If you need true cell protection (assuming you are the spreadsheet owner), you can create a standalone webapp to do all of the admin stuff and lock everything down on the user side as desired. When the user clicks the button, a web request is issued from the GAS bound script of the spreadsheet to the published webapp, and in the webapp you add the row on behalf of the user from your own account (i.e., "Execute the app as: Me"). Something like this, where the parameters sent are the row to be added, for example:
Note that if there can be multiple simultaneous users adding rows on the same place, you may like to use LockService.

Is it possible with GAS to automatically send emails when a data validation cell is changed without triggers?

Essentially I have a template of a sheet that is copied and filled out by others to make a request, and then their copy is passed between multiple people for approval based on its status, which is represented by a list of items. I'm trying to add a script to the sheet so that when the value in the status cell is changed, it automatically emails the necessary people to notify them of the status change.
The only way I've found to do this so far is to have an onEdit trigger manually added that checks whether the edited range matches the cell's range, and if so, send the email with the new value held in the cell. Ideally, I would be able to incorporate this into the built-in onEdit(e) simple trigger, but I believe that one of the restrictions included with the simple triggers is that they can't be used to send emails, as it would not send the emails until I added the manual trigger.
My problem is that when other people make a copy of the template, the manually added trigger is lost, so the onEdit function no longer properly sends the email. I was hoping that I could set up this trigger in onOpen(e), but it turns out that creating triggers is also a restricted operation for the simple triggers.
I'm trying to find the easiest way to retain the ability to email when the status is changed. I know I could also just make a button next to the status cell, but my boss is really pushing for it to be automatic to changing the status. Thanks!

google apps script to send spreadsheet pdf

I'm very new at this and I think I'm trying to do something a little complicated. Ultimately I'm looking to do the following: I would like to have a spreadsheet that upon a submit button does the following, sends spreadsheet to a specified email address, then increases a specified cell number by 1, then clears data that was entered in specific cells.
I have figured out how to increase a cell on a button push that's as far as I have gotten. Any chance someone would be able to point me in the right direction?
For your emailing you want to use MailApp.sendEmail()
The PDF conversion mentioned in the subject line is also pretty easy to use Spreadsheet.getBlob() or if you want to be a bit more verbose Spreadsheet.getAs('application/pdf')
Generally the GoogleAPI is pretty good at explaining a lot of the detail on how to connect to the google libraries

Emails within Google Sheets

I am hoping i can find a kind hearted person to help me if possible with some google sheets scripting.
I am looking for a way to automate something i currently have to do manually everyday. I take data from present day I have entered into a spreadsheet, copy it, paste it into and email and send it. I have used the Query function within Google sheets to help me separate who gets what information, that has made my life a tad bit easier.
I currently go to each tab, copy data, go to Gmail, paste data, type in the email address of the recipient, type in the subject, send, then go on to the next one 30+ times.
What I would like to do...
1. Click a menu item at top that says something like "Send Emails Now"
2. Once triggered, this would go to all (except 3, but i can just have these go to my own junk mail if too much) the tabs and send an email with the information contained only on that sheet to an email address in Cell A2
Email would have the subject "Call Parking Time XXXXXX" where XXXX is the date in a E1
Here is a copy of the current sheet I have, some reason it will not let me share the original. So i have updated the 20-Sioux Falls and 21-Frederick sheets to show what i mean.
https://docs.google.com/spreadsheets/d/1nOgZzwbKEUF4_adkjxkvmiiVwFtu-2OGCPJ0Wj_keQU/edit?usp=sharing
Thank you for your help in advance.
Although this is an older question, I still have a recommendation for you and other potential users facing the same problem.
I use a Google Sheets add-on called FormMule that should work as a great solution to your challenge here. It allows you to create personalized template emails, set conditions for your merge, and have multiple templates to various people based upon your chosen criteria.
You can also set a time trigger, form submission trigger, or manually trigger it through the add-on menu.
Although I agree with Zig, here are a few things that'll help you get started:
//1. Define your Custom Menu Item
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Data', functionName: 'sendEmail_'}
];
spreadsheet.addMenu('Automated Emails', menuItems);
}
Tutorial for Sending Email (Although this one sends data in each row as a separate email but you should get the idea of what to do)
Sample for creating a table from Spreadsheet Data
This should give you a good head-start. Put pieces of these together and you should be pretty much good to go. And if you hit any roadblocks along the way, people on SO are always glad to help. :)