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

如何在Google Sheets中创建按钮将考勤数据同步到另一电子表格

需求可行性结论

该需求完全可以实现,无需第三方工具,使用Google Sheets内置的Google Apps Script即可完成开发,适配你团队当前的Google Suite办公环境。

实现步骤

1. 导入适配你表格参数的脚本

打开你的Dashboard表格,点击顶部菜单栏「扩展程序」→「Apps 脚本」,清空默认代码后粘贴以下代码:

function submitAttendance() {
  // 获取两个工作表对象
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const dashboardSheet = ss.getSheetByName("Dashboard");
  const attendanceSheet = ss.getSheetByName("Attendance");

  // 读取Dashboard范围内的学生、志愿者姓名及对应考勤标记
  const studentNames = dashboardSheet.getRange("B2:B6").getValues().flat();
  const studentAttendance = dashboardSheet.getRange("A2:A6").getValues().flat();
  const volunteerNames = dashboardSheet.getRange("C2:C6").getValues().flat();
  const volunteerAttendance = dashboardSheet.getRange("D2:D6").getValues().flat();

  // 合并为姓名-考勤的键值对映射
  const attendanceMap = {};
  studentNames.forEach((name, index) => {
    if (name) attendanceMap[name.trim()] = studentAttendance[index];
  });
  volunteerNames.forEach((name, index) => {
    if (name) attendanceMap[name.trim()] = volunteerAttendance[index];
  });

  // 匹配Attendance表的今日日期列
  const dateList = attendanceSheet.getRange("B1:F1").getValues().flat();
  const nameList = attendanceSheet.getRange("A2:A13").getValues().flat();
  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const targetColIndex = dateList.findIndex(item => {
    if (item instanceof Date) {
      const tempDate = new Date(item);
      tempDate.setHours(0, 0, 0, 0);
      return tempDate.getTime() === today.getTime();
    }
    return false;
  });

  // 日期不存在时抛出提示
  if (targetColIndex === -1) {
    SpreadsheetApp.getUi().alert("未在Attendance表找到今日日期,请先补充日期行数据");
    return;
  }

  // 批量写入考勤数据
  nameList.forEach((name, rowIndex) => {
    const formattedName = name ? name.trim() : "";
    if (formattedName && attendanceMap[formattedName] !== undefined) {
      attendanceSheet.getRange(rowIndex + 2, targetColIndex + 2).setValue(attendanceMap[formattedName]);
    }
  });
  SpreadsheetApp.getUi().alert("今日考勤已同步完成");
}

点击代码编辑器顶部的保存按钮,给项目命名(比如「考勤同步工具」)即可。

2. 配置权限

首次运行代码时,点击代码编辑器顶部的运行按钮,按照弹出提示完成授权:遇到「Google未验证此应用」提示时,点击「高级」→「继续前往[你的项目名称]」即可完成授权,后续所有团队成员使用无需重复授权。
确认项目共享权限和表格一致,所有项目成员有编辑权限即可全员使用。

3. 添加Dashboard点击按钮

返回Dashboard表格,点击顶部菜单栏「插入」→「绘图」,自定义按钮样式(比如填充颜色、添加「同步今日考勤」文字),保存后拖放到表格合适位置。
点击插入的绘图右上角的三个点,选择「分配脚本」,输入函数名submitAttendance确认即可。

可调整项

  • 如果后续学生、志愿者人数扩容,修改代码中对应区域的范围参数即可,比如把B2:B6修改为B2:B50
  • 考勤标记的格式不限制,打勾、文本、数字都可以原样同步
  • 如果不需要默认匹配当日日期,可以修改日期匹配逻辑,改为手动指定同步日期即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 23:36:04