下面提到的是我的 ss 链接,它有“Sheet1”,其中有数据。 每当从下拉列表 (E2) 中选择电子邮件地址并将复选框选中为 true(F2) 时,需要将 A、B 和 C 列 (A1:C) 中存在的数据发送到所选电子邮件地址。 注意:可能有 1 个条目或多个可用条目,需要发送 CHECK 时可用的所有数据。
提前致谢。 SS链接:https://docs.google.com/spreadsheets/d/1tmnDOMyupjeO8d65qHQsxb5KrrKeq7pMYIE8h2VmEJ4/edit?usp=sharing
最佳答案
我相信您的目标如下。
- 您的电子邮件地址和复选框分别位于单元格“E2”和“F2”中。
- 选中该复选框后,您希望检索电子邮件地址以及“A”到“C”列中的值,并希望将这些值作为电子邮件发送。
- 您想要将值转换为 HTML 表格。
在这种情况下,下面的示例脚本怎么样?
示例脚本:
此脚本由安装的 OnEdit 触发器运行。所以,please install OnEdit trigger to the function installedOnEdit
。并且,当您想对此进行测试时,请选中“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();
}
引用文献:
添加:
关于您的以下第三个问题,
,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
例如,下面的修改如何?
修改后的脚本:
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);
}
关于google-apps-script - Google 表 : On checkbox click, 表中的数据应发送至所选电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72611684/