[英]How to send several rows of google sheet table via email if cell match today's date
每天,我必須通過電子郵件將不同數量的Google工作表表格行(取決於所離開的卡車數量)發送給一些收件人。
請提供一個示例,該示例如何在單元格與當前日期匹配的情況下收集幾行google表,然后通過電子郵件發送給我?
例如,我需要獲取當天日期的所有行,並通過電子郵件從A,B,C,E列發送數據。
感謝您的任何幫助:)
讓我們一步一步走。 我假設列A包含日期。 如果不正確,請更改它:
function main() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sheet.getActiveSheet();
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("A2:E"+ (row_count + 1)).getValues(); //We get all the values from A to E and from row 2 to the last one.
countToday(sheet, today, row_count, data);
sendEmails(data);
}
function countToday(sheet,today, row, data){
var lastrow = "A" + (row + 1);
var col_A = sheet.getRange('A2:'+lastrow).getValues();
for (var i = row; i >= 0; i--){
if (col_A[i] != today){
data.splice(i, 1); //
}
}
}
function sendEmails(data){
MailApp.sendEmail({
to: "example1@mail.com" + "example2@mail.com", //You can put as many emails you want
subject: "Example",
htmlBody:"<html><body>" + createTable(data)+ "</body></html>"});
}
data
(即每個工作表單元格)中分離出每個元素,然后將其添加到變量的末尾。 </ td>將為我們創建行。 function createTable(data){
var cells = [];
var table = "<html><body><br><table border=1><tr><th>Date</th><th>Column B</th><th>Column C</th><th>Column D</th><th>Column E</tr></br>";
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>";
}
}
table=table+"</table></body></html>";
return table;
}
這是我的代碼示例。 但是我不知道如何實現防止重復發送郵件的功能。 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: "xxxxxxxxxx@gmail.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: "xxxxxxx@gmail.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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.