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

如何基于选定月份筛选Google Sheets API返回的员工数组?

用Google Sheets API实现员工任职数据筛选方案

结合你提到的两张工作表结构和两个需求,我来拆解下具体的实现步骤:

先明确数据前提

  • 报表工作表:包含年份(如2011)和月份(如March)两列,用来指定筛选的目标月份
  • 员工数据工作表:包含startdate(格式MM-DD-YYYY,如03-24-2011)、enddate(同格式,离职员工有值,在职员工可能为空)等字段

第一步:把选定月份转成可对比的日期区间

首先得把报表里的“年份+月份”转成该月份的第一天和最后一天,这是后续筛选的核心基准。用Google Apps Script(和Sheets API适配性最好)的话可以写个工具函数:

function getTargetMonthRange(year, monthName) {
  // 把月份名称转成数字索引(0=1月,11=12月)
  const monthIndex = new Date(Date.parse(`${monthName} 1, ${year}`)).getMonth();
  // 生成该月第一天(如2011-03-01)
  const monthStart = new Date(year, monthIndex, 1);
  // 生成该月最后一天(如2011-03-31,传入0会自动取上月最后一天)
  const monthEnd = new Date(year, monthIndex + 1, 0);
  return { start: monthStart, end: monthEnd };
}

第二步:实现需求1——移除endDate晚于选定月份的员工

逻辑很简单:只要员工的enddate晚于选定月份的最后一天,就从数组里移除(在职员工因为enddate为空,不会被移除)。筛选代码示例:

function filterOutLateEndDateEmployees(employees, monthEndDate) {
  return employees.filter(emp => {
    // 处理离职员工的enddate,在职员工enddate为空则跳过判断
    const empEndDate = emp.enddate ? new Date(emp.enddate) : null;
    // 保留:要么没离职,要么离职日期<=选定月份最后一天
    return !(empEndDate && empEndDate > monthEndDate);
  });
}

第三步:实现需求2——仅显示选定月份任职的员工

这里要覆盖两种情况:

  • 在职员工:只要入职日期<=选定月份最后一天,就算该月份任职
  • 离职员工:入职日期<=选定月份最后一天 且 离职日期>=选定月份第一天

对应的筛选代码:

function filterEmployeesInTargetMonth(employees, monthStartDate, monthEndDate) {
  return employees.filter(emp => {
    const empStartDate = new Date(emp.startdate);
    // 在职员工用当前日期代替enddate(确保>=月份第一天)
    const empEndDate = emp.enddate ? new Date(emp.enddate) : new Date();
    
    // 核心判断:员工任职区间和选定月份有重叠
    return empStartDate <= monthEndDate && empEndDate >= monthStartDate;
  });
}

整合使用示例

把上面的函数串起来,完整流程大概是这样:

function runEmployeeFilter() {
  // 1. 从报表表读取选定的年份和月份(假设A1是年份,B1是月份)
  const reportSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("报表表");
  const targetYear = reportSheet.getRange("A1").getValue();
  const targetMonth = reportSheet.getRange("B1").getValue();
  
  // 2. 获取目标月份的日期区间
  const { start: monthStart, end: monthEnd } = getTargetMonthRange(targetYear, targetMonth);
  
  // 3. 从员工表读取数据(假设A列是startdate,B列是enddate,从第2行开始)
  const employeeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("员工数据表");
  const employeeData = employeeSheet.getRange(2, 1, employeeSheet.getLastRow()-1, 2).getValues();
  // 转成对象数组方便处理
  const employees = employeeData.map(row => ({
    startdate: row[0],
    enddate: row[1]
  }));
  
  // 4. 执行两个需求的筛选(可以单独执行,也可以组合)
  const filteredForReq1 = filterOutLateEndDateEmployees(employees, monthEnd);
  const filteredForReq2 = filterEmployeesInTargetMonth(employees, monthStart, monthEnd);
  
  // 5. 把筛选结果写回工作表(示例:写回C、D列)
  const resultRange = employeeSheet.getRange(2, 3, filteredForReq2.length, 2);
  resultRange.setValues(filteredForReq2.map(emp => [emp.startdate, emp.enddate]));
}

注意事项

  • 确保员工表的日期格式是Google Sheets能识别的MM-DD-YYYY,否则new Date()可能解析出错
  • 如果在职员工的enddate不是空而是未来日期,代码里的empEndDate处理逻辑依然有效
  • 可以根据实际需求调整筛选后的输出位置或格式

内容的提问来源于stack exchange,提问作者Dženis H.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:32:25