使用appsscript.json创建Google Sheets插件侧边栏报错及可行性咨询
问题解答
错误原因
你在appsscript.json里添加的sidebar字段是Google Apps Script配置清单(manifest)规范里不存在的,这就是报错的直接原因——官方根本没有这个配置项。
修复步骤
移除无效配置字段
先把appsscript.json里的sidebar整个节点删掉,修改后的配置文件如下:{ "timeZone": "America/New_York", "dependencies": { }, "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8", "oauthScopes": [ "https://www.googleapis.com/auth/script.container.ui", "https://www.googleapis.com/auth/spreadsheets" ] }通过代码创建侧边栏
侧边栏必须通过编写Apps Script代码来实现,具体步骤:- 新建HTML文件(命名为
sidebar.html),写入侧边栏内容:<!DOCTYPE html> <html> <body> <h2>我的自定义侧边栏</h2> <p>这里可以添加任意网页内容</p> </body> </html> - 在
.gs文件(比如Code.gs)里编写触发函数,实现侧边栏的显示逻辑,还可以添加菜单让用户手动调用:// 打开表格时自动添加自定义菜单 function onOpen() { SpreadsheetApp.getUi() .createMenu('我的工具') .addItem('打开侧边栏', 'showSidebar') .addToUi(); } // 显示侧边栏的核心函数 function showSidebar() { const htmlContent = HtmlService.createHtmlOutputFromFile('sidebar.html') .setTitle('My Sidebar Add-on'); SpreadsheetApp.getUi().showSidebar(htmlContent); }
- 新建HTML文件(命名为
关于manifest创建侧边栏的疑问
Google Apps Script完全不支持通过appsscript.json配置侧边栏。侧边栏的创建、显示必须通过代码调用UI相关API实现,manifest仅用于配置插件的基础信息、权限、顶层菜单结构等元数据,无法直接定义侧边栏。
内容的提问来源于stack exchange,提问作者Let Me Tink About It
相关产品推荐
相关产品推荐

