51 lines
1.8 KiB
PowerShell
51 lines
1.8 KiB
PowerShell
|
|
#!/usr/bin/env pwsh
|
||
|
|
# https://docs.sheetjs.com/docs/getting-started/installation/deno
|
||
|
|
|
||
|
|
$oldDir = Get-Location
|
||
|
|
$tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-deno"
|
||
|
|
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force }
|
||
|
|
New-Item -ItemType Directory -Path $tempDir | Out-Null
|
||
|
|
Set-Location -Path $tempDir
|
||
|
|
|
||
|
|
@'
|
||
|
|
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
|
||
|
|
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs';
|
||
|
|
|
||
|
|
/* 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: any) => row.terms.some((term: any) => 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: any) => ({
|
||
|
|
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: number, r: any) => 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", { compression: true });
|
||
|
|
'@ | Out-File -FilePath "SheetJSDeno.ts" -Encoding utf8
|
||
|
|
|
||
|
|
deno run -A SheetJSDeno.ts
|
||
|
|
npx xlsx-cli Presidents.xlsx | Select-Object -First 10
|
||
|
|
|
||
|
|
Set-Location $oldDir
|
||
|
|
Remove-Item -Path $tempDir -Recurse -Force
|