Intelligent GridDocumentation

Import

The grid reads .csv, .tsv, .txt and .xlsx files without a dependency: CSV with a standard (RFC 4180) parser, XLSX with a small zip reader on the browser's DecompressionStream.

input.addEventListener('change', async () => {
    const result = await grid.importFile(input.files[0]);
    console.log(result);
    // { rows: 120, fields: ['name', 'qty', 'due'], skipped: ['Color'], errors: [...], sheet: 'Sheet1' }
});

grid.importCSV('name;qty\nPen;3');                      // text
await grid.importXLSX(bytes, { sheet: 'Data' });         // Uint8Array or ArrayBuffer
grid.importTable([['name', 'qty'], ['Pen', 3]]);         // rows of cells you already have

Or let users do it from the navbar:

navbar: { import: true }                     // "Import" button: replaces the rows
navbar: { import: { mode: 'append' } }       // or any import options

The button asks for confirmation when unsaved changes would be lost, and reports the result in the status bar ("120 row(s) imported, 2 value(s) did not fit and were left blank").

Options

Option Default
mode 'replace' 'replace' works like setData(); 'append' adds the rows as one undo step
header true the first row holds titles. false fills the columns in order (formula columns skipped)
delimiter detected ,, ; or tab, from the first line (.tsv files use tab)
sheet first sheet .xlsx sheet name or 0-based index
createColumns when the grid has no columns add columns for titles that match none, with an inferred type

How values are read

The import event fires with the same result object.

Standalone readers

import { parseDelimited, detectDelimiter, readXlsx } from '@andmarruda/intelligent-grid';

parseDelimited('a,"b,c"\n1,2');        // [['a', 'b,c'], ['1', '2']]
const sheet = await readXlsx(bytes);  // { name, rows: [['Name', 'Qty'], ['Pen', 3]] }

Reading .xlsx needs DecompressionStream: every current browser, and Node 18 or later.

Edit this page on GitHub