0% found this document useful (0 votes)
12 views7 pages

HODD

This document contains a Google Apps Script function that sends an email with data from multiple Google Sheets formatted as HTML tables. The email is sent to a list of recipients and includes various sales reports and summaries. The script defines functions to convert specific ranges of data into HTML tables for inclusion in the email body.

Uploaded by

kunal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

HODD

This document contains a Google Apps Script function that sends an email with data from multiple Google Sheets formatted as HTML tables. The email is sent to a list of recipients and includes various sales reports and summaries. The script defines functions to convert specific ranges of data into HTML tables for inclusion in the email body.

Uploaded by

kunal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

function sendEmailWithSheetsAsTables() {

// Replace with the recipient's email address


var subject = "Google Sheets Data as Tables";
var recipients= [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
]

// Get the active spreadsheet


var ss = SpreadsheetApp.getActiveSpreadsheet();

// Get the specific sheets by name


var detailsSheet = ss.getSheetByName("HOD mail body");
var dmoSheet = ss.getSheetByName("Hod mail body PDF 2");
var productSheet = ss.getSheetByName("HOD Product mail PDF");

// Define the ranges you want to send (adjust as needed)


var range1 = detailsSheet.getRange('B4:V9'); // Adjust the range to fit your data
// Get the range from A9 to the last row with data in column A
var lastRow = dmoSheet.getLastRow(); // Get the last row with data
var range2 = dmoSheet.getRange('A9:V' + lastRow); // Create the range from A9 to
the last row

var range11 = detailsSheet.getRange('B12:V18');


var range12 = detailsSheet.getRange('B21:V35');
var range14 = detailsSheet.getRange('D38:Q44');
// Convert the ranges to HTML tables using separate functions for each sheet
var htmlTable1 = rangeToHtmlTable1(range1); // For "HOD mail body"
var htmlTable2 = rangeToHtmlTable2(range2); // For "Hod mail body PDF 2" (with
green background)
var htmlTable11 = rangeToHtmlTable1(range11);
var htmlTable12 = rangeToHtmlTable1(range12);
var htmlTable14 = rangeToHtmlTable14(range14);
var productHeaders = ["Product", "Volume", "Value In Crs", "Volume", "Value In
Crs", "Volume", "Value in Crs", "Volume", "Value In Crs", "Volume", "Value In
Crs","Volume", "Value In Crs"];
var productData = productSheet.getDataRange().getValues()
.slice(6) // Skip the first 6 rows (indexes 0 to 5)

.map(row => row.slice(3, 16)); // Select columns from index 3 to 15

// Adding the merged row above headers


var productTableRows = `<tr>
<td style="border:1px solid #ddd; padding:5px;"></td>

<td colspan="2" style="border:1px solid #ddd; padding:5px; text-align:center;


background-color:#f4cccc;">Budget for the month</td>
<td colspan="2" style="border:1px solid #ddd; padding:5px; text-align:center;
background-color:#d9ead3;">Forecast of the month</td>
<td colspan="6" style="border:1px solid #ddd; padding:5px; text-align:center;
background-color:#d9ead3;">MTD</td>
<td colspan="2" style="border:1px solid #ddd; padding:5px; text-align:center;
background-color:#d9ead3;">Balance against Forecast</td>
</tr>`;
productTableRows += `<tr>
<td style="border:1px solid #ddd; padding:5px;"></td>
<td colspan="2" style="border:1px solid #ddd; padding:5px;"></td>
<td colspan="2" style="border:1px solid #ddd; padding:5px;"></td>
<td colspan="2" style="border:1px solid #ddd; padding:5px; text-
align:center;background-color:#fff2cc;">Sales</td>
<td colspan="2" style="border:1px solid #ddd; padding:5px; text-align:center;
background-color:#f4cccc;">Pending</td>
<td colspan="2" style="border:1px solid #ddd; padding:5px; text-align:center;
background-color:#d9ead3;">Total</td>
<td colspan="2" style="border:1px solid #ddd; padding:5px;"></td>
</tr>`;
// Adding headers row
productTableRows += `<tr style="background-color:#d9ead3; color:black;">
${productHeaders.map(h => `<th>${h}</th>`).join('')}
</tr>`;

// Adding data rows


productData.forEach((row, rowIndex) => {
productTableRows += `<tr style="${rowIndex === productData.length - 1 ?
'background-color: ##ddd;' : ''}">
${row.map((d, i) => {
if (i >= 2 && i <= 14 ) {
return `<td style="border:1px solid #ddd; padding:5px;text-align:center;">$
{parseFloat(d).toFixed(2)}</td>`;
} else if (i === 100 ) {
return `<td style="border:1px solid #ddd; padding:5px;text-align:center;">$
{(parseFloat(d) * 100).toFixed(0)}%</td>`;
} else {
return `<td style="border:1px solid #ddd; padding:5px;">${d}</td>`;
}
}).join('')}
</tr>`;
});

// Create the email body


var emailBody = `
<div style="font-family:Arial, sans-serif; padding:20px; background:#dfe4df00;">
<h2 style="color:#1b5e20;">
Daily Sales Report <span style="font-size: 0.7em;">(Value in Lacs)</span>
</h2>

<h3>MTD Sales</h3>
${htmlTable1}
<h3></h3>
${htmlTable11}
<h3></h3>
${htmlTable12}
<h3></h3>
${htmlTable14}
<h3>Category Sales</h3>
${htmlTable2}

<h3>Product Summary</h3>
<table style="border-collapse: collapse; width: 80%; border: 2px solid black;">
${productTableRows}
</table>

<p style="font-weight: bold; text-align: center; margin-top: 20px;">DSR - Daily


Sales Report</p>

<p style="margin-top: 20px;">Best Regards,<br>


<strong>Team Commercial Excellence (EWIN)</strong></p>
</div>
`;

recipients.forEach(function(emailAddress) {

console.log(recipients)
console.log(emailAddress)
// Send the email
MailApp.sendEmail({
to: emailAddress,
subject: "DSR MTD - " + new Date().toLocaleDateString('en-GB'),
htmlBody: emailBody
});})
}

// Function to convert range into HTML table for "HOD mail body" sheet
function rangeToHtmlTable1(range) {

var values = range.getValues();


var numRows = values.length;
var numCols = values[0].length;
var htmlTable = "<table border='1' cellpadding='5' cellspacing='0' style='border-
collapse: collapse; border: 1px solid black;'>";

// Header row (merged cells)


htmlTable += "<tr>";
// First 9 cells are merged and left blank
htmlTable += "<th colspan='6'></th>";
// Next 3 cells are merged with "PENDING" and pink background
htmlTable += "<th colspan='3' style='background-color: pink;'>PENDING</th>";
// Next 6 cells are merged and left blank
htmlTable += "<th colspan='8'></th>";
// Last 4 cells are merged with "SM" and green background
htmlTable += "<th colspan='4' style='background-color: green;'>SM</th>";
htmlTable += "</tr>";

// Add the data rows


for (var row = 1; row < numRows; row++) {
// Check if the first column is empty and set the background color for the row
var rowStyle = "";
if (values[row][0] === "" || values[row][0] == null) {
rowStyle = "background-color: #d4edda;"; // Green background for empty first
column
}

htmlTable += "<tr style='" + rowStyle + "'>";

for (var col = 0; col < numCols; col++) {


var cellValue = values[row][col];
var formattedValue = cellValue;

// Round off numbers to 2 decimal places if it's a number


if (typeof cellValue === 'number') {
formattedValue = cellValue.toFixed(2);
}

// If it's the 11th column (index 10), apply a pink background


var cellStyle = "";
if (col === 7) { // 11th cell (index 10)
cellStyle = "background-color: pink;";
}

// Multiply 16th, 18th, and 21st columns by 100 and add percentage sign
(starting from 3rd row)
if (row >= 2) { // From 3rd row onward
if (col === 12 || col === 14 || col === 16|| col === 19) { // Columns 16,
18, 21 (zero-indexed 15, 17, 20)
if (typeof cellValue === 'number') {
formattedValue = (cellValue * 100).toFixed(2) + "%";
}
}
}

htmlTable += "<td style='" + cellStyle + "'>" + formattedValue + "</td>";


}

htmlTable += "</tr>";
}

htmlTable += "</table>";
return htmlTable;
}

// Function to convert range into HTML table for "Hod mail body PDF 2" sheet (with
green background for empty first column)
function rangeToHtmlTable2(range) {
var values = range.getValues();
var numRows = values.length;
var numCols = values[0].length;
var htmlTable = "<table border='1' cellpadding='5' cellspacing='0' style='border-
collapse: collapse; border: 1px solid black;'>";

// Header row (merged cells)


htmlTable += "<tr>";
// First 9 cells are merged and left blank
htmlTable += "<th colspan='9'></th>";
// Next 3 cells are merged with "PENDING" and pink background
htmlTable += "<th colspan='3' style='background-color: pink;'>PENDING</th>";
// Next 6 cells are merged and left blank
htmlTable += "<th colspan='6'></th>";
// Last 4 cells are merged with "SM" and green background
htmlTable += "<th colspan='4' style='background-color: green;'>SM</th>";
htmlTable += "</tr>";

// Add the data rows


for (var row = 1; row < numRows; row++) {
// Check if the first column is empty and set the background color for the row
var rowStyle = "";
if (values[row][0] === "" || values[row][0] == null) {
rowStyle = "background-color: #d4edda;"; // Green background for empty first
column
}

htmlTable += "<tr style='" + rowStyle + "'>";

for (var col = 0; col < numCols; col++) {


var cellValue = values[row][col];
var formattedValue = cellValue;

// Round off numbers to 2 decimal places if it's a number


if (typeof cellValue === 'number') {
formattedValue = cellValue.toFixed(2);
}

// If it's the 11th column (index 10), apply a pink background


var cellStyle = "";
if (col === 10) { // 11th cell (index 10)
cellStyle = "background-color: pink;";
}

// Multiply 16th, 18th, and 21st columns by 100 and add percentage sign
(starting from 3rd row)
if (row >= 2) { // From 3rd row onward
if (col === 15 || col === 17 || col === 20) { // Columns 16, 18, 21 (zero-
indexed 15, 17, 20)
if (typeof cellValue === 'number') {
formattedValue = (cellValue * 100).toFixed(2) + "%";
}
}
}

htmlTable += "<td style='" + cellStyle + "'>" + formattedValue + "</td>";


}

htmlTable += "</tr>";
}
htmlTable += "</table>";
return htmlTable;
}

function rangeToHtmlTable14(range) {
var values = range.getValues();
var numRows = values.length;
var numCols = values[0].length;
var htmlTable = "<table border='1' cellpadding='5' cellspacing='0' style='border-
collapse: collapse; border: 1px solid black;'>";

// Header row (merged cells)


htmlTable += "<tr>";
// First 9 cells are merged and left blank
htmlTable += "<th colspan='1'>MTD</th>";
// Next 3 cells are merged with "PENDING" and pink background
htmlTable += "<th colspan='9' style='background-color: pink;'>REVENUE</th>";
// Next 6 cells are merged and left blank

htmlTable += "<th colspan='4' style='background-color: green;'>SM</th>";


htmlTable += "</tr>";

// Add the data rows


for (var row = 1; row < numRows; row++) {
// Check if the first column is empty and set the background color for the row
var rowStyle = "";
if (values[row][0] === "" || values[row][0] == null) {
rowStyle = "background-color: #d4edda;"; // Green background for empty first
column
}

htmlTable += "<tr style='" + rowStyle + "'>";

for (var col = 0; col < numCols; col++) {


var cellValue = values[row][col];
var formattedValue = cellValue;

// Round off numbers to 2 decimal places if it's a number


if (typeof cellValue === 'number') {
formattedValue = cellValue.toFixed(2);
}

// If it's the 11th column (index 10), apply a pink background


var cellStyle = "";
if (col === 10) { // 11th cell (index 10)
cellStyle = "background-color: WHITE;";
}

// Multiply 16th, 18th, and 21st columns by 100 and add percentage sign
(starting from 3rd row)
if (row >= 2) { // From 3rd row onward
if (col === 7 || col === 9 || col === 12) { // Columns 16, 18, 21 (zero-
indexed 15, 17, 20)
if (typeof cellValue === 'number') {
formattedValue = (cellValue * 100).toFixed(2) + "%";
}
}
}
htmlTable += "<td style='" + cellStyle + "'>" + formattedValue + "</td>";
}

htmlTable += "</tr>";
}

htmlTable += "</table>";
return htmlTable;
}

You might also like