/* sheetjs (C) SheetJS -- https://sheetjs.com */ import { useState, useCallback } from 'react'; import { StyleSheet, Text, Button, Alert, ScrollView } from 'react-native'; import { Table, Row, Rows, TableWrapper } from 'react-native-tabeller'; import { read, utils, WorkSheet } from 'xlsx'; const make_width = (ws: WorkSheet): number[] => { const aoa = utils.sheet_to_json(ws, { header: 1 }), res: number[] = []; aoa.forEach((r) => { r.forEach((c, C) => { res[C] = Math.max(res[C] || 60, String(c).length * 10); }); }); for (let C = 0; C < res.length; ++C) if (!res[C]) res[C] = 60; return res; }; function SheetJSExample(): JSX.Element { const [data, setData] = useState([ 'SheetJS'.split(''), [5, 4, 3, 3, 7, 9, 5], [8, 6, 7, 5, 3, 0, 9], ]); const [widths, setWidths] = useState( Array.from({ length: 7 }, () => 20) ); const importFile = useCallback(async () => { try { const ab = await ( await fetch('https://docs.sheetjs.com/pres.numbers') ).arrayBuffer(); const wb = read(ab); /* convert first worksheet to AOA */ const wsname = wb.SheetNames[0]; const ws = wb.Sheets[wsname]; const data = utils.sheet_to_json(ws, { header: 1 }); /* update state */ setData(data); setWidths(make_width(ws)); } catch (err) { Alert.alert('importFile Error', 'Error ' + ((err as any).message || err)); } }, []); return (