google-apps-script - 你可以在谷歌脚本中自我引用回调代码吗?
问题描述
我仍在试图弄清楚谷歌的脚本库的东西。我遇到的一个限制是回调。似乎您不能让回调引用库(即使它起源于那里),因此您必须创建一个本地回调,然后调用库回调。
让我解释:
库 LIB_A:
/*
Populates the drop-down menu on the sheet
*/
function onOpen() {
var tenantMenu = ui.createMenu("Tenant")
tenantMenu.addItem("Add Tenant", "showAddTenantDialog")
topMenu.addSubMenu(tenantMenu)
topMenu.addToUi()
function showAddTenantDialog() {
...
}
在导入 LIB_A 的代码中,要使其正常工作,我似乎必须具备:
/* This function is called by the sheet when it is opened. I understand why this is required */
function onOpen() {
/* create the menu */
LIB_A.onOpen()
}
/* Callback from the menu (that is actually in LIB_A) */
function showAddTenantDialog() {
/* Call the intended function that resides in LIB_A */
LIB_A.showAddTenantDialog()
}
我真的很想让我的 LIB_A 代码看起来像这样:
tenantMenu.addItem("Add Tenant", "LIB_A.showAddTenantDialog")
所以我只需要在导入 LIB_A 的代码中使用它。
function onOpen() {
LIB_A.onOpen()
}
解决方案
推荐阅读
- python - Socket 无法读取大文件。Python
- azure - Azure DevOps 在 Excel 中以树级别提取工作项
- c++ - 如何将 .o 文件输出到一个特定的文件夹中?
- javascript - 内置显示“矩形”的 HTML 编辑器
- sql - MongoDB多次查找到同一个集合
- java - 如何解决片段中的 Viewpager 问题?
- c# - 从外部程序集加载时,IServiceCollection 无法解析类型
- mysql - MySQL 授予本地 db 用户权限以允许远程主机使用 ansible
- php - 逻辑和代码codeigniter如何将页面查看注册过程的会话数据作为临时会话使用
- python - 在将日期从 obj 转换为 datetime 数据类型时在 python 中使用 Pandas,我没有正确转换日期。为什么?