I believe your goal is as follows.
- You have the email address and checkbox in the cells "E2" and "F2", respectively.
- When the checkbox is checked, you want to retrieve the email address and the values from the columns "A" to "C", and want to send the values as an email.
- You want to convert the values to the HTML table.
In this case, how about the following sample script?
Sample script:
This script is run by the installed OnEdit trigger. So, please install OnEdit trigger to the function installedOnEdit
. And, when you want to test this, please check the checkbox of "F2".
function installedOnEdit(e) {
const range = e.range;
const sheet = range.getSheet();
const [email, checkbox] = sheet.getRange("E2:F2").getValues()[0];
if (sheet.getSheetName() != "Sheet1" || !checkbox) return;
const values = sheet.getRange("A1:C" + sheet.getLastRow()).getValues();
const html = '<table border="1" style="border-collapse: collapse">' + values.reduce((s, r) => s += "<tr>" + r.map(c => `<td>${c}</td>`).join("") + "</tr>", "") + "</table>";
MailApp.sendEmail({ to: email, subject: "sample subject", htmlBody: html});
range.uncheck();
}
References:
Added:
About your following 3rd question,
,the given SS link may not work now, but is there any work around that only the occupied cells get sent on mail. With the above code, entire column including the black cells are getting sent on the email.
With the above code, all the data present in the range "A1:C" are being sent on the mail with the borders. But I want the table to be sent on mail only till the last data row available. This script is sending the the entire available rows i.e. A1:C1000 with borders. For e.g. If the data is available till the 3rd row i.e. A1: C3, i want only that data to be sent, instead this code is sending the entire available rows i.e. A1:C1000
For example, how about the following modification?
Modified scirpt:
function installedOnEdit(e) {
// Ref: https://stackoverflow.com/a/44563639
Object.prototype.get1stEmptyRowFromTop = function (columnNumber, offsetRow = 1) {
const range = this.getRange(offsetRow, columnNumber, 2);
const values = range.getDisplayValues();
if (values[0][0] && values[1][0]) {
return range.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() + 1;
} else if (values[0][0] && !values[1][0]) {
return offsetRow + 1;
}
return offsetRow;
};
const range = e.range;
const sheet = range.getSheet();
const [email, checkbox] = sheet.getRange("E2:F2").getValues()[0];
if (sheet.getSheetName() != "Sheet1" || !checkbox) return;
const values = sheet.getRange("A1:C" + sheet.get1stEmptyRowFromTop(1)).getValues();
const html = '<table border="1" style="border-collapse: collapse">' + values.reduce((s, r) => s += "<tr>" + r.map(c => `<td>${c}</td>`).join("") + "</tr>", "") + "</table>";
MailApp.sendEmail({ to: email, subject: "sample subject", htmlBody: html});
range.uncheck();
// This is from your 2nd question.
var dstSheet = e.source.getSheetByName("History");
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}