Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cell formats not applied when importing data #504

Open
valpackett opened this issue Feb 5, 2024 · 1 comment
Open

Cell formats not applied when importing data #504

valpackett opened this issue Feb 5, 2024 · 1 comment

Comments

@valpackett
Copy link
Contributor

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.

let value = getCellValue(r, c, d, "m");
if (value == null) {
value = getCellValue(r, c, d, "v");

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 });
        }
      }} />
@sanchit3008
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants