#!/usr/bin/env pwsh # https://docs.sheetjs.com/docs/demos/frontend/bundler/rollup $oldDir = Get-Location $tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-rollup" if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force } New-Item -ItemType Directory -Path $tempDir | Out-Null Set-Location -Path $tempDir npm init -y npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz @' import { utils, version, writeFileXLSX } from 'xlsx'; document.getElementById("xport").addEventListener("click", async() => { /* 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 = utils.json_to_sheet(rows); const workbook = utils.book_new(); utils.book_append_sheet(workbook, worksheet, "Dates"); /* fix headers */ 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 */ writeFileXLSX(workbook, "Presidents.xlsx"); }); '@ | Out-File -FilePath "index.js" -Encoding utf8 @'

SheetJS Presidents Demo

'@ | Out-File -FilePath "index.html" -Encoding utf8 npm i --save puppeteer express@4 @' const puppeteer = require('puppeteer'); const express = require('express'); const app = express(); app.use(express.static('./')); app.listen(7262, async() => { await new Promise((res,rej) => setTimeout(res, 1000)); const browser = await puppeteer.launch(); const page = await browser.newPage(); page.on("console", msg => console.log("PAGE LOG:", msg.text())); await page.setViewport({width: 1920, height: 1080}); const client = await page.target().createCDPSession(); await client.send('Browser.setDownloadBehavior', { behavior: 'allow', downloadPath: require("path").resolve('./') }); await page.goto('http://localhost:7262/'); await page.click("#xport"); await new Promise((res,rej) => setTimeout(res, 1000)); await browser.close(); process.exit(); }); '@ | Out-File -FilePath "test.js" -Encoding utf8 ## RollupJS 4.x npm i --save rollup@4.x @rollup/plugin-node-resolve npx -y rollup@4.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "bundle.js" -Force Remove-Item -Path "Presidents.xlsx" -Force ## RollupJS 3.x npm i --save rollup@3.x @rollup/plugin-node-resolve npx -y rollup@3.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "bundle.js" -Force Remove-Item -Path "Presidents.xlsx" -Force ## RollupJS 2.x npm i --save rollup@2.x @rollup/plugin-node-resolve npx -y rollup@2.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "bundle.js" -Force Remove-Item -Path "Presidents.xlsx" -Force ## RollupJS 1.x npm i --save rollup@1.x rollup-plugin-node-resolve npx -y rollup@1.x index.js --plugin rollup-plugin-node-resolve --file bundle.js --format iife node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "bundle.js" -Force Remove-Item -Path "Presidents.xlsx" -Force Set-Location $oldDir Remove-Item -Path $tempDir -Recurse -Force