57 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
		
		
			
		
	
	
			57 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
|  | #!/bin/bash
 | ||
|  | # https://docs.sheetjs.com/docs/getting-started/installation/bun | ||
|  | 
 | ||
|  | cd /tmp | ||
|  | rm -rf sheetjs-bun-dle | ||
|  | mkdir sheetjs-bun-dle | ||
|  | cd sheetjs-bun-dle | ||
|  | echo "{}" > package.json | ||
|  | 
 | ||
|  | bun install https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz || bun install xlsx@https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz | ||
|  | 
 | ||
|  | cat <<EOF >SheetJSBun.js | ||
|  | import * as XLSX from 'xlsx'; | ||
|  | import * as fs from 'fs'; | ||
|  | XLSX.set_fs(fs); | ||
|  | 
 | ||
|  | /* fetch JSON data and parse */ | ||
|  | const url = "https://docs.sheetjs.com/executive.json"; | ||
|  | const raw_data = await (await fetch(url)).json(); | ||
|  | 
 | ||
|  | /* filter for the Presidents */ | ||
|  | const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez")); | ||
|  | 
 | ||
|  | /* sort by first presidential term */ | ||
|  | prez.forEach(row => row.start = row.terms.find(term => term.type === "prez").start); | ||
|  | prez.sort((l,r) => l.start.localeCompare(r.start)); | ||
|  | 
 | ||
|  | /* flatten objects */ | ||
|  | const rows = prez.map(row => ({ | ||
|  |   name: row.name.first + " " + row.name.last, | ||
|  |   birthday: row.bio.birthday | ||
|  | })); | ||
|  | 
 | ||
|  | /* generate worksheet and workbook */ | ||
|  | const worksheet = XLSX.utils.json_to_sheet(rows); | ||
|  | const workbook = XLSX.utils.book_new(); | ||
|  | XLSX.utils.book_append_sheet(workbook, worksheet, "Dates"); | ||
|  | 
 | ||
|  | /* fix headers */ | ||
|  | XLSX.utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" }); | ||
|  | 
 | ||
|  | /* calculate column width */ | ||
|  | const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10); | ||
|  | worksheet["!cols"] = [ { wch: max_width } ]; | ||
|  | 
 | ||
|  | /* create an XLSX file and try to save to Presidents.xlsx */ | ||
|  | XLSX.writeFile(workbook, "Presidents.xlsx"); | ||
|  | EOF | ||
|  | 
 | ||
|  | bun build --target=bun SheetJSBun.js --outfile=app.js | ||
|  | rm -f package.json bun.lockb bun.lock SheetJSBun.js | ||
|  | rm -rf ./node_modules | ||
|  | 
 | ||
|  | bun app.js | ||
|  | bunx xlsx-cli Presidents.xlsx | ||
|  | 
 |