docs.sheetjs.com/tests/headless/puppeteer.sh
2025-05-27 18:50:10 -04:00

52 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# https://docs.sheetjs.com/docs/demos/net/headless#puppeteer
cd /tmp
rm -rf sheetjs-puppeteer
mkdir sheetjs-puppeteer
cd sheetjs-puppeteer
cat >SheetJSPuppeteer.js <<EOF
const fs = require("fs");
const puppeteer = require('puppeteer');
(async () => {
/* (1) Load the target page */
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('https://sheetjs.com/demos/table');
/* (2) Load the standalone SheetJS build from the CDN */
await page.addScriptTag({ url: 'https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js' });
/* (3) Run the snippet in browser and return data */
const bin = await page.evaluate(() => {
/* find first table */
var table = document.body.getElementsByTagName('table')[0];
/* call table_to_book on first table */
var wb = XLSX.utils.table_to_book(table);
/* generate XLSB and return binary string */
return XLSX.write(wb, {type: "binary", bookType: "xlsb"});
});
/* (4) write data to file */
fs.writeFileSync("SheetJSPuppeteer.xlsb", bin, { encoding: "binary" });
await browser.close();
})();
EOF
for v in 24 23 22 21 20 15 10; do
npm i --save https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz puppeteer@$v
npm ls | grep puppeteer
node SheetJSPuppeteer.js
npx -y xlsx-cli SheetJSPuppeteer.xlsb
rm SheetJSPuppeteer.xlsb
done