function CollatePendingTasks(){
//Define spreadsheet and source data sheet
var spst = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spst.getSheetByName("Calibration Data"); // Insert the name of your Data Sheet source
// define two weeks from today
var currentDate = new Date();
var afterTwoWeeksDate = currentDate;
afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate()+14);
// define one month from today
var newCurrentDate = new Date();
var afterOneMonthDate = newCurrentDate;
afterOneMonthDate.setDate(afterOneMonthDate.getDate()+30);
// define dataset parameters
var startRow =2;
var strtCol = 1;
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
//select data range
var dataRange = dataSheet.getRange(startRow,strtCol,lastRow-1,lastCol);
var data = dataRange.getValues();
// helper parameters
var expirationDateCol = 7;
var checkTwoWeekExpire = 1;
var checkOneMonthExpire = 1;
// go through the enitre range to filter dataset into two required groups
for (var i = 0; i < data.length; i++){
if(data[i][expirationDateCol-1]<=afterTwoWeeksDate){
checkTwoWeekExpire = checkTwoWeekExpire + 1;
if(checkTwoWeekExpire == 2){
var twoWeekSheet = spst.insertSheet();
twoWeekSheet.setName("Expire Within Two Weeks");
var headerRow = dataSheet.getRange(1,1,1,lastCol);
headerRow.copyTo(twoWeekSheet.getRange("A1"));
}
var twoWeekData = dataSheet.getRange(i+2,1,1,lastCol);
twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire,1));
}
if(data[i][expirationDateCol-1]<=afterOneMonthDate){
checkOneMonthExpire = checkOneMonthExpire + 1;
if(checkOneMonthExpire == 2){
var oneMonthSheet = spst.insertSheet();
oneMonthSheet.setName("Expire Within One Month");
var newHeaderRow = dataSheet.getRange(1,1,1,lastCol);
newHeaderRow.copyTo(oneMonthSheet.getRange("A1"));
}
var oneMonthData = dataSheet.getRange(i+2,1,1,lastCol);
oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire,1));
}
}
// define helper data sheets for filter groups
var ss1 = spst.getSheetByName("Expire Within two Weeks");
var ss2 = spst.getSheetByName('Expire Within One Month');
// define gmail parameters
var subject = "Machine Calibration Expriation";
var recipient = "[email protected]"; // change the email address to your required email address
var sender = Session.getActiveUser().getEmail();
// Email the first filtered group
if(ss1){
var twLastRow = ss1.getLastRow;
var twRange = ss1.getRange(1,1,checkTwoWeekExpire,lastCol);
var twData = twRange.getValues();
var twTable = formatDataAsTable(twData);
var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable;
GmailApp.sendEmail(recipient, subject, "", {
htmlBody: twBody,
from: sender
});
spst.deleteSheet(ss1);
}
// Email the second filtered group
if(ss2){
var omLastRow = ss2.getLastRow;
var omRange = ss2.getRange(1,1,checkOneMonthExpire,lastCol);
var omData = omRange.getValues();
var omTable = formatDataAsTable(omData);
var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable;
GmailApp.sendEmail(recipient, subject, "", {
htmlBody: omBody,
from: sender
});
spst.deleteSheet(ss2);
}
}
// Helper function to format the filtered data in tabular format
function formatDataAsTable(data) {
var table = "<table style='border-collapse: collapse;'>";
for (var i = 0; i < data.length; i++) {
table += "<tr>";
for (var j = 0; j < data[i].length; j++) {
var cellValue = data[i][j];
if (cellValue instanceof Date) {
cellValue = formatDate(cellValue);
}
if (i === 0) {
table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>";
} else {
table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>";
}
}
table += "</tr>";
}
table += "</table>";
return table;
}
// Helper function to format date in mm/dd/yyyy format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy");
return formattedDate;
}
function CollatePendingTasks() {
//Define spreadsheet and source data sheet
var spst = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spst.getSheetByName("Calibration Data"); // Insert the name of your Data Sheet source
// define yesterday
var pastCurrentDate = new Date();
var beforeDate = pastCurrentDate;
beforeDate.setDate(beforeDate.getDate() - 1);
// define two weeks from today
var currentDate = new Date();
var afterTwoWeeksDate = currentDate;
afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate() + 14);
// define one month from today
var newCurrentDate = new Date();
var afterOneMonthDate = newCurrentDate;
afterOneMonthDate.setDate(afterOneMonthDate.getDate() + 30);
// define dataset parameters
var startRow = 2;
var strtCol = 1;
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
var expirationDateCol = 7; // i used due date in between cells not last cell considered this parameter and defined cell number
//select data range
var dataRange = dataSheet.getRange(startRow, strtCol, lastRow - 1, lastCol);
var data = dataRange.getValues();
// helper parameters
var checkTwoWeekExpire = 1;
var checkOneMonthExpire = 1;
var checkbeforeYesterday = 1;
// go through the enitre range to filter dataset into two required groups
for (var i = 0; i < data.length; i++) {
if (data[i][expirationDateCol - 1] <= beforeDate) {
checkbeforeYesterday = checkbeforeYesterday + 1;
if (checkbeforeYesterday == 2) {
var pastDateSheet = spst.insertSheet();
pastDateSheet.setName("Already Expired");
var headerRow = dataSheet.getRange(1, 1, 1, lastCol);
headerRow.copyTo(pastDateSheet.getRange("A1"));
}
var expiredData = dataSheet.getRange(i + 2, 1, 1, lastCol);
expiredData.copyTo(pastDateSheet.getRange(checkbeforeYesterday, 1));
}
if (data[i][expirationDateCol - 1] <= afterTwoWeeksDate && data[i][expirationDateCol - 1] > beforeDate) {
checkTwoWeekExpire = checkTwoWeekExpire + 1;
if (checkTwoWeekExpire == 2) {
var twoWeekSheet = spst.insertSheet();
twoWeekSheet.setName("Expire Within Two Weeks");
var headerRow = dataSheet.getRange(1, 1, 1, lastCol);
headerRow.copyTo(twoWeekSheet.getRange("A1"));
}
var twoWeekData = dataSheet.getRange(i + 2, 1, 1, lastCol);
twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire, 1));
}
if (data[i][expirationDateCol - 1] <= afterOneMonthDate && data[i][expirationDateCol - 1] > afterTwoWeeksDate) {
checkOneMonthExpire = checkOneMonthExpire + 1;
if (checkOneMonthExpire == 2) {
var oneMonthSheet = spst.insertSheet();
oneMonthSheet.setName("Expire Within One Month");
var newHeaderRow = dataSheet.getRange(1, 1, 1, lastCol);
newHeaderRow.copyTo(oneMonthSheet.getRange("A1"));
}
var oneMonthData = dataSheet.getRange(i + 2, 1, 1, lastCol);
oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire, 1));
}
}
// define helper data sheets for filter groups
var ss1 = spst.getSheetByName("Expire Within two Weeks");
var ss2 = spst.getSheetByName('Expire Within One Month');
var ss3 = spst.getSheetByName("Already Expired");
// define gmail parameters
var subject = "Machine Calibration Expriation";
var recipient = "[email protected]"; // change the email address to your required email address
var sender = Session.getActiveUser().getEmail();
// Email the first filtered group
if (ss1) {
var twLastRow = ss1.getLastRow;
var twRange = ss1.getRange(1, 1, checkTwoWeekExpire, lastCol);
var twData = twRange.getValues();
var twTable = formatDataAsTable(twData);
var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable;
GmailApp.sendEmail(recipient, subject, "", {
htmlBody: twBody,
from: sender
});
spst.deleteSheet(ss1);
}
// Email the second filtered group
if (ss2) {
var omLastRow = ss2.getLastRow;
var omRange = ss2.getRange(1, 1, checkOneMonthExpire, lastCol);
var omData = omRange.getValues();
var omTable = formatDataAsTable(omData);
var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable;
GmailApp.sendEmail(recipient, subject, "", {
htmlBody: omBody,
from: sender
});
spst.deleteSheet(ss2);
}
if (ss3) {
var uwLastRow = ss3.getLastRow;
var uwRange = ss3.getRange(1, 1, checkbeforeYesterday, expirationDateCol);
var uwData = uwRange.getValues();
var uwTable = formatDataAsTable(uwData);
var uwBody = "Machine Calibration Expired,Top Priority." + uwTable;
GmailApp.sendEmail(recipient, subject, "", {
htmlBody: uwBody,
from: sender
});
spst.deleteSheet(ss3);
}
}
// Helper function to format the filtered data in tabular format
function formatDataAsTable(data) {
var table = "<table style='border-collapse: collapse;'>";
for (var i = 0; i < data.length; i++) {
table += "<tr>";
for (var j = 0; j < data[i].length; j++) {
var cellValue = data[i][j];
if (cellValue instanceof Date) {
cellValue = formatDate(cellValue);
}
if (i === 0) {
table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>";
} else {
table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>";
}
}
table += "</tr>";
}
table += "</table>";
return table;
}
// Helper function to format date in mm/dd/yyyy format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy");
return formattedDate;
}
function CollatePendingTasks() {
//Define spreadsheet and source data sheet
var spst = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spst.getSheetByName("Calibration Data"); // Insert the name of your Data Sheet source
// define yesterday
var pastCurrentDate = new Date();
var beforeDate = pastCurrentDate;
beforeDate.setDate(beforeDate.getDate() - 1);
// define two weeks from today
var currentDate = new Date();
var afterTwoWeeksDate = currentDate;
afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate() + 14);
// define one month from today
var newCurrentDate = new Date();
var afterOneMonthDate = newCurrentDate;
afterOneMonthDate.setDate(afterOneMonthDate.getDate() + 30);
// define dataset parameters
var startRow = 3;
var strtCol = 1;
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
var expirationDateCol = 7; // i used due date in between cells not last cell considered this parameter and defined cell number
//select data range
var dataRange = dataSheet.getRange(startRow, strtCol, lastRow - 1, lastCol);
var data = dataRange.getValues();
// helper parameters
var checkTwoWeekExpire = 1;
var checkOneMonthExpire = 1;
var checkbeforeYesterday = 1;
// go through the enitre range to filter dataset into two required groups
for (var i = 0; i < data.length; i++) {
if (data[i][expirationDateCol - 1] <= beforeDate) {
checkbeforeYesterday = checkbeforeYesterday + 1;
if (checkbeforeYesterday == 2) {
var pastDateSheet = spst.insertSheet();
pastDateSheet.setName("Already Expired");
var headerRow = dataSheet.getRange(1, 1, 2, lastCol);
headerRow.copyTo(pastDateSheet.getRange("A1"));
}
var expiredData = dataSheet.getRange(i + 2, 1, 1, lastCol);
expiredData.copyTo(pastDateSheet.getRange(checkbeforeYesterday+1, 1));
}
if (data[i][expirationDateCol - 1] <= afterTwoWeeksDate && data[i][expirationDateCol - 1] > beforeDate) {
checkTwoWeekExpire = checkTwoWeekExpire + 1;
if (checkTwoWeekExpire == 2) {
var twoWeekSheet = spst.insertSheet();
twoWeekSheet.setName("Expire Within Two Weeks");
var headerRow = dataSheet.getRange(1, 1, 2, lastCol);
headerRow.copyTo(twoWeekSheet.getRange("A1"));
}
var twoWeekData = dataSheet.getRange(i + 2, 1, 1, lastCol);
twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire+1, 1));
}
if (data[i][expirationDateCol - 1] <= afterOneMonthDate && data[i][expirationDateCol - 1] > afterTwoWeeksDate) {
checkOneMonthExpire = checkOneMonthExpire + 1;
if (checkOneMonthExpire == 2) {
var oneMonthSheet = spst.insertSheet();
oneMonthSheet.setName("Expire Within One Month");
var newHeaderRow = dataSheet.getRange(1, 1, 2, lastCol);
newHeaderRow.copyTo(oneMonthSheet.getRange("A1"));
}
var oneMonthData = dataSheet.getRange(i + 2, 1, 1, lastCol);
oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire+1, 1));
}
}
// define helper data sheets for filter groups
var ss1 = spst.getSheetByName("Expire Within two Weeks");
var ss2 = spst.getSheetByName('Expire Within One Month');
var ss3 = spst.getSheetByName("Already Expired");
// define gmail parameters
var subject = "Machine Calibration Expriation";
var recipient = "[email protected]"; // change the email address to your required email address
var sender = Session.getActiveUser().getEmail();
var ccMail = "[email protected]"
// Email the first filtered group
if (ss1) {
var twLastRow = ss1.getLastRow;
var twRange = ss1.getRange(1, 1, checkTwoWeekExpire, lastCol);
var twData = twRange.getValues();
var twTable = formatDataAsTable(twData);
var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable;
GmailApp.sendEmail(recipient, subject,"[email protected]", {
cc: ccMail,
htmlBody: twBody,
from: sender,
});
spst.deleteSheet(ss1);
}
// Email the second filtered group
if (ss2) {
var omLastRow = ss2.getLastRow;
var omRange = ss2.getRange(1, 1, checkOneMonthExpire, lastCol);
var omData = omRange.getValues();
var omTable = formatDataAsTable(omData);
var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: omBody,
from: sender
});
spst.deleteSheet(ss2);
}
if (ss3) {
var uwLastRow = ss3.getLastRow;
var uwRange = ss3.getRange(1, 1, checkbeforeYesterday, expirationDateCol);
var uwData = uwRange.getValues();
var uwTable = formatDataAsTable(uwData);
var uwBody = "Machine Calibration Expired,Top Priority." + uwTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: uwBody,
from: sender
});
spst.deleteSheet(ss3);
}
}
// Helper function to format the filtered data in tabular format
function formatDataAsTable(data) {
var fstRow = data[0].shift()
var table = "<table style='border-collapse: collapse;'>";
table += "<tr>";
table += "<th colspan='7' style='text-align: center; border: 1px solid black; padding: 5px;'>" + fstRow + "</th>";
table += "</tr>";
for (var i = 1; i < data.length; i++) {
table += "<tr>";
for (var j = 0; j < data[i].length; j++) {
var cellValue = data[i][j];
if (cellValue instanceof Date) {
cellValue = formatDate(cellValue);
}
if (i == 1) {
table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>";
} else {
table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>";
}
}
table += "</tr>";
}
table += "</table>";
return table;
}
// Helper function to format date in mm/dd/yyyy format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy");
return formattedDate;
}
function CollatePendingTasks() {
//Define spreadsheet and source data sheet
var spst = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spst.getSheetByName("Calibration Data"); // Insert the name of your Data Sheet source
// define yesterday
var pastCurrentDate = new Date();
var beforeDate = pastCurrentDate;
beforeDate.setDate(beforeDate.getDate() - 1);
// define two weeks from today
var currentDate = new Date();
var afterTwoWeeksDate = currentDate;
afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate() + 14);
// define one month from today
var newCurrentDate = new Date();
var afterOneMonthDate = newCurrentDate;
afterOneMonthDate.setDate(afterOneMonthDate.getDate() + 30);
// define dataset parameters
var startRow = 3;
var strtCol = 1;
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
var expirationDateCol = 7; // i used due date in between cells not last cell considered this parameter and defined cell number
//select data range
var dataRange = dataSheet.getRange(startRow, strtCol, lastRow - 1, lastCol);
var data = dataRange.getValues();
// helper parameters
var checkTwoWeekExpire = 1;
var checkOneMonthExpire = 1;
var checkbeforeYesterday = 1;
// go through the enitre range to filter dataset into two required groups
for (var i = 0; i < data.length; i++) {
if (data[i][expirationDateCol - 1] <= beforeDate) {
checkbeforeYesterday = checkbeforeYesterday + 1;
if (checkbeforeYesterday == 2) {
var pastDateSheet = spst.insertSheet();
pastDateSheet.setName("Already Expired");
var headerRow = dataSheet.getRange(1, 1, 2, lastCol);
headerRow.copyTo(pastDateSheet.getRange("A1"));
}
var expiredData = dataSheet.getRange(i + 3, 1, 1, lastCol);
expiredData.copyTo(pastDateSheet.getRange(checkbeforeYesterday+1, 1));
}
if (data[i][expirationDateCol - 1] <= afterTwoWeeksDate && data[i][expirationDateCol - 1] > beforeDate) {
checkTwoWeekExpire = checkTwoWeekExpire + 1;
if (checkTwoWeekExpire == 2) {
var twoWeekSheet = spst.insertSheet();
twoWeekSheet.setName("Expire Within Two Weeks");
var headerRow = dataSheet.getRange(1, 1, 2, lastCol);
headerRow.copyTo(twoWeekSheet.getRange("A1"));
}
var twoWeekData = dataSheet.getRange(i + 3, 1, 1, lastCol);
twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire+1, 1));
}
if (data[i][expirationDateCol - 1] <= afterOneMonthDate && data[i][expirationDateCol - 1] > afterTwoWeeksDate) {
checkOneMonthExpire = checkOneMonthExpire + 1;
if (checkOneMonthExpire == 2) {
var oneMonthSheet = spst.insertSheet();
oneMonthSheet.setName("Expire Within One Month");
var newHeaderRow = dataSheet.getRange(1, 1, 2, lastCol);
newHeaderRow.copyTo(oneMonthSheet.getRange("A1"));
}
var oneMonthData = dataSheet.getRange(i + 3, 1, 1, lastCol);
oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire+1, 1));
}
}
// define helper data sheets for filter groups
var ss1 = spst.getSheetByName("Expire Within two Weeks");
var ss2 = spst.getSheetByName('Expire Within One Month');
var ss3 = spst.getSheetByName("Already Expired");
// define gmail parameters
var subject = "Machine Calibration Expriation";
var recipient = "[email protected]"; // change the email address to your required email address
var sender = Session.getActiveUser().getEmail();
var ccMail = "[email protected]"
// Email the first filtered group
if (ss1) {
var twLastRow = ss1.getLastRow();
var twRange = ss1.getRange(1, 1, twLastRow, lastCol);
var twData = twRange.getValues();
var twTable = formatDataAsTable(twData);
var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable;
GmailApp.sendEmail(recipient, subject,"[email protected]", {
cc: ccMail,
htmlBody: twBody,
from: sender,
});
spst.deleteSheet(ss1);
}
// Email the second filtered group
if (ss2) {
var omLastRow = ss2.getLastRow();
var omRange = ss2.getRange(1, 1, omLastRow, lastCol);
var omData = omRange.getValues();
var omTable = formatDataAsTable(omData);
var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: omBody,
from: sender
});
spst.deleteSheet(ss2);
}
if (ss3) {
var uwLastRow = ss3.getLastRow();
var uwRange = ss3.getRange(1, 1, uwLastRow, expirationDateCol);
var uwData = uwRange.getValues();
var uwTable = formatDataAsTable(uwData);
var uwBody = "Machine Calibration Expired,Top Priority." + uwTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: uwBody,
from: sender
});
spst.deleteSheet(ss3);
}
}
// Helper function to format the filtered data in tabular format
function formatDataAsTable(data) {
var fstRow = data[0].shift()
var table = "<table style='border-collapse: collapse;'>";
table += "<tr>";
table += "<th colspan='7' style='text-align: center; border: 1px solid black; padding: 5px;'>" + fstRow + "</th>";
table += "</tr>";
for (var i = 1; i < data.length; i++) {
table += "<tr>";
for (var j = 0; j < data[i].length; j++) {
var cellValue = data[i][j];
if (cellValue instanceof Date) {
cellValue = formatDate(cellValue);
}
if (i == 1) {
table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>";
} else {
table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>";
}
}
table += "</tr>";
}
table += "</table>";
return table;
}
// Helper function to format date in mm/dd/yyyy format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy");
return formattedDate;
}
function UpdatedCodeForCollatePendingTasks() {
//Define spreadsheet and source data sheet
var spst = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spst.getSheetByName("Instruments & Gauges"); // Insert the name of your Data Sheet source
// define yesterday
var pastCurrentDate = new Date();
var beforeDate = pastCurrentDate;
beforeDate.setDate(beforeDate.getDate() - 1);
// define two weeks from today
var currentDate = new Date();
var afterTwoWeeksDate = currentDate;
afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate() + 14);
// define one month from today
var newCurrentDate = new Date();
var afterOneMonthDate = newCurrentDate;
afterOneMonthDate.setDate(afterOneMonthDate.getDate() + 30);
// define dataset parameters
var startRow = 2;
var strtCol = 1;
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
var expirationDateCol = 15; // i used due date in between cells not last cell considered this parameter and defined cell number
//select data range
var dataRange = dataSheet.getRange(startRow, strtCol, lastRow - 1, lastCol);
var data = dataRange.getValues();
// helper parameters
var checkTwoWeekExpire = 1;
var checkOneMonthExpire = 1;
var checkbeforeYesterday = 1;
// go through the enitre range to filter dataset into two required groups
for (var i = 0; i < data.length; i++) {
if (data[i][expirationDateCol - 1] <= beforeDate) {
checkbeforeYesterday = checkbeforeYesterday + 1;
if (checkbeforeYesterday == 2) {
var pastDateSheet = spst.insertSheet();
pastDateSheet.setName("Already Expired");
var headerRow = dataSheet.getRange(1, 1, 1, lastCol);
headerRow.copyTo(pastDateSheet.getRange("A1"));
}
var expiredData = dataSheet.getRange(i + 2, 1, 1, lastCol);
expiredData.copyTo(pastDateSheet.getRange(checkbeforeYesterday, 1));
}
if (data[i][expirationDateCol - 1] <= afterTwoWeeksDate && data[i][expirationDateCol - 1] > beforeDate) {
checkTwoWeekExpire = checkTwoWeekExpire + 1;
if (checkTwoWeekExpire == 2) {
var twoWeekSheet = spst.insertSheet();
twoWeekSheet.setName("Expire Within Two Weeks");
var headerRow = dataSheet.getRange(1, 1, 1, lastCol);
headerRow.copyTo(twoWeekSheet.getRange("A1"));
}
var twoWeekData = dataSheet.getRange(i + 2, 1, 1, lastCol);
twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire, 1));
}
if (data[i][expirationDateCol - 1] <= afterOneMonthDate && data[i][expirationDateCol - 1] > afterTwoWeeksDate) {
checkOneMonthExpire = checkOneMonthExpire + 1;
if (checkOneMonthExpire == 2) {
var oneMonthSheet = spst.insertSheet();
oneMonthSheet.setName("Expire Within One Month");
var newHeaderRow = dataSheet.getRange(1, 1, 1, lastCol);
newHeaderRow.copyTo(oneMonthSheet.getRange("A1"));
}
var oneMonthData = dataSheet.getRange(i + 2, 1, 1, lastCol);
oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire, 1));
}
}
// define helper data sheets for filter groups
var ss1 = spst.getSheetByName("Expire Within two Weeks");
var ss2 = spst.getSheetByName('Expire Within One Month');
var ss3 = spst.getSheetByName("Already Expired");
// define gmail parameters
var subject = "Machine Calibration Expriation";
var recipient = "[email protected]"; // change the email address to your required email address
var sender = Session.getActiveUser().getEmail();
var ccMail = "[email protected]"
// Email the first filtered group
if (ss1) {
var twLastRow = ss1.getLastRow();
var twRange = ss1.getRange(1, 1, twLastRow, lastCol);
var twData = twRange.getValues();
var twTable = formatDataAsTable(twData);
var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable;
GmailApp.sendEmail(recipient, subject,"[email protected]", {
cc: ccMail,
htmlBody: twBody,
from: sender,
});
spst.deleteSheet(ss1);
}
// Email the second filtered group
if (ss2) {
var omLastRow = ss2.getLastRow();
var omRange = ss2.getRange(1, 1, omLastRow, lastCol);
var omData = omRange.getValues();
var omTable = formatDataAsTable(omData);
var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: omBody,
from: sender
});
spst.deleteSheet(ss2);
}
if (ss3) {
var uwLastRow = ss3.getLastRow();
var uwRange = ss3.getRange(1, 1, uwLastRow, lastCol);
var uwData = uwRange.getValues();
var uwTable = formatDataAsTable(uwData);
var uwBody = "Machine Calibration Expired,Top Priority." + uwTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: uwBody,
from: sender
});
spst.deleteSheet(ss3);
}
}
// Helper function to format the filtered data in tabular format
function formatDataAsTable(data) {
var table = "<table style='border-collapse: collapse;'>";
for (var i = 0; i < data.length; i++) {
table += "<tr>";
for (var j = 0; j < data[i].length; j++) {
var cellValue = data[i][j];
if (cellValue instanceof Date) {
cellValue = formatDate(cellValue);
}
if (i == 0) {
table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>";
} else {
table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>";
}
}
table += "</tr>";
}
table += "</table>";
return table;
}
// Helper function to format date in mm/dd/yyyy format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy");
return formattedDate;
}
earlier itself i am getting 67 Rows X 24 Columns, can you check up please i need 150 Rows and 24 Columns Can you copy paste in your excel data upto 150Rows and see are u getting data in your mailDear Vishnukumar,
I checked your code. There were a few small discrepancies in your code. I edited those errors and tried the code for a dataset that contains 40 rows and 10 columns.
Here is the updated code for your file:
JavaScript:function UpdatedCodeForCollatePendingTasks() { //Define spreadsheet and source data sheet var spst = SpreadsheetApp.getActiveSpreadsheet(); var dataSheet = spst.getSheetByName("Instruments & Gauges"); // Insert the name of your Data Sheet source // define yesterday var pastCurrentDate = new Date(); var beforeDate = pastCurrentDate; beforeDate.setDate(beforeDate.getDate() - 1); // define two weeks from today var currentDate = new Date(); var afterTwoWeeksDate = currentDate; afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate() + 14); // define one month from today var newCurrentDate = new Date(); var afterOneMonthDate = newCurrentDate; afterOneMonthDate.setDate(afterOneMonthDate.getDate() + 30); // define dataset parameters var startRow = 2; var strtCol = 1; var lastRow = dataSheet.getLastRow(); var lastCol = dataSheet.getLastColumn(); var expirationDateCol = 15; // i used due date in between cells not last cell considered this parameter and defined cell number //select data range var dataRange = dataSheet.getRange(startRow, strtCol, lastRow - 1, lastCol); var data = dataRange.getValues(); // helper parameters var checkTwoWeekExpire = 1; var checkOneMonthExpire = 1; var checkbeforeYesterday = 1; // go through the enitre range to filter dataset into two required groups for (var i = 0; i < data.length; i++) { if (data[i][expirationDateCol - 1] <= beforeDate) { checkbeforeYesterday = checkbeforeYesterday + 1; if (checkbeforeYesterday == 2) { var pastDateSheet = spst.insertSheet(); pastDateSheet.setName("Already Expired"); var headerRow = dataSheet.getRange(1, 1, 1, lastCol); headerRow.copyTo(pastDateSheet.getRange("A1")); } var expiredData = dataSheet.getRange(i + 2, 1, 1, lastCol); expiredData.copyTo(pastDateSheet.getRange(checkbeforeYesterday, 1)); } if (data[i][expirationDateCol - 1] <= afterTwoWeeksDate && data[i][expirationDateCol - 1] > beforeDate) { checkTwoWeekExpire = checkTwoWeekExpire + 1; if (checkTwoWeekExpire == 2) { var twoWeekSheet = spst.insertSheet(); twoWeekSheet.setName("Expire Within Two Weeks"); var headerRow = dataSheet.getRange(1, 1, 1, lastCol); headerRow.copyTo(twoWeekSheet.getRange("A1")); } var twoWeekData = dataSheet.getRange(i + 2, 1, 1, lastCol); twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire, 1)); } if (data[i][expirationDateCol - 1] <= afterOneMonthDate && data[i][expirationDateCol - 1] > afterTwoWeeksDate) { checkOneMonthExpire = checkOneMonthExpire + 1; if (checkOneMonthExpire == 2) { var oneMonthSheet = spst.insertSheet(); oneMonthSheet.setName("Expire Within One Month"); var newHeaderRow = dataSheet.getRange(1, 1, 1, lastCol); newHeaderRow.copyTo(oneMonthSheet.getRange("A1")); } var oneMonthData = dataSheet.getRange(i + 2, 1, 1, lastCol); oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire, 1)); } } // define helper data sheets for filter groups var ss1 = spst.getSheetByName("Expire Within two Weeks"); var ss2 = spst.getSheetByName('Expire Within One Month'); var ss3 = spst.getSheetByName("Already Expired"); // define gmail parameters var subject = "Machine Calibration Expriation"; var recipient = "[email protected]"; // change the email address to your required email address var sender = Session.getActiveUser().getEmail(); var ccMail = "[email protected]" // Email the first filtered group if (ss1) { var twLastRow = ss1.getLastRow(); var twRange = ss1.getRange(1, 1, twLastRow, lastCol); var twData = twRange.getValues(); var twTable = formatDataAsTable(twData); var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable; GmailApp.sendEmail(recipient, subject,"[email protected]", { cc: ccMail, htmlBody: twBody, from: sender, }); spst.deleteSheet(ss1); } // Email the second filtered group if (ss2) { var omLastRow = ss2.getLastRow(); var omRange = ss2.getRange(1, 1, omLastRow, lastCol); var omData = omRange.getValues(); var omTable = formatDataAsTable(omData); var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable; GmailApp.sendEmail(recipient, subject, "", { cc: ccMail, htmlBody: omBody, from: sender }); spst.deleteSheet(ss2); } if (ss3) { var uwLastRow = ss3.getLastRow(); var uwRange = ss3.getRange(1, 1, uwLastRow, lastCol); var uwData = uwRange.getValues(); var uwTable = formatDataAsTable(uwData); var uwBody = "Machine Calibration Expired,Top Priority." + uwTable; GmailApp.sendEmail(recipient, subject, "", { cc: ccMail, htmlBody: uwBody, from: sender }); spst.deleteSheet(ss3); } } // Helper function to format the filtered data in tabular format function formatDataAsTable(data) { var table = "<table style='border-collapse: collapse;'>"; for (var i = 0; i < data.length; i++) { table += "<tr>"; for (var j = 0; j < data[i].length; j++) { var cellValue = data[i][j]; if (cellValue instanceof Date) { cellValue = formatDate(cellValue); } if (i == 0) { table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>"; } else { table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>"; } } table += "</tr>"; } table += "</table>"; return table; } // Helper function to format date in mm/dd/yyyy format function formatDate(date) { var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy"); return formattedDate; }
Click the link below to create a copy of the worksheet I used for this problem.
Best Regards,
Seemanto Saha
ExcelDemy
I just gone through the contents my problem is I have small portable equipments of 150nos all are given in date of expiry as same day, when the day reaches the limit, i need all 150 rows in mail not whatever the expired in 150, bcoz all 150 are same date calibration is happening by different team, I am getting one mail with 67rows as I mentioned earlier another mail with empty data rows and columns, i need all 150Rows in MailDear Vishnukumar,
I checked the code for 150 rows and 13 columns. After executing the code, I received the mails properly. Here is a sample mail:
View attachment 771All machines that require calibration within one month are listed properly here. Below is the link to the Spreadsheet used for this solution.
New Updated Code for Collate Pending Tasks and Send Via Mail
Regards,
Seemanto Saha
ExcelDemy
Yes Thanks Dude for the Support, I didnt notice "view entire message" in the mail mine also comingDear Vishnukumar,
I set the same expiration date for all the rows and then executed the code. Only one mail was sent. However, I got 127 complete rows and 1 partial row (like in the following image). This is because the message is clipped. We can click the View entire message command to view all 150 rows.
View attachment 806
The following video shows an overview of the process.
The link to the spreadsheet is also given below:
Regards,
Seemanto Saha
ExcelDemy
Can you check for Already expired Content for me I am getting Header Rows not below to that, it seems, its checking yesterday date only , if expired can i get details of the same, not just monitoring yesterday.Yes Thanks Dude for the Support, I didnt notice "view entire message" in the mail mine also coming
Dear Vishnukumar,Can you check for Already expired Content for me I am getting Header Rows not below to that, it seems, its checking yesterday date only , if expired can i get details of the same, not just monitoring yesterday.
function UpdatedCodeForCollatePendingTasks() {
//Define spreadsheet and source data sheet
var spst = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spst.getSheetByName("Instruments & Gauges"); // Insert the name of your Data Sheet source
// define yesterday
var pastCurrentDate = new Date();
var beforeDate = pastCurrentDate;
beforeDate.setDate(beforeDate.getDate() - 1);
// define two weeks from today
var currentDate = new Date();
var afterTwoWeeksDate = currentDate;
afterTwoWeeksDate.setDate(afterTwoWeeksDate.getDate() + 14);
// define one month from today
var newCurrentDate = new Date();
var afterOneMonthDate = newCurrentDate;
afterOneMonthDate.setDate(afterOneMonthDate.getDate() + 30);
// define dataset parameters
var startRow = 2;
var strtCol = 1;
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
var expirationDateCol = 10; // i used due date in between cells not last cell considered this parameter and defined cell number
//select data range
var dataRange = dataSheet.getRange(startRow, strtCol, lastRow - 1, lastCol);
var data = dataRange.getValues();
// helper parameters
var checkTwoWeekExpire = 1;
var checkOneMonthExpire = 1;
var checkbeforeYesterday = 1;
// go through the enitre range to filter dataset into two required groups
for (var i = 0; i < data.length; i++) {
if (data[i][expirationDateCol - 1] <= beforeDate) {
checkbeforeYesterday = checkbeforeYesterday + 1;
if (checkbeforeYesterday == 2) {
var pastDateSheet = spst.insertSheet();
pastDateSheet.setName("Already Expired");
var headerRow = dataSheet.getRange(1, 1, 1, lastCol);
headerRow.copyTo(pastDateSheet.getRange("A1"));
}
var expiredData = dataSheet.getRange(i + 2, 1, 1, lastCol);
expiredData.copyTo(pastDateSheet.getRange(checkbeforeYesterday, 1));
}
if (data[i][expirationDateCol - 1] <= afterTwoWeeksDate && data[i][expirationDateCol - 1] > beforeDate) {
checkTwoWeekExpire = checkTwoWeekExpire + 1;
if (checkTwoWeekExpire == 2) {
var twoWeekSheet = spst.insertSheet();
twoWeekSheet.setName("Expire Within Two Weeks");
var headerRow = dataSheet.getRange(1, 1, 1, lastCol);
headerRow.copyTo(twoWeekSheet.getRange("A1"));
}
var twoWeekData = dataSheet.getRange(i + 2, 1, 1, lastCol);
twoWeekData.copyTo(twoWeekSheet.getRange(checkTwoWeekExpire, 1));
}
if (data[i][expirationDateCol - 1] <= afterOneMonthDate && data[i][expirationDateCol - 1] > afterTwoWeeksDate) {
checkOneMonthExpire = checkOneMonthExpire + 1;
if (checkOneMonthExpire == 2) {
var oneMonthSheet = spst.insertSheet();
oneMonthSheet.setName("Expire Within One Month");
var newHeaderRow = dataSheet.getRange(1, 1, 1, lastCol);
newHeaderRow.copyTo(oneMonthSheet.getRange("A1"));
}
var oneMonthData = dataSheet.getRange(i + 2, 1, 1, lastCol);
oneMonthData.copyTo(oneMonthSheet.getRange(checkOneMonthExpire, 1));
}
}
// define helper data sheets for filter groups
var ss1 = spst.getSheetByName("Expire Within two Weeks");
var ss2 = spst.getSheetByName('Expire Within One Month');
var ss3 = spst.getSheetByName("Already Expired");
// define gmail parameters
var subject = "Machine Calibration Expriation";
var recipient = "[email protected]"; // change the email address to your required email address
var sender = Session.getActiveUser().getEmail();
var ccMail = "[email protected]"
// Email the first filtered group
if (ss1) {
var twLastRow = ss1.getLastRow();
var twRange = ss1.getRange(1, 1, twLastRow, lastCol);
var twData = twRange.getValues();
var twTable = formatDataAsTable(twData);
var twBody = "Machine Calibration will be expired within two weeks, plan at the earliest." + twTable;
GmailApp.sendEmail(recipient, subject,"[email protected]", {
cc: ccMail,
htmlBody: twBody,
from: sender,
});
spst.deleteSheet(ss1);
}
// Email the second filtered group
if (ss2) {
var omLastRow = ss2.getLastRow();
var omRange = ss2.getRange(1, 1, omLastRow, lastCol);
var omData = omRange.getValues();
var omTable = formatDataAsTable(omData);
var omBody = "Machine Calibration will be expired within one month, plan at the earliest." + omTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: omBody,
from: sender
});
spst.deleteSheet(ss2);
}
if (ss3) {
var uwLastRow = ss3.getLastRow();
var uwRange = ss3.getRange(1, 1, uwLastRow, lastCol);
var uwData = uwRange.getValues();
var uwTable = formatDataAsTable(uwData);
var uwBody = "Machine Calibration Expired,Top Priority." + uwTable;
GmailApp.sendEmail(recipient, subject, "", {
cc: ccMail,
htmlBody: uwBody,
from: sender
});
spst.deleteSheet(ss3);
}
}
// Helper function to format the filtered data in tabular format
function formatDataAsTable(data) {
var table = "<table style='border-collapse: collapse;'>";
for (var i = 0; i < data.length; i++) {
table += "<tr>";
for (var j = 0; j < data[i].length; j++) {
var cellValue = data[i][j];
if (cellValue instanceof Date) {
cellValue = formatDate(cellValue);
}
if (i == 0) {
table += "<th style='border: 1px solid black; padding: 5px; font-weight: bold;'>" + cellValue + "</th>";
} else {
table += "<td style='border: 1px solid black; padding: 5px;'>" + cellValue + "</td>";
}
}
table += "</tr>";
}
table += "</table>";
return table;
}
// Helper function to format date in mm/dd/yyyy format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM dd, yyyy");
return formattedDate;
}