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

如何通过HTML按钮调用Google Script清空Google Sheet文本内容?

实现HTML按钮调用Google Script清空Google Sheet内容

1. 编写Google Script后端代码

打开目标Google表格,点击「扩展程序」→「Apps脚本」,替换默认代码为以下内容:

// 返回前端HTML页面
function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}

// 清空表格内容的核心函数
function clearSheetContent() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const activeSheet = spreadsheet.getActiveSheet();
  
  // 仅清空单元格文本内容(保留格式),如需连格式一起清除改用 activeSheet.clear()
  activeSheet.clearContent();
  
  return "表格内容已成功清空";
}

2. 添加前端HTML页面

在Apps脚本编辑器左侧,点击「文件」→「新建」→「HTML文件」,命名为index,粘贴以下代码:

<!DOCTYPE html>
<html>
  <body style="padding: 20px;">
    <button style="padding: 8px 16px; font-size: 16px;" onclick="triggerClear()">清空表格所有内容</button>

    <script>
      function triggerClear() {
        // 调用后端Google Script函数
        google.script.run
          .withSuccessHandler(function(msg) {
            alert(msg);
          })
          .withFailureHandler(function(err) {
            alert('操作失败:' + err.message);
          })
          .clearSheetContent();
      }
    </script>
  </body>
</html>

3. 部署Web App并测试

  1. 在Apps脚本编辑器右上角,点击「部署」→「新部署」
  2. 点击齿轮图标,选择「Web应用」作为部署类型
  3. 配置选项:
    • 执行权限:选择「任何人,甚至匿名」(测试阶段推荐,后续可根据需求调整权限)
    • 访问权限:保持默认即可
  4. 点击「部署」,授权脚本访问你的表格权限后,复制生成的Web App链接
  5. 在浏览器中打开该链接,点击页面上的按钮,即可触发清空表格内容的操作

内容的提问来源于stack exchange,提问作者sadwave

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 19:18:09