Get Form Data from google docs in an email message



  • Create a new form in Google Docs (or use any of your existing forms) and switch to your Google Docs dashboard. Open the Spreadsheet associated with the Google Form.
  • Go to Tools – > Script Editor and choose “Blank Project.” Remove any existing code in the code editor window and copy-paste the following code. Save the project.
  • Replace “XYZ” in the code with your own email address where you want to receive the form email notifications.
function sendFormByEmail(e)
{  
  // Remember to replace XYZ@yourdomain.com with your own email address
  var email = "xyz@yourdomain.com";

  //Get User mail
var umail = (Session.getUser());

    //Get time
  var enddate = Utilities.formatDate(new Date(), "GMT+7", "HH:mm:ss");

  // Optional but change the following variable
  // to have a custom subject for Google Docs emails
  var subject = umail + "_" + "IT Mobile call log" + "_" +enddate;

  // The variable e holds all the form values in an array.
  // Loop through the array and append values to the body.
  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];  
  var message = "";  
  // Credit to Henrique Abreu for fixing the sort order

  for(var i in headers)
    message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n";
  // This is the MailApp service of Google Apps Script
  // that sends the email. You can also use GmailApp here.
  MailApp.sendEmail(email, subject, message);
  // Watch the following video for details
  // http://youtu.be/z6klwUxRwQI
  // By Amit Agarwal - www.labnol.org
  // Edited by Techtoy
}
  • From the Resources menu, choose Current Script’s Triggers and set up a new trigger. Replace “On Open” with “On Form Submit” and save the trigger.
  • The script with require you to authorize Google Docs to access your Gmail account (for sending email). Just say yes and you’re done.

ความคิดเห็น

บทความที่ได้รับความนิยม