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

求助:SharePoint Excel Script获取下一个工作日脚本问题

问题分析与修复方案

你的脚本出现结果不符合预期的原因主要有三点:

  1. 时区未转换:脚本使用本地时间而非美国东部时间(EST/EDT)判断下午5点的阈值
  2. 时间判断逻辑错误:用字符串格式的时间直接比较,导致判断不准确
  3. 节假日检查效率低且循环逻辑存在疏漏:重复遍历列数据,且未正确连续跳过节假日+周末的组合

以下是修复后的完整代码:

function main(workbook: ExcelScript.Workbook) {
    console.log("Script started");
    getNextWorkingDay(workbook);
    console.log("Script finished");
}

function getNextWorkingDay(workbook: ExcelScript.Workbook) {
    const sheetName = "Sheet1";
    const ws = workbook.getWorksheet(sheetName);

    if (!ws) {
        console.log(`工作表 '${sheetName}' 不存在`);
        return;
    }
    console.log(`已访问工作表 '${sheetName}'`);

    // 获取A列的节假日数据并转换为Date对象集合
    const holidayRange = ws.getUsedRange()?.getColumn(1);
    if (!holidayRange) {
        console.log("A列无数据");
        return;
    }

    const holidayDateSet = new Set<string>();
    const holidayValues = holidayRange.getValues();
    holidayValues.forEach(row => {
        if (row[0]) {
            const holidayDate = new Date(row[0].toString());
            holidayDateSet.add(holidayDate.toDateString());
        }
    });
    console.log(`已加载节假日日期集合: ${Array.from(holidayDateSet)}`);

    // 将当前时间转换为美国东部时间(考虑夏令时)
    const currentDateEST = new Date(new Date().toLocaleString("en-US", { timeZone: "America/New_York" }));
    const currentHourEST = currentDateEST.getHours();
    console.log(`美国东部时间: ${currentDateEST}, 小时数: ${currentHourEST}`);

    let nextWorkingDate = new Date(currentDateEST);
    let needAdjust = false;

    // 判断是否需要调整日期:节假日、周末、EST下午5点之后
    const isTodayHoliday = holidayDateSet.has(currentDateEST.toDateString());
    const isWeekend = currentDateEST.getDay() === 0 || currentDateEST.getDay() === 6;
    const isAfter5PM = currentHourEST >= 17;

    if (isTodayHoliday || isWeekend || isAfter5PM) {
        needAdjust = true;
    }

    // 循环查找下一个工作日
    if (needAdjust) {
        let isNextHoliday: boolean;
        let isNextWeekend: boolean;
        do {
            nextWorkingDate.setDate(nextWorkingDate.getDate() + 1);
            isNextHoliday = holidayDateSet.has(nextWorkingDate.toDateString());
            isNextWeekend = nextWorkingDate.getDay() === 0 || nextWorkingDate.getDay() === 6;
        } while (isNextHoliday || isNextWeekend);
    }

    // 格式化日期为MM/DD/YY格式
    const formattedDate = `${("0" + (nextWorkingDate.getMonth() + 1)).slice(-2)}/${("0" + nextWorkingDate.getDate()).slice(-2)}/${nextWorkingDate.getFullYear().toString().slice(-2)}`;
    console.log(`计算得到的下一个工作日: ${formattedDate}`);

    // 写入C3单元格(修正原代码中C3后的空格)
    ws.getRange("C3").setValue(formattedDate);
    console.log("结果已写入C3单元格");
}

关键修改点说明

  • 时区转换:使用toLocaleString指定America/New_York时区,确保时间判断基于美国东部时间(自动适配夏令时EDT)
  • 节假日集合优化:将所有节假日日期存入Set,避免重复遍历列数据,提升检查效率
  • 时间判断修正:直接比较小时数currentHourEST >=17,替代原有的字符串比较逻辑
  • 循环逻辑修正:在循环内实时判断下一天是否为节假日/周末,确保连续跳过所有非工作日
  • 单元格引用修正:移除原代码中C3后的空格,避免引用错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 05:17:24