LesterLyu / fast-formula-parser

Parse and evaluate MS Excel formula in javascript.
https://www.npmjs.com/package/fast-formula-parser
MIT License
454 stars 63 forks source link

onCell return date #62

Closed Stevemoretz closed 3 months ago

Stevemoretz commented 3 months ago

I can't seem to find a way to return a valid date from inside a variable.

  onCell: ({ sheet, row, col }) => {
      return new Date();
      //or
      return [1999,9,9];
      //or
      return "1999-9-9";
      //or
      return "1999/9/9";
  }

None of the above works.

michaeltford commented 3 months ago

Fast-formula-parser adopts a similar convention to other spreadsheets and manages dates as numbers. If you want to specify as a string you can use DATEVALUE

  import DateFunctions from 'fast-formula-parser/formulas/functions/date';

  onCell: ({ sheet, row, col }) => {
      return DateFunctions.DATEVALUE('1999-9-9');
  }

This will return 36412 consistent with Google Sheets and Excel (if you apply number formatting you can see this.)