某些 Google Apps 脚本函数(例如 Logger.log() 和 Utility.formatString())接受无限的参数。
我想包装对 Logger.log() 或 Utility.formatString() 的调用,以便在最里面的函数调用中,我可以在结果字符串前面加上时间戳和我自己的文本。通常,您只需.apply
当前模块的标记到该函数,但在我的测试中,这并不能按预期工作。它会导致错误 TypeError: Cannot find default value for object。 (第 14 行,文件“代码”)
其中 looks来自 java/服务器端。
这是我用于测试的代码。问题:
- 这是这些 Apps 脚本方法中的错误吗?
- 是否有解决方法或替代方法来实现我的目标?
如果这是一个错误,我也会将其记录在问题跟踪器跟踪器上
function testMyLogger() {
myLogger('Testing %s %s %s', 'one', 2, 'three');
}
function myLogger() {
// do common things like prefix a timestamp to the first arg
var logdate = Utilities.formatDate(new Date(), Session.getTimeZone(), "yyyy-MM-dd HH:mm:ss");
arguments[0]= logdate + " " + arguments[0];
Logger.log.apply(this, arguments);
// At the above line I get error "TypeError: Cannot find default value for object. (line 14, file "Code")"
// but I expected '<timestamp> Testing one 2.0 three' to the log (View > Logs...)
}
最佳答案
尝试Logger.log.apply(Logger,参数);
关于javascript - 如何包装对接受无限参数的 Google Apps 脚本函数的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913071/