Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/clickup/formulajsProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,26 @@ const overrides = {
hasNil(startDate, endDate) ? Number.NaN : formulajs.YEARFRAC(startDate, endDate, basis),
WORKDAY: (startDate: unknown, days: unknown, holidays: unknown) =>
hasNil(startDate) ? Number.NaN : formulajs.WORKDAY(startDate, nilToZero(days), holidays),
NETWORKDAYS: (startDate: unknown, endDate: unknown, holidays: unknown) =>
hasNil(startDate, endDate) ? Number.NaN : formulajs.NETWORKDAYS(startDate, endDate, holidays),
NETWORKDAYS: (startDate: unknown, endDate: unknown, holidays: unknown) => {
if (hasNil(startDate, endDate)) return Number.NaN;
// Strip time from Date objects. Excel/Sheets ignores time-of-day, but formulajs
// uses Math.round on the ms difference which produces fractional results and
// off-by-one errors when Date objects have time components. Strings and serial
// numbers are unaffected — formulajs.parseDate handles those correctly.
const stripTime = (v: unknown): unknown =>
v instanceof Date ? new Date(v.getFullYear(), v.getMonth(), v.getDate()) : v;
const start = stripTime(startDate);
const end = stripTime(endDate);
// formulajs NETWORKDAYS doesn't handle start > end: the day-counting loop
// never runs when days <= 0, so weekends/holidays aren't subtracted.
// Fix by computing the forward result and negating.
const result = formulajs.NETWORKDAYS(start, end, holidays);
if (typeof result === 'number' && result <= 0) {
const reverse = formulajs.NETWORKDAYS(end, start, holidays);
if (typeof reverse === 'number' && reverse > 0) return -reverse;
}
return result;
},
};

export const formulajsProxy = new Proxy(formulajs, {
Expand Down
28 changes: 28 additions & 0 deletions test/integration/parsing/formula/date-time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ describe('.parse() date & time formulas', () => {
expect(parser.parse('NETWORKDAYS(null, "2013-12-05")')).toMatchObject({ error: null, result: Number.NaN });
expect(parser.parse('NETWORKDAYS("2013-12-05", null)')).toMatchObject({ error: null, result: Number.NaN });
expect(parser.parse('NETWORKDAYS(null, null)')).toMatchObject({ error: null, result: Number.NaN });

// Forward: Aug 7 (Wed) to Aug 9 (Fri) — 3 working days inclusive
expect(parser.parse('NETWORKDAYS("8/7/2024", "8/9/2024")')).toMatchObject({ error: null, result: 3 });

// Reverse direction (start > end) — should return negative of forward count
expect(parser.parse('NETWORKDAYS("2013-12-05", "2013-12-04")')).toMatchObject({ error: null, result: -2 });
expect(parser.parse('NETWORKDAYS("2013-12-05", "2013-11-04")')).toMatchObject({ error: null, result: -24 });
expect(parser.parse('NETWORKDAYS("3/1/2013", "10/1/2012", [\'11/22/2012\'])')).toMatchObject({
error: null,
result: -109,
});
});

// Time components in Date objects should be ignored (Excel/Sheets behavior).
// ClickUp field values are Date objects that may include time-of-day.
it.each([new Parser(), ClickUpParser.create()])('NETWORKDAYS with time components', (parser) => {
const vars = {};
parser.on('callVariable', (name, done) => done(vars[name]));

// Aug 7 (Wed) 23:00 → Aug 9 (Fri) 01:00: should be 3, not 2
vars.start = new Date(2024, 7, 7, 23, 0, 0);
vars.end = new Date(2024, 7, 9, 1, 0, 0);
expect(parser.parse('NETWORKDAYS(start, end)')).toMatchObject({ error: null, result: 3 });

// Aug 7 (Wed) 01:00 → Aug 9 (Fri) 23:00: should be 3, not ~2.9
vars.start = new Date(2024, 7, 7, 1, 0, 0);
vars.end = new Date(2024, 7, 9, 23, 0, 0);
expect(parser.parse('NETWORKDAYS(start, end)')).toMatchObject({ error: null, result: 3 });
});

it.each([new Parser(), ClickUpParser.create()])('NOW', (parser) => {
Expand Down
Loading