sand4sale
sand4sale

Reputation: 3

How to get the column header values to be included in email with this script for Google Sheets

i found this post,Get Google Forms Responses in an Email Message, that helped me set-up an script that will send a new email to the email provided when a new row is created in a sheet. that works great!

however, i would like for it to include the column header (forgive me if that's not the right name) before the corresponding row cell-data.

this is my script currently:

function send_email() {
  var row = SpreadsheetApp.getActive().getDataRange().getValues().pop();
  var message = row.join('\n');
  var subject = 'test Request for Bid';
  var address = '[email protected]';
  GmailApp.sendEmail(address, subject, message);

}

this is what the emails look like:

erik
lastname
[email protected]
1234567890
Email
material 1, material 2
77 tons
something and something
something
testing

each line of the email above has a labeled column header i.e. first name, last name, email, phone, etc.)

how would i get the final email to look like this (everything before the ":" is the labeled column header):

first name: erik
last name: lastname
email: [email protected]
phone: 1234567890
preferred contact method: email
materials: material 1, material 2
quantity: 77 tons
cross streets: something and something
city: something
message: testing

i do not know where to begin with this unfortunately.

Upvotes: 0

Views: 102

Answers (1)

Boris Baublys
Boris Baublys

Reputation: 1203

Try it:

function send_email() {
  var header = SpreadsheetApp.getActive().getDataRange().getValues().shift();
  var row = SpreadsheetApp.getActive().getDataRange().getValues().pop();
  var message = "";
  for (var i = 0; i < header.length; i++) {
    message += header[i] + ": " + row[i] + "\n";
  }
  var subject = 'test Request for Bid';
  var address = '[email protected]';
  GmailApp.sendEmail(address, subject, message);
}

Result:

first name: erik
last name: lastname
email: [email protected]
phone: 1234567890
preferred contact method: email
materials: material 1, material 2
quantity: 77 tons
cross streets: something and something
city: something
message: testing

shift()

Upvotes: 0

Related Questions