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

Return formula result datatype #31

Open LesterLyu opened 3 years ago

LesterLyu commented 3 years ago

Why?

People need to know the format of the formula result to visulize the result.

Example 1:

DATE(2000, 1, 1) results in

Example 2:

DATE(CONCAT(200, 0), 1, 1) returns a date value/ serial number. CONCAT(200, 0) always return a string (or error), DATE(CONCAT(200, 0), 1, 1) always return a date (or error).

Example 3:

CONCATE(DATE(2000, 1, 1)) returns a string.

Questions

Do we need to propagate the type internally?

It is unnecessary. We only need to know the out-most function name, since the out-most function determines the result type.

How many types are there?

So far, I discovered only Date and General. General can be JavaScript Number, String, Boolean.

How about some formulas returning a cell reference, range reference, array? Should we tell the user what we returning?

How about a simple reference =A1, where A1 has a formula type Date?

We should know the type of the given cell, but how?

Should we provide a format function in the result wrapper, which is basically SSF.format but only takes a format string?

Do we use a pure JS object to wrap the formula result, or a class?

class may use more memory. Generating more unnecessary data → Invoke Garbage Collector more frequently.

More questions?

Proposing

Currently I don't have a clear idea of the implementation, we must think of all the questions above carefully.

Instead of returning the value directly, we return an object that wraps the result and add information about the type, e.g., Date, or Excel Serial Number.

LesterLyu commented 3 years ago

@rmdort Please take a look.

rmdort commented 3 years ago

Looks great. A few things if not covered.

  1. It would be good if we return specific types rather than General, since each function already knows the resultype it returns.
  2. IMO, i expect resulttype only for functions and not for pure cell references.
  3. I don't think the parser should format the result. User can now do that easily since they have access to resulttype