gpt4 book ai didi

debugging - 如何在 GAS 中测试触发功能?

转载 作者:行者123 更新时间:2023-12-05 07:30:19 32 4
gpt4 key购买 nike

Google Apps 脚本支持 Triggers ,那个通行证Events触发功能。不幸的是,开发环境将允许您在不传递参数的情况下测试函数,因此您不能以这种方式模拟事件。如果您尝试,您会收到如下错误:

ReferenceError: 'e' is not defined.

或者

TypeError: Cannot read property *...* from undefined

(其中 e 未定义)

可以将事件视为可选参数,并使用 Is there a better way to do optional function parameters in JavaScript? 中的任何技术将默认值插入触发器函数。 .但这带来了一个风险,即懒惰的程序员(如果是你,请举手!)会留下该代码,从而产生意想不到的副作用。

肯定有更好的方法吗?

最佳答案

您可以编写一个测试函数,将模拟事件传递给您的触发器函数。下面是一个测试 onEdit() 触发函数的示例。它传递一个事件对象,其中包含 Understanding Events 中“电子表格编辑事件”描述的所有信息。 .

要使用它,请在目标 onEdit 函数中设置断点,选择函数 test_onEdit 并点击 Debug

/**
* Test function for onEdit. Passes an event object to simulate an edit to
* a cell in a spreadsheet.
*
* Check for updates: https://stackoverflow.com/a/16089067/1677912
*
* See https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
*/
function test_onEdit() {
onEdit({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}

如果您好奇的话,这是为了测试 Google Spreadsheet conditional on three cellsonEdit 函数而编写的。 .

这是电子表格表单提交事件的测试函数。它通过读取表单提交数据来构建其模拟事件。这最初是为 Getting TypeError in onFormSubmit trigger? 编写的.

/**
* Test function for Spreadsheet Form Submit trigger functions.
* Loops through content of sheet, creating simulated Form Submit Events.
*
* Check for updates: https://stackoverflow.com/a/16089067/1677912
*
* See https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
*/
function test_onFormSubmit() {
var dataRange = SpreadsheetApp.getActiveSheet().getDataRange();
var data = dataRange.getValues();
var headers = data[0];
// Start at row 1, skipping headers in row 0
for (var row=1; row < data.length; row++) {
var e = {};
e.values = data[row].filter(Boolean); // filter: https://stackoverflow.com/a/19888749
e.range = dataRange.offset(row,0,1,data[0].length);
e.namedValues = {};
// Loop through headers to create namedValues object
// NOTE: all namedValues are arrays.
for (var col=0; col<headers.length; col++) {
e.namedValues[headers[col]] = [data[row][col]];
}
// Pass the simulated event to onFormSubmit
onFormSubmit(e);
}
}

提示

模拟事件时,注意尽可能匹配记录的事件对象。

  • 如果您希望验证文档,您可以记录从触发器函数接收到的事件。

    Logger.log( JSON.stringify( e , null, 2 ) );
  • 在电子表格表单提交事件中:

    • 所有 namedValues 值都是数组。
    • 时间戳是字符串,它们的格式将本地化为表单的区域设置。如果从具有默认格式* 的电子表格中读取,它们是 Date 对象。如果您的触发器函数依赖于时间戳的字符串格式(这是一个坏主意),请注意确保您适本地模拟该值。
    • 如果您的电子表格中有一些列不在您的表单中,此脚本中的技术将模拟一个包含这些附加值的“事件”,这不是您从表单提交中收到的内容。
    • Issue 4335 中所述,values 数组会跳过空白答案(在“新表单”+“新表格”中)。 filter(Boolean) 方法用于模拟此行为。

*格式为“纯文本”的单元格会将日期保留为字符串,这不是一个好主意。

关于debugging - 如何在 GAS 中测试触发功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52403030/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com