You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google Workspace插件在表格中获取编辑器插件菜单及自定义函数

如何通过Google Workspace插件自动关联安装Editor插件以添加自定义函数

Google Workspace插件(Add-on)可通过关联绑定容器绑定脚本(Editor插件)的方式,在用户安装时自动完成Editor插件的部署,从而实现自定义函数的添加。核心是借助Google Apps Script的部署API与插件授权机制,以下是具体实现步骤:

  • 步骤1:创建并部署Editor插件(容器绑定脚本)
    在目标Google Sheets文件中创建容器绑定脚本,编写自定义函数逻辑,示例代码如下:

    // 容器绑定脚本的Code.js
    function MY_CUSTOM_FUNCTION(input) {
      return "处理后的数据:" + input;
    }
    

    将该脚本部署为可安装编辑器附加组件,获取部署ID,并根据需求设置共享权限(确保Workspace插件可调用)。

  • 步骤2:在Workspace插件中添加自动安装逻辑
    在Workspace插件的Code.js中,添加触发逻辑,在用户首次安装或授权时自动安装Editor插件,示例代码:

    // Workspace插件的Code.js
    function onInstall(e) {
      onOpen(e);
      installEditorAddon();
    }
    
    function onOpen(e) {
      SpreadsheetApp.getUi().createAddonMenu()
        .addItem('安装自定义函数', 'installEditorAddon')
        .addToUi();
    }
    
    function installEditorAddon() {
      const editorAddonDeploymentId = "你的Editor插件部署ID";
      try {
        const response = UrlFetchApp.fetch(
          `https://script.googleapis.com/v1/scripts/${editorAddonDeploymentId}:install`,
          {
            method: 'POST',
            headers: {
              'Authorization': `Bearer ${ScriptApp.getOAuthToken()}`,
              'Content-Type': 'application/json'
            },
            payload: JSON.stringify({
              documentId: SpreadsheetApp.getActiveSpreadsheet().getId()
            })
          }
        );
        SpreadsheetApp.getUi().alert("自定义函数安装成功!");
      } catch (err) {
        SpreadsheetApp.getUi().alert("安装失败:" + err.message);
      }
    }
    
  • 步骤3:配置Workspace插件的权限与触发规则
    修改appsscript.json,添加必要的OAuth范围与文件授权触发规则,确保插件具备调用API的权限:

    {
      "timeZone": "Asia/Shanghai",
      "dependencies": {},
      "oauthScopes": [
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/script.deployments",
        "https://www.googleapis.com/auth/script.external_request"
      ],
      "addOns": {
        "common": {
          "name": "我的Workspace插件",
          "logoUrl": "https://example.com/logo.png",
          "layoutProperties": {
            "primaryColor": "#4285F4"
          },
          "homepageTrigger": {
            "runFunction": "showSidebar",
            "enabled": true
          }
        },
        "sheets": {
          "onFileScopeGrantedTrigger": {
            "runFunction": "installEditorAddon"
          }
        }
      }
    }
    

    其中onFileScopeGrantedTrigger是核心配置,当用户授权插件访问当前文件时,会自动执行Editor插件的安装函数。

  • 步骤4:侧边栏交互配置(可选)
    若需在侧边栏提供手动安装入口,可在Sidebar.html中添加对应按钮:

    <!-- Sidebar.html -->
    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
      </head>
      <body>
        <button onclick="google.script.run.installEditorAddon()">安装自定义函数</button>
      </body>
    </html>
    

这种实现方式的核心是利用Google Apps Script的部署安装API,通过Workspace插件的授权令牌,将预先部署好的Editor插件绑定到用户当前打开的Sheet中。GPT for Docs Sheets Slides即采用类似机制,在用户安装Workspace插件时自动完成Editor插件的绑定,让用户无需手动操作即可使用自定义函数。

内容的提问来源于stack exchange,提问作者Let Me Tink About It

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 18:37:57