I’m writing to you because I’m facing a problem with an automatic email script based on Google Form submission (or new GSheet entry when GForm is sent). Process in short:
I have a Google Form (here's similar one) that is linked to Data Studio and Power BI Dashboards. Based on data that goes into spreadsheets, the forms are automatically pre-filled using calculated fields in dashes (Date, ActionID, Code, ID, Location etc.) and data goes to Dash-linked spreadsheet [Example of what it might look like]. The thing is that in case of errors found during inspections, the copy of the form/email should be also sent to Employee (optionally, the person that is superior to them, so that both know the details, not only the auditor who fills-in the form – just option). I can’t use any available Add-ons for GForms due to sensitive nature of data. Auditors don’t have edit access to Forms/Dashes, and sending all information via email would take too long. I also created a test Google Form to demonstrate what it looks like.
All in all, What I’m looking for is a dynamic script that will also grab company email of the employee (supervisor is optional), and when auditor submits the form, the email/copy of the form also goes to those mentioned aboce. I couldn't find anything GForms related.
I used a similar script before, but don’t think it was based on new rows added to spreadsheets [Example]. If it’s not possible to achieve something like this, even spreadsheet entry form/email would be really greatly appreciated (email based on "EmployeeEmail" from form + selected columns included, as marked in sheet), although I’m afraid of things going wrong in a file with lots of rows. I tried to find something similar and discovered 2 scripts, but I don’t know how to make them dynamic: The first one below just sends messages to emails from a given range and it’s static (range).
thanks! It looks good, but I have 2 issues.
Edit:, it always needs that dummy email ('xxx#xxx.xxx'”, which means I’ll (or sb else) be getting hundreds of emails. Can we somehow get rid of this line or replace it with form sender email (I can replace existing column with one with form sender email (google form add emails automatically)/those that are already in columns 2/3?
2nd thing, how can I concatenate dummy message lines to variables:
var message = row.join('\n'); (currently adds values from row n)
e.g. can we somehow replace text join with e.g.
// Info from specific columns
var Date = lastRowValues[n][5]; //I don’t know how to grab last value from columns, it’s probably something like variable.getLastRow() + variable.getLastColumn() + Get values
var ID = lastRowValues[n][6];
var Code= lastRowValues[n][8];
var Action = lastRowValues[n][9];
var Location = lastRowValues[n][10];
var ErrorType = lastRowValues[n][11];
and then in message
// var = message = “You’ve made an error. Please find the details below.” +
“\n “ +
“\n Date of Action: “ + Date +
“\n ID: “ + ID +
“\n Action Code: “ Code +
“\n Type of Action: “ + Action +
“\n Location: “ + Location +
“\n Error Type: “ + ErrorType +
The rest, such as the title, may stay the same. I realised that numbers only are hard to read. Is it possible to achieve this output? This is what it looks like now.
You can add this code in your linked spreadsheet:
// this function will fires automatically
// it grabs all data from the last row and send it via email
function send_email() {
var row = SpreadsheetApp.getActive().getDataRange().getValues().pop();
var message = row.join('\n');
var subject = 'test subject';
var address = 'xxx#xxx.xxx';
GmailApp.sendEmail(address, subject, message);
// additional emails
var address2 = row[2];
GmailApp.sendEmail(address2, subject, message);
var address3 = row[3];
GmailApp.sendEmail(address3, subject, message);
}
// this function should be run just once to install the trigger
function install_onFormSubmitTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('send_email').forSpreadsheet(ss).onFormSubmit().create();
}
After you install the trigger every time your form was submitted all data from the form (= all cells from the last row on the sheet) will be sent to xxx#xxx.xxx. And to two additional addresses from cells 'C' and 'D'.
Feel free to change the code as you need.
Related
I'm making a website for school to connect students and teachers. Teachers fill out a Google Form asking for service (grading papers...) and it is connected to Google Spreadsheets. Then a student can see that Spreadsheet and sign up on a different Google Form to help the teacher, and that response is recorded in the same Google Spreadsheet. When the student submits his/her Google Form, I want the teacher to be notified by email that a student signed up to help them. How can I send that notification? And I don't want the notification to come from my own school address. Is that possible?
Here is the Google website: https://sites.google.com/fcpsschools.net/jmhsservicesignup/subjects/math
This is the code I have so far:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tutors
Signed Up");
var startRow = sheet.getLastRow(); // First row of data to process
var dataRange = sheet.getRange(startRow, 1, 1, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[3]; // Second column
var tutor = row[0];
MailApp.sendEmail(emailAddress,"Tutor Request Filled",""+tutor+" has accepted your request to be tutored! Please notify"+tutor+" if anything changes.");
}
}
The getLastRow doesn't seem to work, and the email sends from my own address, not a Google forms notification if that is even possible.
If this is impossible I might just make the students send the email to the teacher, but I really want to automate the process. If you can think of a workaround, that would also be awesome.
Adding to what Sandy Good and Guilherme mentioned.
Here are a few more tips:
Not clear what data you're trying to get in your function. Are you trying to grab the data from the form submission? In that case you need to set up a trigger from the scripts menu, and connect it to the function (function needs to have an 'event' argument, usually called e).
function processFromSubmission(e) {//get data from e}
Apologies if you're not trying to do that. What happens when you Log sheet.getLastRow()?
Ask the school if they can get you another email address to use for this form, something like notifications#yourschool.edu. Follow the steps here to set up your alias. Do it for the gmail account that "owns" the spreadsheet these forms are sending data to.
Make sure your alias is set up properly. You should be able to see it in the "from" dropdown menu when you compose an email. Also try running the code Logger.log(GmailApp.getAliases()) and check the log to see that it's available.
Use GmailApp.sendEmail to send email, not MailApp.sendEmail. They're almost the same, but it looks like the MailApp version doesn't let you send from an alias. You can specify the 'replyTo' email as well if you want to set it to something else, like the student filling out the form or whatever (still not clear exactly what you're trying to set up!).
Send email like:
GmailApp.sendEmail(emailAddress, subject, body,
{replyTo: replyToEmailAddress_can_be_any_email,
from: 'notifications#yourschool.edu'
});
Note: your link didn't open.
Basically what the title says, but I want to:
1) Automatically count the number of emails received to my gmail account, that has a certain label on it
2) Count once everyday, even if zero
3) And report daily to a Google Spreasheet
4) So I can make a monthly report like such:
Date / #
Date / #
Date / #
.
.
.
Total for October / #
Average per day / #
I'm sure this is piece of cake using Google Script for script gurus, but I have no clue. Please teach me!
Open a new Untitled spreadsheet and go to Tools -> open Script editor and paste the code given below.
function CountEmail()
{
var label = GmailApp.getUserLabelByName("LabelName");
var labelname = label.getName();
var mails = label.getThreads();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var date = new Date();
sheet.appendRow([labelname,date,mails.length]);
}
To run the script daily you will need to set a project trigger in the app script.For that follow the steps given below:
Resources -> Current Project's Trigger -> Add trigger -> Time driven -> Hour timer -> select the time when you want the script to run.
The total number of emails and the average emails/day can be calculated in the spreadsheet itself by just using the Sum() function.
Referencing the code Suyash Gandhi has posted (citing it here in case it gets removed so there is no confusion).
NOTE: not my code!!! Credit to Suyash Gandhi
function CountEmail()
{
var label = GmailApp.getUserLabelByName("LabelName");
var labelname = label.getName();
var mails = label.getThreads();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var date = new Date();
sheet.appendRow([labelname,date,mails.length]);
}
NOTE: not my code!!! Credit to Suyash Gandhi
See the picture below
What you see here are 2 threads, 1 email in the bottom one, 3 emails in the top one and 1 draft in the top one. That given code will return a 2 here. If you wish to count all 4 (or 5 if you want the draft) you will need to use Gmail API (see reference here).
I have a script that gets all emails (every message) for a specific time period and outputs the receive date, sender, recipient and title to a spreadsheet. This is the code that actually fetches the emails. The rest of the code is mostly creating files, generating the query string and reset the script if it runs too close to 6 minutes.
queriedMessages =
Gmail.Users.Messages.list(userInfo.mail,
{
'q': queryString,
'pageToken': execProperties.nextPageId
});
userInfo.mail is the email address you are fetching the emails from. This is simply written like this because the script can be run with any account
queryString is a string that is used to search for emails and is exactly the same as you use in the gmail search box. So you would have label:labelname
pageToken is a code of the page of the search (basically what is needed when you click the next page button in gmail). It is returned as part of this function so you would be able to access it from queriedMessages.nextPageToken. So if you get more than 1 page, then you will need it to access the rest of the messages.
Also, keep in mind that you are getting all the messages fitting the query, so if you do this daily, you may want to include a trigger. Also, keep in mind that functions firing from triggers ignore your timezone (known bug), but you can figure out how to create a query that works for only 1 day fairly easily. Personally I just grab +1 day on the beginning and the end and just filter those messages out.
Q: How can an AppsScript attached to a Form store an extra piece of data into the Sheet?
Situation: We have a (long) Google Form that stores many pieces of data into a Google Sheet. Often the entries need to be edited, and it is much easier to edit using the original form than trying to edit directly into the sheet. (Some of the items are text, several paragraphs long.) I would like to store into the spreadsheet one additional piece of data, specifically the URL that an editor can use to edit the row entry using the form.
I can already get all the form data and I can get the right URL with formResponse.getEditResponseUrl(). And I can send all of that in an email to a user, usually the editor who is collecting all the form entries. (Thanks to many helpful answers in StackOverflow for getting me this far!) But the editor has to manually copy and paste the URL into an additional column in the proper row of the spreadsheet.
I see an interface in class Sheet to add a column to the spreadsheet, but I don't see how to populate that extra column for the particular row that the form just stored. We have added the column manually, and have verified that it is not overwritten by Google when editing via the form. How do I store that one little piece of data into the sheet?
What am I missing? Any help will be greatly appreciated. Thanks.
[added clarifications 2015-02-06]
We have a long form that some people submit and other people edit. Editing is to be done using the form, not editing directly in the spreadsheet, so we need the URL that permits the editors to re-edit the response.
I would like to store that URL into the spreadsheet during the form submission, so that the editors, who have access to the sheet, can find it.
In a script on the Form side, I can easily calculate that URL, but now how do I store it into the sheet in an extra column?
In my Form-side script at the moment, I get the URL and send it, along with all the form data, in an email to the editors' distribution list. One of the editors then copies the URL from the email and pastes it into the sheet. (Most of the time, into the correct row, even. :-) This is a potentially error-prone manual step.)
A secondary question: what is up with the row numbers in the sheet versus the response numbers in the form.getResponses()? The row numbers and response numbers seem to wander as new items are submitted (i.e., new rows), and old items are edited. Can one reasonably predict the sheet's row number in which the editor will find the form data?
Again, thanks for any help you can give me on this. We have a survivable interim solution. However, with a hundred or so form entries coming in the next couple months, I would love to error-proof the process as much as possible.
rick
So, I've just stumbled upon your questions and, hopefully, I've understood it correctly.
Possible problems:
the script is incorrectly bound to the spreadsheet attached to the form and not to the form itself (which is not the problem in your case as far as I understood from your description)
race conditions between submission insertion and additional column edit, or between simultaneous submissions (see lines 27-32 from code)
accessing the spreadsheet directly, without prior selecting a sheet from the spreadsheet, even if it spreadsheet contains only one sheet! (see lines 36-37 from code)
using the column numeric index, instead of the corresponding column letter as argument for getRange() method, which accepts only column letters AFAIK (see lines 42-43 from code)
Below you have the code which should address all these problems (I have not tested it, but it is an adaptation of a perfect working solution for a very similar scenario):
// Converts sheet column numeric index to corresponding column letter
function columnToLetter(column)
{
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
The following function must be registered to an "On form submit" event from form - not from the spreadsheet! (Script Toolbar -> Resources -> Current project's triggers -> Add a new trigger)
// Associated the sheet rows with response URLs in an additional column
function onFormSubmit(e)
{
try
{
// Get the response Url, either from FormApp:
var responseUrl = FormApp.getActiveForm().getEditResponseUrl();
// Or alternatively get it from the event:
// var responseUrl e.response.getId().getEditResponseUrl();
// ....................
// Other URL processing
// ....................
// Get a public lock on this script, because we're about to modify a shared resource.
var lock = LockService.getPublicLock();
// Wait for up to 30 seconds for other processes to finish.
lock.waitLock(30000);
// Wait for row insertion to finish, so that sheet.getLastRow() method gets the updated number of rows
Utilities.sleep(1000); // 1 second
// Here insert the URL to your spreadsheet
var spreadsheetUrl = "https://docs.google.com/spreadsheets/d/YGUgHi28_gYUffGYGGH_78hkO1Pk/edit";
// Gets the first sheet inside the spreadsheet (if you have multiple sheets, just change the value [0])
var sheet = SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets()[0];
// Get updated number of rows and columns, after form submit inserted the new row
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
// Get the exact cell, next to the right of the new row, by converting the column index to corresponding letter
var lastCell = columnToLetter(lastColumn) + lastRow.toString();
// Set the content of the cell with the new URL
sheet.getRange(lastCell).setValue(responseUrl);
// Release the lock so that other processes can continue.
lock.releaseLock();
}
catch (error)
{
// If there's an error, show the error message
return error.toString();
}
}
For any other questions, just write a comment. Hope it helps.
You can use the form submit range parameter to get the row / spreadsheet range of the form data being placed in the sheet. Then use the range offset method to push your data into the column after the last column of form data.
Notice if you use the HYPERLINK formula, you must escape the quotes that are passes as parameters.
e.g.
function formProcessing(e){
var formData = e.values;
var dataRange = e.range; // gets the range on the spreadsheet
/*
do all your processing
*/
var url = "http://www.google.com"; // whatever url to put in spreadsheet
// add the url value to the spreadsheet
formRange.getCell(1,formRange.getLastColumn()).offset(0,1).setValue(url);
// or if you want a named link
//formRange.getCell(1,formRange.getLastColumn()).offset(0,1).setFormula("HYPERLINK(\"" + url + "\", \"Edit Form\")");
}
I have created an application where I am collecting form responses from various users. I am getting responses with email id in responses spreadsheet. As I don't want to store data in spreadsheet so I am reading data trough responses. I am facing some challenges please guide.
Query 1
while using onFormSubmit(e) I am not able to read submitted form, given code is returning null:
var form = FormApp.getActiveForm();
Logger.log('usename:' + form.getId());
error " Cannot call method "getId" of null." although if I hard coded value of formid var form = FormApp.openById('<<form_id_xyz>>'); then it is working fine and I can read responses as well.
How can I get form responses for multiple users?
Query 2
getRespondentEmail(); is not working in my case. Even I use form id <<form_id_xyz>> and trying to get email id from responses which I have captured at the time of form submission form.setCollectEmail(true); I tried following code in onFormSubmit(e) function but dint get a result:
var formResponse=form.response;
Logger.log('email id of user: ' + formResponses.getRespondentEmail());
and another way:
Logger.log('email id of user: ' + form.getRespondentEmail());
and
Logger.log('email id of user: ' + e.values[1]);
nothing works for me. Kindly guide.
Query 1: Hope it's clear in my comment.
Query 2:
Sorry to say, I don't understand your second query problem completely.
However as per your requirement I am suggesting this code.
If you have created a form you should know the form id (I assume) so try this code.
var form=FormApp.openById('your form id here');
//this returns your form which you created//
var responses=form.getResponses();
/// this will give you all responses of your form as an array////
///iterate the array to get respondent email id///
for(var i = 0; i < responses.length; i++){
Logger.log(responses[i].getRespondentEmail());
}
I think it's important to note that at the present time the answer to your question is: You can get what they enter, but you cannot get their true verified Email Address. This is explained better in this question and one of the answers details some workarounds such as publishing form as a web script.
The accepted answer displays what email address the user has typed into the form. There is no authentication to this beyond it having an # symbol thus a user could type foofoo#zoomZoom.com and it would be viewed in the forms results and scripts.
What's annoying is that google IS capturing the user's true email address because if settings are set to Allow One Response Per User, then the user is limited to one submission -- regardless of what they put as their email account. I'm not sure why Google won't provide a method to view the submitter's login email address since it has been disclosed to the user that this will be disclosed.
Microsoft Forms does capture this.
Seeking guidance on the following:
User submits Formstack form -> Form entry populates a row on Google Spreadsheet -> Trigger the execution of a document merge for that submission (now a ROW in the sheet) with the column header defining the variables -> Email merged document to User via submitted email address.
My document requires the ability to conditionally handle many of the variables (if/then conditional text based on form response). I've played with an App called Ultradoc which seems great for the variable handling in the Google Document, supports conditions, etc.
However, the problem is it doesn't know how to select one ROW of data, rather it's designed to run a merge for everything in each column.
One idea might be to run a script which takes that new ROW and somehow makes it look like a two row sheet (header+submitted row)?? Somehow hides the other rows? This seems terribly kludgy. What's the right way to approach something like this?
Thanks in advance
You actually don't need any third party apps for this.
Create the form as a Google Form, create the script and make it run on Form submit.
To get the values that were entered into the form, pass the form submission as a parameter to the function: function processForm(e){
To get the data in the form, access the e.values array. It's zero indexed, starting with the top item on the form.
Store the submitted data into variables
var name = e.values[0];
var email = e.values[1];
// and so on...
Perform any validation or document handling.
Create a copy of your template document
var copyId = DocsList.getFileById("templateDocID")
.makeCopy(docName+' from '+name) //or whatever you wanted to call the resulting document
.getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getActiveSection();
Replace the text in the template
copyBody.replaceText("NAME", name);
copyBody.replaceText("EMAIL", email);
// and so on...
Send the document to the user (for my application, I sent as pdf, but you can send it how ever you like)
copyDoc.saveAndClose();
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
MailApp.sendEmail(email, copyDoc.getName(), "Here's your document", {attachments:pdf});
DocsList.getFileById(copyId).setTrashed(true);