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

请求编写可绑定Google Sheets按钮的绘图创建脚本

解决方案:绑定按钮触发绘图编辑器的Google Apps Script

因为Google Sheets的宏无法录制打开绘图编辑器的操作,我们可以用Apps Script写一个脚本,绑定到自定义按钮上来实现这个功能:

步骤1:编写脚本

  1. 在你的Google Sheets中,点击顶部菜单栏的扩展 > Apps 脚本,打开脚本编辑器。
  2. 删除默认的myFunction代码,替换为以下脚本:
function openDrawingEditor() {
  const ui = SpreadsheetApp.getUi();
  const html = HtmlService.createHtmlOutput(`
    <script>
      function triggerDrawing() {
        // 找到"插入"菜单按钮
        const insertMenu = document.querySelector('[aria-label="插入"]');
        if (insertMenu) {
          insertMenu.click();
          // 延迟等待菜单展开
          setTimeout(() => {
            // 找到"绘图"选项
            const drawingOption = Array.from(document.querySelectorAll('.goog-menuitem'))
              .find(item => item.textContent.includes('绘图'));
            if (drawingOption) {
              drawingOption.click();
              google.script.host.close();
            } else {
              alert('未找到绘图选项,请手动点击「插入 > 绘图」');
              google.script.host.close();
            }
          }, 300);
        } else {
          alert('未找到插入菜单,请手动点击「插入 > 绘图」');
          google.script.host.close();
        }
      }
      window.onload = triggerDrawing;
    </script>
    <p>正在打开绘图编辑器...</p>
  `).setWidth(250).setHeight(80);
  ui.showModalDialog(html, '打开绘图工具');
}
  1. 点击脚本编辑器顶部的保存按钮,给脚本命名(比如OpenDrawingTool)。

步骤2:授权脚本

  1. 点击脚本编辑器顶部的运行按钮(▶️),首次运行会弹出授权提示。
  2. 按照提示完成授权:选择你的Google账户,点击「高级」,然后选择「转到[你的脚本名](不安全)」(这是因为脚本是你自己编写的,Google无法验证,实际是安全的),最后点击「允许」。

步骤3:创建并绑定按钮

  1. 返回Google Sheets,点击顶部菜单栏的插入 > 绘图,绘制一个按钮(比如矩形,添加文字如「打开绘图」),完成后点击「保存并关闭」将绘图插入到表格中。
  2. 点击刚才插入的绘图按钮,右上角会出现三个点的菜单,选择分配脚本。
  3. 在弹出的输入框中输入脚本名openDrawingEditor(注意大小写要和代码里的函数名一致),点击「确定」。

测试功能

现在点击这个自定义按钮,就会自动触发打开原生的绘图编辑器,和手动点击「插入 > 绘图」的效果完全一致。

注意:如果Google Sheets的UI元素类名或标签有更新,脚本可能需要微调,但目前这个方法可以稳定工作。

内容的提问来源于stack exchange,提问作者Desi Vargas-Garcia

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 02:12:52