handsontable / hyperformula

HyperFormula is an open-source headless spreadsheet for business web apps. It comes with over 400 formulas, CRUD operations, undo-redo, clipboard support, and sorting.
https://hyperformula.handsontable.com/
Other
1.97k stars 108 forks source link

ExcelJS loading function fixed #1362

Closed charanjit-singh closed 8 months ago

charanjit-singh commented 9 months ago

Page URL

https://hyperformula.handsontable.com/guide/file-import.html#import-xlsx-files

Improvement description

Instead of

function convertXlsxWorkbookToJavascriptArrays(workbook) {
  const workbookData = {};

  workbook.eachSheet((worksheet) => {
    const sheetData = [];

    worksheet.eachRow((row) => {
      const rowData = [];

      row.eachCell((cell) => {
        const cellData = cell.value.formula ? `=${cell.value.formula}` : cell.value;
        rowData.push(cellData);
      });

      sheetData.push(rowData);
    });

    workbookData[worksheet.name] = sheetData;
  })

  return workbookData;
}

using

function convertXlsxWorkbookToJavascriptArrays(workbook: Workbook) {
  const workbookData: {
    [sheetName: string]: any[]
  } = {};

  workbook.eachSheet((worksheet: Worksheet) => {
    const rows: any[] = [];

    worksheet.eachRow((row) => {
      const rowData: any[] = [];
      console.log({ row })

      row.eachCell((cell, colNumber) => {
        // const cellData = cell?.value?.formula ? `=${cell.value.formula}` : cell.value;
        let cellData: any = '';
        if (cell.type === ValueType.Formula) {
          if (cell?.formula) {
            cellData = `=${cell.formula}`
          }
        } else {
          cellData = cell.value;
        }
        // Set cell data in colNumber
        rowData[colNumber - 1] = cellData;
      });

      rows.push(rowData);
    });

    workbookData[worksheet.name] = rows;
  })
  return workbookData;
}

to set cell data in column number fixes the issue of formula evaluation.

sequba commented 9 months ago

Hi @charanjit-singh, thank you for this suggestion.

fixes the issue of formula evaluation.

Please describe what the issue with the current code is and how your change fixes it. If possible, provide a code example that reproduces the issue.

sequba commented 8 months ago

Closing due to inactivity.

@charanjit-singh, if you have more information about this issue, please re-open it.