Skip to content

Commit dd6bb02

Browse files
committed
fixes for reading dates in Chrome
1 parent 43a7a5e commit dd6bb02

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,44 @@ data grid for previewing and modifying structured data in the web browser. The
16441644
`stox` function for converting from a workbook to x-spreadsheet data object.
16451645
<https://oss.sheetjs.com/sheetjs/x-spreadsheet> is a live demo.
16461646

1647+
<details>
1648+
<summary><b>Previewing data in a React data grid</b> (click to show)</summary>
1649+
1650+
[`react-data-grid`](https://npm.im/react-data-grid) is a data grid tailored for
1651+
react. It expects two properties: `rows` of data objects and `columns` which
1652+
describe the columns. For the purposes of massaging the data to fit the react
1653+
data grid API it is easiest to start from an array of arrays:
1654+
1655+
```js
1656+
import { useEffect, useState } from "react";
1657+
import DataGrid from "react-data-grid";
1658+
import { read, utils } from "xlsx";
1659+
1660+
const url = "https://oss.sheetjs.com/test_files/RkNumber.xls";
1661+
1662+
export default function App() {
1663+
const [columns, setColumns] = useState([]);
1664+
const [rows, setRows] = useState([]);
1665+
useEffect(() => {(async () => {
1666+
const wb = read(await (await fetch(url)).arrayBuffer(), { WTF: 1 });
1667+
1668+
/* use sheet_to_json with header: 1 to generate an array of arrays */
1669+
const data = utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], { header: 1 });
1670+
1671+
/* see react-data-grid docs to understand the shape of the expected data */
1672+
setColumns(data[0].map((r) => ({ key: r, name: r })));
1673+
setRows(data.slice(1).map((r) => r.reduce((acc, x, i) => {
1674+
acc[data[0][i]] = x;
1675+
return acc;
1676+
}, {})));
1677+
})(); });
1678+
1679+
return <DataGrid columns={columns} rows={rows} />;
1680+
}
1681+
```
1682+
1683+
</details>
1684+
16471685
<details>
16481686
<summary><b>Populating a database (SQL or no-SQL)</b> (click to show)</summary>
16491687

bits/20_jsutils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,18 @@ function fuzzynum(s/*:string*/)/*:number*/ {
139139
if(!isNaN(v = Number(ss))) return v / wt;
140140
return v;
141141
}
142+
var lower_months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
142143
function fuzzydate(s/*:string*/)/*:Date*/ {
143144
var o = new Date(s), n = new Date(NaN);
144145
var y = o.getYear(), m = o.getMonth(), d = o.getDate();
145146
if(isNaN(d)) return n;
147+
var lower = s.toLowerCase();
148+
if(lower.match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) {
149+
lower = lower.replace(/[^a-z]/g,"");
150+
if(lower.length > 3 && lower_months.indexOf(lower) == -1) return n;
151+
} else if(lower.match(/[a-z]/)) return n;
146152
if(y < 0 || y > 8099) return n;
147153
if((m > 0 || d > 1) && y != 101) return o;
148-
if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;
149154
if(s.match(/[^-0-9:,\/\\]/)) return n;
150155
return o;
151156
}

docbits/32_egress.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,46 @@ data grid for previewing and modifying structured data in the web browser. The
4040
`stox` function for converting from a workbook to x-spreadsheet data object.
4141
<https://oss.sheetjs.com/sheetjs/x-spreadsheet> is a live demo.
4242

43+
<details>
44+
<summary><b>Previewing data in a React data grid</b> (click to show)</summary>
45+
46+
[`react-data-grid`](https://npm.im/react-data-grid) is a data grid tailored for
47+
react. It expects two properties: `rows` of data objects and `columns` which
48+
describe the columns. For the purposes of massaging the data to fit the react
49+
data grid API it is easiest to start from an array of arrays.
50+
51+
This demo starts by fetching a remote file and using `XLSX.read` to extract:
52+
53+
```js
54+
import { useEffect, useState } from "react";
55+
import DataGrid from "react-data-grid";
56+
import { read, utils } from "xlsx";
57+
58+
const url = "https://oss.sheetjs.com/test_files/RkNumber.xls";
59+
60+
export default function App() {
61+
const [columns, setColumns] = useState([]);
62+
const [rows, setRows] = useState([]);
63+
useEffect(() => {(async () => {
64+
const wb = read(await (await fetch(url)).arrayBuffer(), { WTF: 1 });
65+
66+
/* use sheet_to_json with header: 1 to generate an array of arrays */
67+
const data = utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], { header: 1 });
68+
69+
/* see react-data-grid docs to understand the shape of the expected data */
70+
setColumns(data[0].map((r) => ({ key: r, name: r })));
71+
setRows(data.slice(1).map((r) => r.reduce((acc, x, i) => {
72+
acc[data[0][i]] = x;
73+
return acc;
74+
}, {})));
75+
})(); });
76+
77+
return <DataGrid columns={columns} rows={rows} />;
78+
}
79+
```
80+
81+
</details>
82+
4383
<details>
4484
<summary><b>Populating a database (SQL or no-SQL)</b> (click to show)</summary>
4585

misc/docs/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,41 @@ data grid for previewing and modifying structured data in the web browser. The
15381538
<https://oss.sheetjs.com/sheetjs/x-spreadsheet> is a live demo.
15391539

15401540

1541+
[`react-data-grid`](https://npm.im/react-data-grid) is a data grid tailored for
1542+
react. It expects two properties: `rows` of data objects and `columns` which
1543+
describe the columns. For the purposes of massaging the data to fit the react
1544+
data grid API it is easiest to start from an array of arrays:
1545+
1546+
```js
1547+
import { useEffect, useState } from "react";
1548+
import DataGrid from "react-data-grid";
1549+
import { read, utils } from "xlsx";
1550+
1551+
const url = "https://oss.sheetjs.com/test_files/RkNumber.xls";
1552+
1553+
export default function App() {
1554+
const [columns, setColumns] = useState([]);
1555+
const [rows, setRows] = useState([]);
1556+
useEffect(() => {(async () => {
1557+
const wb = read(await (await fetch(url)).arrayBuffer(), { WTF: 1 });
1558+
1559+
/* use sheet_to_json with header: 1 to generate an array of arrays */
1560+
const data = utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], { header: 1 });
1561+
1562+
/* see react-data-grid docs to understand the shape of the expected data */
1563+
setColumns(data[0].map((r) => ({ key: r, name: r })));
1564+
setRows(data.slice(1).map((r) => r.reduce((acc, x, i) => {
1565+
acc[data[0][i]] = x;
1566+
return acc;
1567+
}, {})));
1568+
})(); });
1569+
1570+
return <DataGrid columns={columns} rows={rows} />;
1571+
}
1572+
```
1573+
1574+
1575+
15411576
The [`database` demo](/demos/database/) includes examples of working with
15421577
databases and query results.
15431578

0 commit comments

Comments
 (0)