google-sheets - 如何在 Google 表格中运行动态脚本(实时编辑)
问题描述
我正在尝试运行一个脚本,该脚本将在复选框标记为“TRUE”时执行并向客户发送电子邮件。我无法让触发器工作,我似乎也可以自己做。我把代码放在下面。请帮忙。
'''code'''
***Email Function
function SendEmail(i) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//var lr= ss.getLastRow();
//for (var i = 2; i<=lr;i++){
var currentEmail= ss.getRange(i, 1).getValue();
//logger.log(currentEmail);
var currentADID= ss.getRange(i, 3).getValue();
MailApp.sendEmail(currentEmail,"Customer Ready"+ currentADID,"This customer should be ready to schedule and installtion date and time" );
}
Trigger Function
function CheckCD() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr= ss.getLastRow();
for (var i = 2; i<=lr;i++){
var currentDCheck= ss.getRange(i, 4).getValue();
var x= onEdit():
if (currentDCheck == true)
SendEmail(i)
}
}***
解决方案
您实际上误解了如何使用onEdit
触发器,尽管由于需要获得许可,在这种情况下您需要安装触发器MailApp.sendEmail
无论如何,您需要使用事件对象 ( e
),因为它包含有关已编辑单元格属性的详细信息。请参见下面的代码:
代码:
// Trigger function, install it under "Triggers" tab as you need permission for MailApp
function CheckCD(e) {
// get the row and column of the edited cell
var row = e.range.getRow();
var column = e.range.getColumn();
// proceed only if edited cell's range is D2:D and value is "TRUE"
if (row > 1 && column == 4 && e.value == "TRUE") {
// get the sheet where the cell was edited
var ss = e.source.getActiveSheet();
// get Email and ADID of the same row where checkbox was ticked
var currentEmail = ss.getRange(row, 1).getValue();
var currentADID = ss.getRange(row, 3).getValue();
MailApp.sendEmail(currentEmail, "Customer Ready" + currentADID, "This customer should be ready to schedule and installtion date and time");
}
}
安装触发器:
- 确保选择
On edit
事件类型并选择要运行的功能。(在这种情况下,CheckCD
)
样本数据:
输出:
笔记:
- 该脚本的行为是,每当有人选中 range 上的复选框
D2:D
并且结果值为TRUE
时,它就会发送一封电子邮件。(因为您似乎每行都有不同的电子邮件,但如果没有,请将它们全部发送到一封电子邮件中)
参考:
推荐阅读
- python - 如何在单个 jupyter 单元格中显示多个 pandas describe() 输出?
- java - 如何从 Java 8 中的对象列表中获取两个属性的乘积
- php - Symfony Panther 中 getInternalResponse 的 getHeader 总是返回一个空数组
- python - 为 Dataframe.apply() 提供数据类型
- android - 什么时候会创建 Android 包私有目录?
- javascript - 验证不适用于材料 ui textinput
- c - 不完全确定我的回报做错了什么
- c# - 如何通过在 Unity 运行时设置远程目录加载路径来通过可寻址加载模型?
- html - TR 不移动到下一行
- javascript - HTML/CSS 单键输入