fortune-sheet icon indicating copy to clipboard operation
fortune-sheet copied to clipboard

Cell formats not applied when importing data

Open valpackett opened this issue 1 year ago • 1 comments

Describe the bug

When initializing the component with cells that only have a value and a desired format, but aren't pre-formatted, the raw value is displayed directly.

https://github.com/ruilisi/fortune-sheet/blob/e2527a5fe656e0c91fd788f238a8072c7d93acbe/packages/core/src/modules/format.ts#L317-L319

However it would make sense to generate and cache the m formatted value using the cell's format.

To Reproduce

Initialize with e.g.

  const [sheets, setSheets] = React.useState<Sheet[]>([{
    name: "Sheet1", 
    celldata: [
      { r: 0, c: 0, v: { v: "69420", ct: { "fa": "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)", "t": "n" } } }
    ]
  }]);

The currency format isn't applied until we edit the value.

Expected behavior

The cell appearing as $69,420.00 instead of 69420 immediately.

Additional context

LuckySheet actually was applying formats on initialization. I'm porting LuckyExcel, which didn't generate the m pre-cached text values itself (and it'd be annoying to generate them since the formatter isn't actually exposed in the API), and it worked fine on the original version.

As an ugly workaround I'm currently using setCellFormat with the existing formats everywhere:

      <Workbook key={key} ref={sheetRef} data={sheets} style={{ flex: '1' }} onChange={() => {
        // This handler is run when the sheets themselves change, *not* any piece of data inside them.
        for (const sheet of sheetRef.current.getAllSheets()) {
          if (!sheet.data) continue;
          for (let r = 0; r < sheet.data.length; r++)
            for (let c = 0; c < sheet.data[r].length; c++)
                if (typeof sheet.data[r][c]?.ct === 'object' && sheet.data[r][c].ct.t !== 's')
                  sheetRef.current.setCellFormat(r, c, 'ct', sheet.data[r][c].ct, { id: sheet.id });
        }
      }} />

valpackett avatar Feb 05 '24 03:02 valpackett

I believe that this issue closely mirrors #499, which is essentially the sheet needing manually calculated values of cells containing any types of formulae - since it updates the cells only upon editing something.

I have been using a hack to make the sheet set values programatically as a workaround, but it quickly runs into issues at scale. I believe the way to solve all of these issues without much effort would be to call jfrefreshgrid after the sheet is done loading for the first time.

sanchit3008 avatar Feb 06 '24 15:02 sanchit3008