Вот мой пример кода.Но я не могу понять, как реализовать функцию предотвращения дублирования отправки писем.EMAIL_SENT работает только с циклом for.Но в этом случае, если ячейка не содержит строку «EMAIL_SENT», электронное письмо отправляется столько раз, сколько пустых ячеек.
function main() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sheet.getSheetByName("HR-SI B2C");
var row_count = sheet.getLastRow() - 1;
var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the
date format you prefer
var data = sheet.getRange("A3:K"+ (row_count + 1)).getValues(); //We get all the
values from A to E and from row 2 to the last one.
var lastrow = "A" + (row_count + 1);
var col_A = sheet.getRange('A3:'+lastrow).getDisplayValues();
var row = data[i];
for (var i = row_count; i >= 0; i--){
if (col_A[i] != today){
data.splice(i, 1);
}
}
if (("K" + (row_count+2)) == "EMAIL_SENT"){
}else if(("D" + (row_count)) == "" || ("F" + (row_count)) == ""){
dontSendEmail();
}else{
sendEmailsQHNBRGR(data);
}
}
//-----------------------------------------------------------------------------------
function dontSendEmail(){
var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the
date format you prefer
MailApp.sendEmail({
to: "dymitrian.janowy@orsay.com", //You can put as many emails you want
subject: "Brak wpisanych ilości dla HR i SI za " + today,
htmlBody:"<html><body> Witam,<br><br>" +
"<span><b>Proszę bardzo wpisać ilości dla HR i SI do tabeli</b></span>" +
"</body></html>"});
}
//-----------------------------------------------------------------------------------
function sendEmailsQHNBRGR(data, platesNum){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sheet.getSheetByName("HR-SI B2C");
var row_count = sheet.getLastRow() - 1;
var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the date format you prefer
var data = sheet.getRange("A3:G"+ (row_count + 1)).getDisplayValues(); //We get all the values from A to D and from row 2 to the last one.
var cells = [];
var table = "<html><body><table border=3><tr><th>Date</th><th>Day</th><th>HR [pcs]</th><th>HR [ep]</th><th>SI [pcs]</th><th>SI [ep]</th></tr>";
var lastrow = "A" + (row_count + 1);
var col_A = sheet.getRange('A3:'+lastrow).getDisplayValues();
MailApp.sendEmail({
to: "dymitrian.janowy@orsay.com", //You can put as many emails you want
subject: "(HR+SI) Orsay prealert " + today,
htmlBody:"<html><body> Dear All,<br><br>" +
"<span style=background-color:rgb(217,234,211)><b>Today we've sent e-com goods in listed quantity:</b></span>" +
createTable(data) + "<br>" +
"<span style=background-color:rgb(255,242,204)><b>@DPD team, could you pick it up
tomorrow at 02:00 PM?</b></span><br>" +
"</body></html>"});
}
//-----------------------------------------------------------------------------------
function createTable(data){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sheet.getSheetByName("HR-SI B2C");
var row_count = sheet.getLastRow() - 1;
var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the date format you prefer
var data = sheet.getRange("A3:G"+ (row_count + 1)).getDisplayValues(); //We get all the values from A to D and from row 2 to the last one.
var cells = [];
var table = "<html><body><table border=3><tr><th>Date</th><th>Day</th>
<th>Plates number</th><th>HR [pcs]</th><th>HR [ep]</th><th>SI [pcs]</th>
<th>SI [ep]</th></tr>";
var lastrow = "A" + (row_count + 1);
var col_A = sheet.getRange('A3:'+lastrow).getDisplayValues();
for (var i = row_count; i >= 0; i--){
if (col_A[i] != today){
data.splice(i, 1);
}
}
for (var i = 0; i < data.length; i++){
cells = data[i].toString().split(",");
table = table + "<tr></tr>";
for (var u = 0; u < cells.length; u++){
table = table + "<td>"+ cells[u] +"</td>";
sheet.getRange("K3:K"+ (row_count + 1)).setValue("EMAIL_SENT");
}
}
table=table+"</table></body></html>";
return table;
}
}