| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  | /* sheetjs (C) SheetJS -- https://sheetjs.com */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import React, { useState, useCallback } from 'react'; | 
					
						
							| 
									
										
										
										
											2025-03-09 03:24:13 +00:00
										 |  |  |  | import { StyleSheet, Text, Button, Alert, Image, ScrollView } from 'react-native'; | 
					
						
							|  |  |  |  | import { Table, Row, Rows, TableWrapper } from 'react-native-tabeller'; | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  | 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 App(): JSX.Element { | 
					
						
							|  |  |  |  |   const [data, setData] = useState<any[]>([ | 
					
						
							|  |  |  |  |     "SheetJS".split(""), | 
					
						
							|  |  |  |  |     [5,4,3,3,7,9,5], | 
					
						
							|  |  |  |  |     [8,6,7,5,3,0,9] | 
					
						
							|  |  |  |  |   ]); | 
					
						
							|  |  |  |  |   const [widths, setWidths] = useState<number[]>(Array.from({length:7}, () => 20)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   const importFile = useCallback(async() => { | 
					
						
							|  |  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2024-04-26 04:16:13 +00:00
										 |  |  |  |       const ab = await (await fetch("https://docs.sheetjs.com/pres.numbers")).arrayBuffer(); | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  |       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 ( | 
					
						
							| 
									
										
										
										
											2025-03-08 16:55:39 +00:00
										 |  |  |  |       <ScrollView contentContainerStyle={styles.container}> | 
					
						
							|  |  |  |  |         <Text style={styles.welcome}> </Text> | 
					
						
							|  |  |  |  |         <Text style={styles.welcome}> <Image source={require("./logo.png")} style={styles.image}/>   SheetJS × React Native</Text> | 
					
						
							|  |  |  |  |         <Button onPress={importFile} title="Import data from a spreadsheet" color="#841584" /> | 
					
						
							|  |  |  |  |         <Text style={styles.bolded}>Current Data</Text> | 
					
						
							|  |  |  |  |         <ScrollView style={styles.table} horizontal={true} > | 
					
						
							|  |  |  |  |           <Table style={styles.table}> | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  |             <TableWrapper> | 
					
						
							| 
									
										
										
										
											2025-03-08 16:55:39 +00:00
										 |  |  |  |               <Row data={data[0]} style={styles.thead} textStyle={styles.text} widthArr={widths}/> | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  |             </TableWrapper> | 
					
						
							| 
									
										
										
										
											2025-03-08 16:55:39 +00:00
										 |  |  |  |             <ScrollView> | 
					
						
							|  |  |  |  |               <TableWrapper> | 
					
						
							|  |  |  |  |                 <Rows data={data.slice(1)} style={styles.tr} textStyle={styles.text} widthArr={widths}/> | 
					
						
							|  |  |  |  |               </TableWrapper> | 
					
						
							|  |  |  |  |             </ScrollView> | 
					
						
							|  |  |  |  |           </Table> | 
					
						
							|  |  |  |  |         </ScrollView> | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  |       </ScrollView> | 
					
						
							|  |  |  |  |   ); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | const styles = StyleSheet.create({ | 
					
						
							| 
									
										
										
										
											2025-03-09 03:24:13 +00:00
										 |  |  |  |   container: { backgroundColor: '#F5FCFF' }, | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  |   welcome: { fontSize: 20, textAlign: 'center', margin: 10 }, | 
					
						
							|  |  |  |  |   bolded: { textAlign: 'center', color: '#333333', marginBottom: 5, fontWeight: "bold" }, | 
					
						
							| 
									
										
										
										
											2025-03-10 21:58:01 +00:00
										 |  |  |  |   thead: { height: 40, backgroundColor: '#f1f8ff', borderWidth: 0.5, justifyContent: 'center' }, | 
					
						
							|  |  |  |  |   tr: { height: 30, borderWidth: 0.5, justifyContent: 'center' }, | 
					
						
							| 
									
										
										
										
											2025-03-08 16:55:39 +00:00
										 |  |  |  |   text: { marginLeft: 5 }, | 
					
						
							| 
									
										
										
										
											2023-04-24 08:50:42 +00:00
										 |  |  |  |   table: { width: "100%" }, | 
					
						
							|  |  |  |  |   image: { height: 16, width: 16 } | 
					
						
							|  |  |  |  | }); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-08 16:55:39 +00:00
										 |  |  |  | export default App; |