简体   繁体   中英

How to send data from spreadsheet by email using google scripts

I'm making an online registration with google forms. Once a person submits, an email will be sent to the email they entered. The data is the things they entered in the form. So this email serves as a data confirmation email. The problem now is, the test i did was successful but, the emails contained aaalll of the data. My question would be, how to email the data from a specific row for a specific email

here's what i did (copied from someone)

      function sendEmail() {

 //setup function
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 3;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
 var AllValues = WholeRange.getValues();

 var message = "";
  //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var EmailSent = CurrentRow[11];

 //if row has been sent, then continue to next iteration
 if (EmailSent == "sent") 
 continue;

 //set HTML template for information
 message +=
  "<p><b>Found by: </b>" + CurrentRow[1] + "</p>" +
  "<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
  "<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
  "<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
  "<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
  "<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
  "<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
  "<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
  "<p><b>Useful Links: </b>" + CurrentRow[9] + "</p><br><br>";

  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 11).setValue("sent");
  }

 //define who to send grants to 
 var SendTo = "emailaddress1@gmail.com" + "," + "emailaddress2@gmail.com";

 //set subject line
 var Subject = "Grants";


  //send the actual email  
  MailApp.sendEmail({
  to: SendTo,
  cc: "",
  subject: Subject,
  htmlBody: message,
   });
    }

I answered a similar question about this recently. It's always easier for me to break down the problem into the parts we need. In this case, we need: All the responses, then we need to match a response

The best way is to first read the form responses as an array:

function formResponsesToArray() {
  var app = FormApp.openById(...),
  responses = app.getResponses(), stringResponses = []

  responses.forEach(function(r) {
    var response = []
    r.getItemResponses().forEach(function(i) {
      response.push(i.getResponse())
    })

    stringResponses.push(response)
  })

  Logger.log(stringResponses)
}

If you know the persons email, then we can get their response like this, assuming the email is the first thing they enter in to the form:

var theirEmail = "hello@hello.com"
var theirResponse = stringResponses.filter(function(response) {
  return response[0] === theirEmail
})

Then you can use theirResponse to get the information they entered in to the form!

The code you shared basically gathers all the rows data where a email status field has no "sent" value and composes a single message at the end and sends it. According to your condition, you need to send a different mail for every row where email status field has no "sent" value.

I have added the mailApp syntax inside the loop only to send different mails and done some changes with message variables.

 function sendEmail() {

 //setup function
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 3;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
 var AllValues = WholeRange.getValues();

  //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var EmailSent = CurrentRow[11];

 //if row has been sent, then continue to next iteration
 if (EmailSent == "sent") 
 continue;

 //set HTML template for information
 //here i have removed the "+" sign to create a new message for its respective row
var message=
  "<p><b>Found by: </b>" + CurrentRow[1] + "</p>" +
  "<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
  "<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
  "<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
  "<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
  "<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
  "<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
  "<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
  "<p><b>Useful Links: </b>" + CurrentRow[9] + "</p><br><br>";

  //here you can add the email address field to whom you want to send emails.
  //e.g. var SendTo=CurrentRow[1];
   //define who to send grants to 
   var SendTo = "emailaddress1@gmail.com" + "," + "emailaddress2@gmail.com";

   //set subject line
   var Subject = "Grants";

    //send the actual email  
    MailApp.sendEmail({
    to: SendTo,
    cc: "",
    subject: Subject,
    htmlBody: message,
     });


  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 11).setValue("sent");
  }
  }

Hope this helps, Thanks

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