Skip to content

Adopt upstream PR #2956: Fix excelToDate() for non-numeric values #19

@protobi-pieter

Description

@protobi-pieter

Summary

Adopt upstream PR exceljs#2956 to fix handling of non-numeric values in excelToDate().

Currently, when a non-numeric value is passed to excelToDate() (e.g., when a cell has date formatting but contains text), it returns "Invalid Date" which gets converted to NaN and causes Excel to show warnings when opening the file.

Upstream PR

Changes

File modified:

  • lib/utils/utils.js - Add validation check in excelToDate() (1 line)

Total: 1 addition, 0 deletions

Bug Description

When a cell has date formatting but contains a non-numeric value, excelToDate() tries to convert it and returns "Invalid Date", which persists as NaN in the Excel file.

Current behavior:

excelToDate(v, date1904) {
  const millisecondSinceEpoch = Math.round((v - 25569 + ...) * 24 * 3600 * 1000);
  return new Date(millisecondSinceEpoch);  // Returns Invalid Date for non-numeric v
}

Fixed behavior:

excelToDate(v, date1904) {
  if (!Number.isFinite(v)) return v;  // Return original value if not numeric
  const millisecondSinceEpoch = Math.round((v - 25569 + ...) * 24 * 3600 * 1000);
  return new Date(millisecondSinceEpoch);
}

Value Proposition

  • Low Risk - Single line validation check
  • Bug Fix - Prevents Excel warnings on file open
  • User Experience - Eliminates annoying error popups

Testing Plan

  • Apply changes from upstream PR
  • Run existing test suite
  • Verify non-numeric values don't cause Invalid Date

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions