Best way to read a file and import it into HF? #805
Replies: 5 comments
-
Hi @mmahalwy HyperFormula works on an array of arrays.
So if you would like to pass JSON, you need to parse it and build an array out of the data first. Using variables is permitted, so once you import any function you can use it on a regular basis (replace the Ps. your data can be of different types https://handsontable.github.io/hyperformula/guide/types-of-values.html#getting-cell-type If I got your question wrong or missed anything, feel free to let me know. |
Beta Was this translation helpful? Give feedback.
-
@AMBudnik makes sense. I was looking forward to something like |
Beta Was this translation helpful? Give feedback.
-
We do not have this kind of functionality yet. But I also think that it would be a useful feature. |
Beta Was this translation helpful? Give feedback.
-
This is an essential feature this module is missing (or at least an example or documentation about how to do it). Here is my approach (TypeScript): import { ConfigParams, HyperFormula } from "hyperformula";
import XLSX from "xlsx";
// Load file using SheetJS (https://sheetjs.com/)
const route = path.join(process.cwd(), "files", "foo.xlsx");
const workbook = XLSX.readFile(route);
// Excel compatibility
const config: Partial<ConfigParams> = {
licenseKey: "gpl-v3",
evaluateNullToZero: true,
leapYear1900: true,
};
const hf = HyperFormula.buildEmpty(config);
// Excel compatibility
hf.addNamedExpression("FALSE", "=FALSE()");
hf.addNamedExpression("TRUE", "=TRUE()");
// Prevent calculations and evaluations while building
hf.suspendEvaluation();
// Build HyperFormula sheets
for (const name of workbook.SheetNames) {
const sheet = workbook.Sheets[name];
const hf_sheet = hf.addSheet(name);
const hf_sheetId = hf.getSheetId(hf_sheet)!;
for (const [key, cell] of Object.entries(sheet)) {
if (key.startsWith("!")) continue; // skip meta data
const address = hf.simpleCellAddressFromString(key, hf_sheetId);
if (cell.f) {
const value = `=${cell.f}`;
hf.setCellContents(address, value);
} else {
const value = cell.v;
hf.setCellContents(address, value);
}
}
}
// Let's say you want to use a sheet named 'main'
const hf_sheetId = hf.getSheetId("main")!;
const hf_addressInput = hf.simpleCellAddressFromString("A1", hf_sheetId)!;
const hf_addressOutput = hf.simpleCellAddressFromString("A2", hf_sheetId)!;
// Modify input value
hf.setCellContents(hf_addressInput, "42");
// Turn on evaluations
hf.resumeEvaluation();
// Get computed result
const result = hf.getCellValue(hf_addressOutput);
console.log(result) Regards |
Beta Was this translation helpful? Give feedback.
-
It doesn't make sense to have this in HyperFormula. HyperFormula is for cell calculations, not parsing files. Sheetjs and other libraries already does parsing of .xlsx files. What you should do is use something like You then call Should this be in HF/Handonstable docs? Probably because a lot of users will want to know the answer to this question. |
Beta Was this translation helpful? Give feedback.
-
Description
What's the best way to read a file and import it into HF?
Links
Should be here: https://handsontable.github.io/hyperformula/guide/advanced-usage.html#demo
Type of issue
Beta Was this translation helpful? Give feedback.
All reactions