从Worbook/Worksheet/Table中删除所有活动处理程序
我正在使用ScriptLab玩活动处理程序。每次我运行脚本时,都会注册一个新的活动处理程序。 我知道如何删除一个事件处理程序,但是每次刷新代码执行时,不再有可能删除在上次执行中注册的事件处理程序,因此它们会堆叠并使用新代码执行。
我想知道是否可以从工作表,表,工作簿等中删除所有活动处理程序。
也许是一种访问注册活动处理程序集合并允许删除它们的方法,就像我们可以使用JavaScript一样。
编辑:上下文代码,
$("#run").click(() => tryCatch(run));
$("#remove").click(() => tryCatch(remove));
let eventResult;
async function run() {
await Excel.run(async (context) => {
const worksheet = context.workbook.worksheets.getItem("Sample");
eventResult = worksheet.onSelectionChanged.add(handleSelectionChange);
await context.sync();
console.log("Event handler successfully registered for onSelectionChanged event in the worksheet.");
});
}
async function handleSelectionChange(event) {
await Excel.run(async (context) => {
await context.sync();
console.log("Address of current selection: " + event.address);
});
}
async function remove() {
await Excel.run(eventResult.context, async (context) => {
eventResult.remove();
await context.sync();
eventResult = null;
console.log("Event handler successfully removed.");
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
请参见如果将其存储在变量(EventResult
)中,则很容易删除处理程序。但是,一旦我对ScriptLab代码窗格进行任何更改,每个变量都会重置,但是该事件仍在工作表中活跃。
即使代码脱离上下文,变量丢失了状态,我也希望一种删除事件的方法。
I'm using ScriptLab to play with event handlers. Every time I run a script, a new event handler is registered.
I know how to remove one event handler, but everytime the code execution is refreshed, it is not possible anymore to delete the event handler registered in the last execution, so they get stacked and execute with the new code.
I'd like to know if it is possible to remove all event handlers from a worksheet, table, workbook, etc.
Maybe a way to access a collection of registered event handlers and allow to delete them, the same way we can do with JavaScript.
EDIT: code for context
$("#run").click(() => tryCatch(run));
$("#remove").click(() => tryCatch(remove));
let eventResult;
async function run() {
await Excel.run(async (context) => {
const worksheet = context.workbook.worksheets.getItem("Sample");
eventResult = worksheet.onSelectionChanged.add(handleSelectionChange);
await context.sync();
console.log("Event handler successfully registered for onSelectionChanged event in the worksheet.");
});
}
async function handleSelectionChange(event) {
await Excel.run(async (context) => {
await context.sync();
console.log("Address of current selection: " + event.address);
});
}
async function remove() {
await Excel.run(eventResult.context, async (context) => {
eventResult.remove();
await context.sync();
eventResult = null;
console.log("Event handler successfully removed.");
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
See that it is easy to remove a handler if you store it in a variable (eventResult
). But as soon as I make any change to the ScriptLab code pane, every variable is resetted, but the event is still active in the worksheet.
I'd like a way to remove the event(s) even if the code goes out of context and variables lose state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论