简体   繁体   中英

Google Sheet Auto Email Script

I have a form that contains 84 questions, not all of them are mandatory.

This is the script I manage to write so far:

function SendEmail() {

 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

 var StartRow = 2;

 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;

 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,84);

 var AllValues = WholeRange.getValues();

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

 var CurrentRow = AllValues[i];

 var EmailSent = CurrentRow[85];

 if (EmailSent == "Sent") 
     continue;


# I know this part takes only the first 5 column, I wrote them only as an example. In bold the headers of each column.

  message = 
     "<p><b>Kind of content: </b>" + CurrentRow[2] + "</p>" +
     "<p><b>Project Name: </b>" + CurrentRow[3] + "</p>" +
     "<p><b>Project Description: </b>" + CurrentRow[4] + "</p>" +
     "<p><b>Name of your team: </b>" + CurrentRow[5] + "</p>" +
     "<p><b>Scope of work: </b>" + CurrentRow[6] + "</p>";


  var setRow = parseInt(i) + StartRow;


  ActiveSheet.getRange(setRow, 85).setValue("sent");
}


 var SendTo = "email@gmail.com";

 var Subject = "New"+" " + CurrentRow[2] +" "+"project request";

  MailApp.sendEmail({
      to: SendTo,
      cc: "",
      subject: Subject,
      htmlBody: message,
});
}

What I want is to send an email every time somebody fills the form and the content of the email should include only the last row and only the columns with data with their header.

The way this script is written will generate an email with 84 rows, most of them empty and not relevant. Can somebody give me a hand with it?

Thank you so much for your help!!

You can use sheet.getLastRow() to get the index of the last row in the sheet that has data.

For finding columns that have data, you can iterate through the row data and look for cell values that are not blank.

var header = sheet
             .getRange(1,1,1,sheet.getLastColumn())
             .getDisplayValues()[0];


var data = sheet
             .getRange(sheet.getLastRow(),1,1,sheet.getLastColumn())
             .getDisplayValues()[0];

var output = [];

for (var d=0; d<data.length; d++) {
  if (data[d] !== "") {
    output.push(header[d] + " = " + data[d]);    
  }
}

return data.join("\n");

You can use filter, for example

var AllValues = WholeRange.getValues().filter( row => row[5] != '');

will reduce AllValues to only those there column 6 isn't empty

I know you are too naive to coding and Amit is a busy person, so just to help you, I am plugging in the code he has provided to your code with a small correction, so you can just copy the entire code:)

function SendEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lRow = sheet.getLastRow();
  var emailSent = sheet.getRange(lRow, 86).getValue();

  var header = sheet
  .getRange(1,1,1,sheet.getLastColumn())
  .getDisplayValues()[0];

  if (emailSent != "Sent"){
    var data = sheet
    .getRange(lRow,1,1,sheet.getLastColumn())
    .getDisplayValues()[0];
    var output = [];
    for (var d=0; d<data.length; d++) {
      if (data[d] !== "") {
        output.push(header[d] + " = " + data[d]);    
      }
    }
    var message = output.join("\n");
    var SendTo = "email@gmail.com";
    var Subject = "New"+" " + sheet.getRange(lRow, 3).getValue() +" "+"project request";
    MailApp.sendEmail({
      to: SendTo,
      cc: "",
      subject: Subject,
      htmlBody: message,
    });
    sheet.getRange(lRow, 86).setValue("sent");
  }
}

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