google-apps-script - OnChange 在某些工作表中更改时触发,而不是在工作簿的任何工作表中
问题描述
我在工作表文档中有一个名为“Notif Inv”的选项卡,该选项卡还有其他几个选项卡。这是一个只有 2 行的选项卡,其中第二行使用来自工作表的另一个选项卡的范围的最新过滤行进行更新。
另一方面,我有这个函数发送电子邮件,从我的“Notif Inv”选项卡的第二行获取数据。
function Email_NewInvs() {
// Get the sheet where the data is, in sheet 'Notif Inv'
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Notif Inv");
var startRow = 2; // First row of data to process since there is a header row
var numRows = sheet.getRange(1,5).getValue(); // Number of rows to process is set by a formula which counts rows
// Fetch the range of cells A2:B6 where the emails and messages are
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range to input into the mailing system
var data = dataRange.getValues();
// This processes the emails you want to send
for (i in data) { var row = data[i];
var emailAddress = row[0]; // First column is the email address
var message = row[1]; // Second column is the message
var subject = "Nueva Factura"; // This is the subject of the email
// This parses the data for the email to send
MailApp.sendEmail(emailAddress, subject, message);
}
}
我的目标是在“Notif Inv”选项卡中的第二行自动更改时触发此功能。
我曾尝试为此脚本使用 onChange 触发器,但它实际上会在所有工作表文档中发生任何更改时触发它,这意味着在选项卡中所做的更改也与此无关。
我也尝试将函数名称更改为 onChange(e),但是当在“Notif Inv”选项卡中发生更改时,它不会执行任何操作。
有什么解决方法吗?
非常感谢
解决方案
要求:
onChange
仅当工作表“Notif Inv”发生更改时,才应运行使用触发器的脚本。
解决方案:
将更改事件传递给您的函数并使用getName()
工作表来确定正在编辑哪个工作表,然后if
根据结果运行语句。
例子:
function Email_NewInvs(e) {
// Get the sheet where the data is, in sheet 'Notif Inv'
var sheet = e.source.getActiveSheet();
if (sheet.getName() === 'Notif Inv') {
var startRow = 2; // First row of data to process since there is a header row
var numRows = sheet.getRange(1,5).getValue(); // Number of rows to process is set by a formula which counts rows
// Fetch the range of cells A2:B6 where the emails and messages are
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range to input into the mailing system
var data = dataRange.getValues();
// This processes the emails you want to send
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column is the email address
var message = row[1]; // Second column is the message
var subject = "Nueva Factura"; // This is the subject of the email
// This parses the data for the email to send
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
解释:
我们现在将事件对象与您的函数一起使用Email_NewInvs(e)
来定义var sheet
,以便我们可以检查哪个工作表已更改:
var sheet = e.source.getActiveSheet();
我添加了一条if
语句来检查已更改的工作表的名称:
if (sheet.getName() === 'Notif Inv') {
所以现在脚本只有在被更改的工作表的名称与“Notif Inv”匹配时才会运行。
笔记:
- 您需要为此脚本设置一个可安装触发器,以便它有足够的权限来发送电子邮件。
- 您将无法从脚本编辑器手动运行此脚本,它会在工作表更改时自动运行。
参考:
推荐阅读
- mysql - 是否有更好的方法使用 GROUP_CONCAT() 进行此特定查询?
- php - phpWord 将图表添加到模板上的变量指定的位置
- java - 如何将 JSON 中的列表对象放入 MySQL
- python - 如何使用 Python 将格式化的“epi-week”转换为日期?
- python-3.x - 如何修复错误“模块'cv2.cv2'没有属性setMouseCallBack?”
- asp.net-core - 在 Azure Devops 上部署 Angular Universal SSR + .NET Core
- java - WCF Java 客户端与带有证书签名的 wsHttpBinding 的服务器通信
- scala - 火花流加入kafka主题
- powershell - 在 PowerShell 中管道时 $_ 是如何工作的?
- pyinstaller - 如何使用 PyInstaller 使打包的应用程序更小