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

Google脚本实现工作簿工作表下拉弹窗用于表内容对比

Google Sheets脚本实现主表与周表对比(带下拉选择弹窗)

你的需求完全可行,Google Apps Script的HTML服务可以创建带下拉列表的自定义弹窗,同时支持脚本暂停逻辑。以下是完整的实现方案和代码:

一、完整代码实现

1. Google Apps Script 代码(.gs 文件)

function onOpen() {
  SpreadsheetApp.getUi() 
      .createMenu('自定义菜单')
      .addItem('选择周表并对比', 'showCompareDialog')
      .addToUi();
}

function showCompareDialog() {
  // 获取当前工作簿所有工作表名称
  const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  const sheetNames = sheets.map(sheet => sheet.getName());
  
  // 加载HTML模板并传递工作表名称
  const template = HtmlService.createTemplateFromFile('CompareDialog');
  template.sheetNames = sheetNames;
  
  // 显示自定义模态对话框
  const htmlOutput = template.evaluate()
      .setWidth(400)
      .setHeight(200);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, '选择待对比的周表');
}

// 执行主表与选中周表的对比逻辑
function compareSheets(selectedSheetName) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const mainSheet = ss.getSheetByName('主表'); // 替换为你的主表实际名称
  const weeklySheet = ss.getSheetByName(selectedSheetName);
  
  // 检查表是否存在
  if (!mainSheet || !weeklySheet) {
    SpreadsheetApp.getUi().alert('主表或选中的周表不存在,请核对工作表名称!');
    return;
  }

  // 实现启动前暂停确认
  const startConfirm = SpreadsheetApp.getUi().alert(
    '即将开始对比,是否继续?',
    SpreadsheetApp.getUi().ButtonSet.OK_CANCEL
  );
  if (startConfirm !== SpreadsheetApp.getUi().Button.OK) {
    SpreadsheetApp.getUi().alert('对比已暂停');
    return;
  }

  // 示例对比逻辑:获取两表数据并对比行数
  const mainData = mainSheet.getDataRange().getValues();
  const weeklyData = weeklySheet.getDataRange().getValues();

  let result = `主表数据行数:${mainData.length}\n周表数据行数:${weeklyData.length}\n`;
  result += mainData.length === weeklyData.length ? '两表行数一致' : '两表行数不一致';

  SpreadsheetApp.getUi().alert('对比结果:\n' + result);

  // 可选:中途暂停逻辑(适合大量数据逐行对比)
  /*
  for (let i = 0; i < mainData.length; i++) {
    // 此处添加逐行对比的业务逻辑...

    // 每处理10行询问是否继续
    if (i % 10 === 0 && i > 0) {
      const continueConfirm = SpreadsheetApp.getUi().alert(
        `已处理第${i}行,是否继续对比?`,
        SpreadsheetApp.getUi().ButtonSet.OK_CANCEL
      );
      if (continueConfirm !== SpreadsheetApp.getUi().Button.OK) {
        SpreadsheetApp.getUi().alert(`对比已暂停,当前处理至第${i}行`);
        break;
      }
    }
  }
  */
}

2. HTML 对话框代码(新建名为 CompareDialog 的HTML文件)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
      .container { padding: 20px; font-family: Arial, sans-serif; }
      select { width: 100%; padding: 8px; margin: 10px 0; font-size: 14px; }
      .button-group { text-align: right; margin-top: 15px; }
      button {
        padding: 8px 16px;
        margin-left: 8px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
      }
      .confirm-btn { background-color: #4285F4; color: white; }
      .confirm-btn:hover { background-color: #3367D6; }
      .cancel-btn { background-color: #f1f3f4; color: #202124; }
      .cancel-btn:hover { background-color: #e8eaed; }
    </style>
  </head>
  <body>
    <div class="container">
      <p>请选择需要对比的周表:</p>
      <select id="sheetSelector">
        <? sheetNames.forEach(name => { ?>
          <option value="<?= name ?>"><?= name ?></option>
        <? }) ?>
      </select>
      <div class="button-group">
        <button class="cancel-btn" onclick="google.script.host.close()">取消</button>
        <button class="confirm-btn" onclick="submitSelection()">确认对比</button>
      </div>
    </div>

    <script>
      function submitSelection() {
        const selectedSheet = document.getElementById('sheetSelector').value;
        google.script.run.compareSheets(selectedSheet);
        google.script.host.close();
      }
    </script>
  </body>
</html>

二、关键功能说明

  • 自定义下拉弹窗:通过HTML服务生成带下拉列表的模态对话框,替代原生prompt的文本输入,避免手动输入错误
  • 暂停逻辑:
    • 对比启动前增加确认弹窗,用户可选择取消以暂停流程
    • 注释部分提供了中途暂停方案,适合大量数据逐行对比时,每处理N行询问用户是否继续
  • 对比逻辑扩展:示例仅对比行数,你可以根据需求修改compareSheets函数,比如对比特定列的单元格值、查找新增/缺失的记录等

三、使用步骤

  1. 打开目标Google Sheets,点击「扩展程序」→「Apps Script」进入脚本编辑器
  2. 删除原有代码,粘贴上述.gs代码,将getSheetByName('主表')中的“主表”替换为你的实际主表名称
  3. 点击「文件」→「新建」→「HTML文件」,命名为CompareDialog,粘贴上述HTML代码
  4. 保存脚本,刷新Google Sheets页面,即可看到顶部的「自定义菜单」

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 23:50:08