Google script editor send email on value insert or change - google-apps-script

I would like to make my sheets to send email message once one field is updated/inserted.
Was trying to follow a few guides, but unfortunately, not getting the result I want
Lets say I have this document:
The most important column is "I" - if the value is inserted here (date) - email must be sent to the client. Client's email can be found in "E" column.
So, what I have to do is to take email from "E" and send him the email with correct date from "I" field.
In this scenario: I have to send the email to user "a#a.com" with the date "2020-01-16"
These rows can be inserted any time since it's connected with Google forms, so the script should always look up if there are any new rows.

You have to use an installable trigger, it might be on form submit or on change
The first will be run every time a new form submission is done. The second will be triggered every time a change (edit, insert row, remove row, etc) is done. Please bear in mind that each trigger has its own event object.
Reference
https://developers.google.com/apps-script/guides/triggers/installable

Related

Generating hyperlinks to prepopulate Google form

Context:
I created a Google Sheets/Forms workflow using sequential stages of Google Forms.
Form 1 (public facing) Accepts some data submitted by a public user and saves to Sheet 1.
Internal staff then contacts the submitter by telephone and conduct a more in-depth interview.
Form 2 (internally facing) is used by the interviewer to document
answers to the phone interview.
This question concerns the generation of Form 2 because I am partially pre-populating it with information from Sheet 1 (Form 1 submissions.) The way I figured out was to formulaically generate a URL with appended pre-population arguments e.g. "&entry.NNNNNN=whatever". I copied-down this formula in the last column of Sheet 1. Clicking on the cell and then the generated hyperlink successfully pre-populates Form 2 with data from the respective row of Sheet 1 as intended.
Problem: As soon as a new Form 1 submission is received, a new row is inserted into the Sheet 1 that does NOT contain the desired hyperlink formula in the last column. I would like that to be automatic so the interviewer is not responsible for performing a copy-down before every request for a Form 2.
I have pursued a couple of approaches to automating this:
One thread advised instead of copy-down, to create an arrayformula in the top cell so that it applies to the entire column including newly inserted rows as well. I tried every way I could think of but was unable to get my formula to produce a column of results with arrayformula(). If there is a way to fix this, that would be a satisfactory solution.
=HYPERLINK(CONCATENATE("https://docs.google.com/forms/d/e/XXXXXXXXXXXXXXXX/viewform?usp=pp_url&entry.251357138=",C2,"&entry.966351469=",D2,"&entry.384696201=",E2,"&entry.1366694528=",F2,"&entry.463407115=",M2,"&entry.1557144679=",B2,if(P2,"&entry.1777888516=Email",""),if(O2,"&entry.1777888516=Phone",""),if(H2,"&entry.2110474669=Individual+(Adult)",""),if(I2,"&entry.2110474669=Individual+(Under+18,+Minor)",""),if(J2,"&entry.2110474669=Couple",""),if(K2,"&entry.2110474669=Family",""),if(L2,"&entry.2110474669=Group",""),if(R2,"&entry.1892971721=San+Jose",""),if(S2,"&entry.1892971721=Sunnyvale","")), "Complete Intake")
I tried to create a ModalDialogue and display a script generated hyperlink in it. I used this approach found in this forum. But this did not open any dialog at all and threw no errors (even after hyperlink was removed.) There was no indication of pop-up blocking. Other parts of my script use Browser.msgBox without any pop-up troubles, but I don't think that will pass a hyperlink.
var htmlOutput = HtmlService
.createHtmlOutput('Click to open and prefill intake interview form.')
.setWidth(250) //optional
.setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Ready to fill intake interview form:');
Using onFormSubmit() and scripting a copydown after a new row has been inserted. But I have been unable to figure out how to identify which row was inserted into Sheet 1. I see some people using lastRow(), but it isn't always inserted into the last row - typically it goes in the middle somewhere.
Request:
Help getting arrayformula to work in my case.
Or help getting ModalDialog to display a script generated hyperlink.
Or help on how to identify the row the Form submission inserted. Or do I just need to make sure the table remains sorted by TimeStamp and then I can use lastrow()?
Suggestion for a cleaner approach to get to the same place (generating a prepopulated Form from a row of data in Sheet 1.)
Thank you for illuminating a path forward.
You should consider using ArrayForumlas to automatically copy down the formula to other rows that have a value in the first column.
Put this formular in row 2 of the column that has the Google Form links.
=ARRAYFORMULA(
IF(ISTEXT(A2:A),
HYPERLINK(
CONCATENATE(
"https://docs.google.com/forms/d/e/XXXXXXXXXXXXXXXX/viewform?usp=pp_url&entry.251357138=",C2:C,"&entry.966351469=",D2:D
)),""))
I wrote a tutorial on copying formulas if you need more examples.
It turned out I was able to successfully employ approach 2, the modalDialog. For some reason no dialog was appearing when I first attempted so I didn't know if there was something fundamental wrong with this approach. I tried again and it worked as shown below so I suppose I just had some typos. Adding target="_blank" was helpful so as to open in a new tab.
var htmlOutput = HtmlService
.createHtmlOutput('Click to open and prefill intake interview form.')
.setWidth(250) //optional
.setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Ready to fill intake interview form:');

How to add date to empty cell by default if none specified in Google Form, on form submit with App Script?

I have a time log form that has a date field I do not want to have to fill out every time as 90% of the time the date would be today's date. (Sometimes you will be entering something from another day and need to change it).
When an entry is added column A is always going to have something, so it should check to see if A is empty and if that same rows D is empty it will input today's date into D.
Some of the example's I have looked at were for onEdit of cell change it, but I could not get it to work with a form submit or even just have it automatically check all the cells in D and if empty put today's date (Only when B has value).
I have a function to sort the sheet right now that is called when on form submit/onEdit happens and would like to stick the new formula in the bottom of the same one (unless that is bad practice). The reason for this is less functions to have to add to the trigger list.
A: Timestamp | B: What | C: Paid? | D: Date
You can try using Date from Google spreadsheets function list which Google Sheets offers.
And, as mentioned in Custom Functions in Google Sheets, if the available functions aren't enough for your needs, you can also use Google Apps Script to write custom functions then use them in Google Sheets just like a built-in function.
To get started, guidelines for custom functions and samples can be found in the given documentations.
You can use Google Apps Script with onFormSubmit trigger which will populate the said cell with default date.
Here is a sample code which will populate the current date in Column 4 of the row in question.
function onFormSubmit(e) {
//edit responses sheet name
var responseSheetName = 'Form Responses 2';
//Edit colmn number, column in which the date has to be autopopulated
var column = 4;
//Get target row number
var row = e.range.rowStart;
//If no date, pouplate the cell with current date
if(!e.values[column-1]){
SpreadsheetApp.getActive().getSheetByName(responseSheetName).getRange(row, column).setValue(new Date())
}
}
In order to setup the above code, open the Spreadsheet which contains the form responses. Go Tools > Script Editor. This will open script editor window. Paste above code and edit the responseSheetName and column . Now Save it.
After saving it, setup an on form submit trigger.
To setup the trigger, follow these steps.
From the script editor, choose Resources > Current project's triggers.
Click the link that says: No triggers set up. Click here to add one now.
Under Run, select the name of function onFormSubmit which you want to trigger.
Under Events, select From spreadsheet.
Select On form submit
Optionally, click Notifications to configure how and when you will be contacted by email if your triggered function fails.
Click Save.

Is it possible choose from contacts when entering email address into a spreadsheet

I would like to recreate the gmail to: type field but for entering in data to a spreadsheet. So if a person starts typing in an email address it will look at their contacts (and domain contacts) and show a filtered set of emails.
No you cant because there is no event to detect the user typing.
What you can do is add a menu item that looks at the active cell and attempt to do the autocomplete on the cell.
I wound up using Data Validation.
Get a CSV of the contacts.
Import those contacts into a sheet on the spreadsheet
use a formula similar to (= A1 & " " & B1) to create a filed that you want to search on. In my case I combined First Name and Last Name
Open a new sheet and right -click cell A1. Use data validation to pull from the column you created in the previous sheet.
just start typing in the box and it opens up like the to: field in an email box.
I did this with 400 names and saw no performance issue.

Create Google Script to send email when cell is edited

This may sound like many other questions - but it's slightly different and I can't formulate an answer.
I have "Tasks" in column A, "Person Responsible" for task in column B, "Email" in column C, and Column D is blank for tracking completion.
When an edit is made to a cell in Column D( like an "X" for completed), I want send an email to the address specified in the corresponding row, with the corresponding task in the email subject/body.
Here is a link to a Google Spreadsheet to help illustrate
http://goo.gl/uFWGE
I can't see any reason why with a minimum of work you couldn't achieve what you want.
please have a look at this post to learn how to restrict the event source range by column and then use simply the Mailapp service like in this tuto and you'll have the pleasure to run an app that you wrote by yourself...
The next one will be even easier and so will be the one after... That process is called learning and we all have been trough this, believe me.

Google Apps Script: .appendRow() doesn't increase form record count

I have a sheet which needs two ways to append records(rows). One is the associated Form and another is the associated script.
The script performs successfull .appendRow() and .getLastRow() was incremented, but the next Form submittion appends data after the wrong row which was the privoius last row (.getLastRow-1).
I also noticed .appendRow() does not inclease the number shown after Form in the spreadsheet menu like Form(123). It seems like the spreadsheet has two different Last Rows.
Is anyone help me to increase The form Last Rows in a script.
Thanks
This appears to be an issue with data populated by a script not being automatically absorbed into the so-called Table associated with Form submissions, which is defined by the light grey background. Subsequent Form submissions will be added to the bottom of the grey area (regardless of data underneath), and the Form submission count will only address the rows in the grey area.
A clumsy workaround is to use the copyTo method to copy the data in place, after appending the row:
sheet.getRange(sheet.getLastRow(),1,1,sheet.getLastColumn())
.copyTo(sheet.getRange(sheet.getLastRow(),1,1,sheet.getLastColumn()));
HTH
Adam