#!/usr/bin/env pwsh # https://docs.sheetjs.com/docs/demos/frontend/bundler/requirejs # requires global puppeteer and express # npm i -g puppeteer express@4 $oldDir = Get-Location $tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-requirejs" if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Recurse -Force } New-Item -ItemType Directory -Path $tempDir | Out-Null Set-Location -Path $tempDir Invoke-WebRequest -Uri "https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js" -OutFile "xlsx.full.min.js" npm init -y npm i puppeteer express @' require(["xlsx"], function(XLSX) { document.getElementById("xport").addEventListener("click", function() { /* fetch JSON data and parse */ var url = "https://docs.sheetjs.com/executive.json"; fetch(url).then(function(res) { return res.json(); }).then(function(raw_data) { /* filter for the Presidents */ var prez = raw_data.filter(function(row) { return row.terms.some(function(term) { return term.type === "prez"; }); }); /* sort by first presidential term */ prez.forEach(function(row) { row.start = row.terms.find(function(term) { return term.type === "prez"; }).start; }); prez.sort(function(l,r) { return l.start.localeCompare(r.start); }); /* flatten objects */ var rows = prez.map(function(row) { return { name: row.name.first + " " + row.name.last, birthday: row.bio.birthday }; }); /* generate worksheet and workbook */ var worksheet = XLSX.utils.json_to_sheet(rows); var 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 */ var max_width = rows.reduce(function(w, r) { return Math.max(w, r.name.length); }, 10); worksheet["!cols"] = [ { wch: max_width } ]; /* create an XLSX file and try to save to Presidents.xlsx */ XLSX.writeFileXLSX(workbook, "Presidents.xlsx"); console.log(XLSX.utils.sheet_to_csv(worksheet)); }); }); }); '@ | Out-File -FilePath "SheetJSRequire.js" -Encoding utf8 @' ({ baseUrl: ".", name: "SheetJSRequire", paths: { xlsx: "./xlsx.full.min" }, out: "SheetJSRequire.min.js" }); '@ | Out-File -FilePath "build.js" -Encoding utf8 @' 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}); await page.goto('http://localhost:7262/', {waitUntil: 'domcontentloaded'}); /* wait for requirejs to request xlsx.full.min.js */ await page.waitForRequest(request => request.url().indexOf('xlsx.full.min.js') !== -1); await new Promise((res,rej) => setTimeout(res, 1000)); await page.click("#xport"); await new Promise((res,rej) => setTimeout(res, 2000)); await browser.close(); process.exit(); }); '@ | Out-File -FilePath "test1.js" -Encoding utf8 @' 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}); await page.goto('http://localhost:7262/optimized.html'); await page.click("#xport"); await new Promise((res,rej) => setTimeout(res, 1000)); await browser.close(); process.exit(); }); '@ | Out-File -FilePath "test2.js" -Encoding utf8 $versions = @("2.3.7", "2.1.22") foreach ($n in $versions) { Write-Host "$n Standalone" @"

SheetJS Presidents Demo

"@ | Out-File -FilePath "index.html" -Encoding utf8 node test1.js Write-Host "$n Optimizer" Remove-Item -Path "SheetJSRequire.min.js" -Force -ErrorAction Ignore #npx -p requirejs@$n r.js -o build.js # NOTE: `npx` issues in Windows necessitates this workaround npm install --save requirejs@$n node .\node_modules\requirejs\bin\r.js -o build.js @"

SheetJS Presidents Demo

"@ | Out-File -FilePath "optimized.html" -Encoding utf8 node test2.js } Set-Location $oldDir Remove-Item -Path $tempDir -Recurse -Force