Home > Net >  Google Apps Script to properly order responses in email
Google Apps Script to properly order responses in email

Time:12-30

I currently have a Google form that when submitted provides a very basic email to a few people with the responses. It works great, however, all of the responses are in a random order. Is there a way to sort them so that I can either have them sent as they are on the form or to decide the order myself?

The fields returned in the sheet are:

  • Timestamp
  • Email Address
  • Site Name
  • Address
  • ORD
  • Approving Person (Drop Down)
  • Serial Number
  • Affect Item
  • Packed (Drop Down)
  • Special Instructions

Here is what I currently use:

function sendFormByEmail (e)
{
  var email = "[email protected], [email protected]";
  var txt = "";
  for (var field in e.namedValues) {
    txt  = field   ' :: '   e.namedValues[field].toString()   "\n\n";
  }

  MailApp.sendEmail(email, "Part Requested", txt);
}

CodePudding user response:

Use e.values instead of e.namedValues, like this:

function sendFormByEmail(e) {
  if (!e) { throw new Error('Run this function through a trigger.'); }
  const emailAddress = '[email protected], [email protected]';
  const fieldNames = ['Timestamp', 'Email Address', 'Site Name', 'Address', 'ORD', 'Approving Person', 'Serial Number', 'Affect Item', 'Packed', 'Special Instructions',];
  const emailBody = e.values
    .map((value, index) => `${fieldNames[index]} :: ${value}`)
    .join('\n\n');
  MailApp.sendEmail(emailAddress, 'Part Requested', emailBody);
}
  • Related