我可以在 Google Script App 中自己构建事件对象吗?
Can i build an event object myself in Google Script App?
我买了第 3 方 google 应用程序脚本来使用。但是,它只能使用 onEdit 方法调用,并且它们的代码是私有的,我无法更改。另外,我需要的是基于时间触发器而不是 onEdit-trigger。因此,我尝试构建自己的事件来触发函数:
// This function work well and can call the 3rd Party App Script
// It is triggered by onEdit googlesheet, which works well
function funcOnEdit(e) {
3rdPartyApp.funcOnEdit(e));
}
// Below is the jsontostring result of the event e
// {authMode:"FULL",oldValue:"false",range:{columnEnd:6,columnStart:6,rowEnd:3,rowStart:3},source:{},triggerUid:"xxx",user:{email:"xxxx@gmail.com",nickname:"xxxx"},value:"TRUE"}
所以我构建了一个类似的事件对象,它由时间触发以使其发生。
function funcOnTimeTrigger(e) {
var e1 = {authMode:"FULL",oldValue:"false",range:{columnEnd:6,columnStart:6,rowEnd:3,rowStart:3},source:{},triggerUid:"xxx",user:{email:"xxxx@gmail.com",nickname:"xxxx"},value:"TRUE"};
e1.triggerUid = e.triggerUid;
3rdPartyApp.funcOnEdit(e1));
}
不幸的是,我找不到任何文档和参考代码来构建“onEdit”事件。这就是为什么,我尝试自己找到 object/class。
function getObjType(obj) {
var type = typeof(obj);
if (type === "object") {
try {
// Try a dummy method, catch the error
type = obj.getObjTypeXYZZY();
} catch (error) {
// Should be a TypeError - parse the object type from error message
// type = error.message.split(" object ")[1].replace('.','');
type = error.message;
}
}
return type;
}
// Below function is triggered by onEdit
function funcOnEdit_checker(e) {
getObjType(e);
}
// Unfortunately, it cannot show the object name or classname
我不知道下一步该做什么,我可以知道是否可以在 Google 脚本应用程序中自己构建事件 class/object 吗?任何人都可以就如何这样做给出一些提示吗?还是不可能?
我想手动创建事件对象“developers.google.com/apps-script/guides/triggers/events”并将事件“e”传递给 3rdPartyApp.funcOnEdit 函数。可以这样做吗?
参考:
感谢@Cooper idea,跟我有一样的想法
最后我找到了结果@Explain purpose of `e` event parameter in onEdit
以下是我的回答(尚未优化但有效):
function funcOnTimeTrigger(e) {
var e2 = {}
e2["authMode"] = ScriptApp.AuthMode.FULL
e2['user'] = "me";
e2['range'] = SpreadsheetApp.getActive().getSheetByName("XXXXXX").getRange(5,3).activate();
e2.range.columnStart = 5;
e2.range.columnEnd = 5;
e2.range.rowStart = 3;
e2.range.rowEnd = 3;
e2['source'] = SpreadsheetApp.getActive();
e2['oldValue'] = "old";
e2['value'] = "new";
// Run The Program
3rdPartyApp.funcOnEdit(e2);
}
我买了第 3 方 google 应用程序脚本来使用。但是,它只能使用 onEdit 方法调用,并且它们的代码是私有的,我无法更改。另外,我需要的是基于时间触发器而不是 onEdit-trigger。因此,我尝试构建自己的事件来触发函数:
// This function work well and can call the 3rd Party App Script
// It is triggered by onEdit googlesheet, which works well
function funcOnEdit(e) {
3rdPartyApp.funcOnEdit(e));
}
// Below is the jsontostring result of the event e
// {authMode:"FULL",oldValue:"false",range:{columnEnd:6,columnStart:6,rowEnd:3,rowStart:3},source:{},triggerUid:"xxx",user:{email:"xxxx@gmail.com",nickname:"xxxx"},value:"TRUE"}
所以我构建了一个类似的事件对象,它由时间触发以使其发生。
function funcOnTimeTrigger(e) {
var e1 = {authMode:"FULL",oldValue:"false",range:{columnEnd:6,columnStart:6,rowEnd:3,rowStart:3},source:{},triggerUid:"xxx",user:{email:"xxxx@gmail.com",nickname:"xxxx"},value:"TRUE"};
e1.triggerUid = e.triggerUid;
3rdPartyApp.funcOnEdit(e1));
}
不幸的是,我找不到任何文档和参考代码来构建“onEdit”事件。这就是为什么,我尝试自己找到 object/class。
function getObjType(obj) {
var type = typeof(obj);
if (type === "object") {
try {
// Try a dummy method, catch the error
type = obj.getObjTypeXYZZY();
} catch (error) {
// Should be a TypeError - parse the object type from error message
// type = error.message.split(" object ")[1].replace('.','');
type = error.message;
}
}
return type;
}
// Below function is triggered by onEdit
function funcOnEdit_checker(e) {
getObjType(e);
}
// Unfortunately, it cannot show the object name or classname
我不知道下一步该做什么,我可以知道是否可以在 Google 脚本应用程序中自己构建事件 class/object 吗?任何人都可以就如何这样做给出一些提示吗?还是不可能?
我想手动创建事件对象“developers.google.com/apps-script/guides/triggers/events”并将事件“e”传递给 3rdPartyApp.funcOnEdit 函数。可以这样做吗?
参考:
感谢@Cooper idea,跟我有一样的想法
最后我找到了结果@Explain purpose of `e` event parameter in onEdit
以下是我的回答(尚未优化但有效):
function funcOnTimeTrigger(e) {
var e2 = {}
e2["authMode"] = ScriptApp.AuthMode.FULL
e2['user'] = "me";
e2['range'] = SpreadsheetApp.getActive().getSheetByName("XXXXXX").getRange(5,3).activate();
e2.range.columnStart = 5;
e2.range.columnEnd = 5;
e2.range.rowStart = 3;
e2.range.rowEnd = 3;
e2['source'] = SpreadsheetApp.getActive();
e2['oldValue'] = "old";
e2['value'] = "new";
// Run The Program
3rdPartyApp.funcOnEdit(e2);
}