简体   繁体   中英

Get Google Forms Responses in an Email Message

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)

eg can we somehow replace text join with eg

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM