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

请求编写Excel Basic脚本:跨表匹配信息并自动更新签到状态

Excel脚本实现跨表匹配签到状态并更新

核心思路

先把「签到数据」表里的有效签到记录整理成可快速查找的格式,再逐个遍历目标工作表,匹配邮箱和日期后更新对应单元格。

示例脚本(适配常见签到表结构)

function main(workbook: ExcelScript.Workbook) {
  // 1. 定义工作表名称(根据你的实际表名修改)
  const sourceSheetName = "签到数据";
  const targetSheetNames = ["月度签到表", "季度签到表"]; // 可添加更多目标表

  // 2. 获取数据源表并读取所有数据
  const sourceSheet = workbook.getWorksheet(sourceSheetName);
  const sourceRange = sourceSheet.getUsedRange();
  const sourceValues = sourceRange.getValues();
  
  // 把签到数据转成字典:key为「邮箱_日期」,value为签到状态
  const signInRecords: {[key: string]: boolean} = {};
  // 跳过表头(假设第一行是表头)
  for (let i = 1; i < sourceValues.length; i++) {
    const email = sourceValues[i][0] as string; // 第一列是邮箱
    const signDate = sourceValues[i][1] as string; // 第二列是签到日期
    const isSigned = sourceValues[i][2] as boolean; // 第三列是签到状态(true/false)
    
    if (email && signDate && isSigned) {
      // 统一日期格式,避免格式不匹配(比如"2024/5/10"和"2024-5-10")
      const formattedDate = new Date(signDate).toLocaleDateString();
      const recordKey = `${email}_${formattedDate}`;
      signInRecords[recordKey] = true;
    }
  }

  // 3. 遍历每个目标工作表,更新签到状态
  targetSheetNames.forEach(sheetName => {
    const targetSheet = workbook.getWorksheet(sheetName);
    if (!targetSheet) {
      console.log(`未找到工作表:${sheetName}`);
      return;
    }

    const targetRange = targetSheet.getUsedRange();
    const targetValues = targetRange.getValues();
    const rowCount = targetValues.length;
    const colCount = targetValues[0].length;

    // 遍历目标表的每一行(跳过表头,第一行是日期)
    for (let row = 1; row < rowCount; row++) {
      const targetEmail = targetValues[row][0] as string; // 第一列是邮箱
      if (!targetEmail) continue;

      // 遍历每一列(第一列是邮箱,从第二列开始是日期)
      for (let col = 1; col < colCount; col++) {
        const targetDate = targetValues[0][col] as string; // 第一行是日期
        if (!targetDate) continue;

        const formattedTargetDate = new Date(targetDate).toLocaleDateString();
        const matchKey = `${targetEmail}_${formattedTargetDate}`;

        // 如果找到匹配的签到记录,更新单元格
        if (signInRecords[matchKey]) {
          const targetCell = targetSheet.getCell(row, col);
          targetCell.setValue("已签到"); // 可改成你需要的内容,比如"√"或者true
          targetCell.getFormat().getFill().setColor("#90EE90"); // 可选:设置背景色标记
        }
      }
    }
  });
}

新手适配指南

  • 修改工作表名称:把sourceSheetName和targetSheetNames改成你实际的表名
  • 调整列位置:如果你的邮箱/日期/状态列不在第1/2/3列,修改sourceValues[i][0]里的索引(索引从0开始)
  • 自定义更新内容:把targetCell.setValue("已签到")改成你需要的内容,比如打勾"√"或者布尔值true
  • 日期格式处理:脚本里用toLocaleDateString()统一日期格式,避免因Excel日期显示格式不同导致匹配失败

常见问题解决

  • 匹配失败:检查数据源和目标表的邮箱是否完全一致(大小写、空格都要注意),日期格式是否统一
  • 脚本报错:先确认所有工作表都存在,没有拼写错误;再检查数据类型,比如状态列如果是文本"true",需要改成sourceValues[i][2] === "true"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 08:10:55