#!/usr/bin/env pwsh # https://docs.sheetjs.com/docs/demos/frontend/bundler/webpack $oldDir = Get-Location $tempDir = Join-Path -Path $env:TEMP -ChildPath "sheetjs-webpack" 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/dist/xlsx.full.min.js'; 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 = utils.json_to_sheet(rows); var 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 */ 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 */ writeFileXLSX(workbook, "Presidents.xlsx"); }); }); '@ | Out-File -FilePath "index.js" -Encoding utf8 @' module.exports = { /* entry point index.js */ entry: './index.js', /* write to index.min.js */ output: { path:__dirname, filename: './index.min.js' } } '@ | Out-File -FilePath "webpack.config.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 $packageJson = Get-Content package.json -Raw -Encoding UTF8 | ConvertFrom-Json $packageJson.PSObject.Properties.Remove("type") $packageJson | ConvertTo-Json -Depth 20 | Set-Content package.json -Encoding ASCII ## Webpack 2.x npx -y webpack@2.x -p node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "index.min.js" -Force Remove-Item -Path "Presidents.xlsx" -Force ## Webpack 3.x npx -y webpack@3.x -p node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "index.min.js" -Force Remove-Item -Path "Presidents.xlsx" -Force ## Webpack 4.x and 5.x do not need to import the standalone build $indexContent = Get-Content "index.js" -Raw $indexContent = $indexContent -replace '/dist/xlsx.full.min.js', '' $indexContent | Out-File -FilePath "index.js" -Encoding utf8 ## Webpack 4.x npm i --save webpack@4.x webpack-cli@4.x npx -y webpack --mode=production node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "index.min.js" -Force Remove-Item -Path "Presidents.xlsx" -Force ## Webpack 5.x npm i --save webpack@5.x webpack-cli@5.x npx -y webpack --mode=production node test.js npx -y xlsx-cli Presidents.xlsx | Select-Object -First 3 Remove-Item -Path "index.min.js" -Force Remove-Item -Path "Presidents.xlsx" -Force Set-Location $oldDir Remove-Item -Path $tempDir -Recurse -Force