docs.sheetjs.com/tests/headless-puppeteer-deno.sh

45 lines
1.4 KiB
Bash
Raw Normal View History

2024-06-25 07:16:41 +00:00
#!/bin/bash
# https://docs.sheetjs.com/docs/demos/net/headless#puppeteer
cd /tmp
rm -rf sheetjs-puppeteer-deno
mkdir sheetjs-puppeteer-deno
cd sheetjs-puppeteer-deno
env PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts
cat >SheetJSPuppeteer.ts <<EOF
import puppeteer from "https://deno.land/x/puppeteer@16.2.0/mod.ts";
import { decode } from "https://deno.land/std/encoding/base64.ts"
/* (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-0.20.2/package/dist/xlsx.full.min.js' });
/* (3) Run the snippet in browser and return data */
const b64 = 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: "base64", bookType: "xlsb"});
});
/* (4) write data to file */
Deno.writeFileSync("SheetJSPuppeteer.xlsb", decode(b64));
await browser.close();
EOF
deno run -A --unstable SheetJSPuppeteer.ts
npx -y xlsx-cli SheetJSPuppeteer.xlsb