简体   繁体   English

Google Sheet Auto Email 脚本

[英]Google Sheet Auto Email Script

I have a form that contains 84 questions, not all of them are mandatory.我有一个包含 84 个问题的表格,并非所有问题都是强制性的。

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.我想要的是每次有人填写表格时发送一个 email,并且 email 的内容应该只包括最后一行,并且只包含带有 Z099FB995346F31C749F6E40DB0F395E3 数据的列。

The way this script is written will generate an email with 84 rows, most of them empty and not relevant.编写此脚本的方式将生成一个包含 84 行的 email,其中大部分为空且不相关。 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.您可以使用sheet.getLastRow()获取工作表中包含数据的最后一行的索引。

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将 AllValues 减少到只有那些第 6 列不是空的

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:)我知道你对编码太天真了,而且 Amit 是一个忙碌的人,所以为了帮助你,我将他提供的代码插入到你的代码中并稍作更正,这样你就可以复制整个代码:)

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");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM