docs.sheetjs.com/tests/data/pouchdb.sh

92 lines
2.8 KiB
Bash
Raw Normal View History

2026-01-09 03:51:22 +00:00
#!/bin/bash
# https://docs.sheetjs.com/docs/demos/data/pouchdb
cd /tmp
rm -rf sheetjs-pouch
mkdir sheetjs-pouch
cd sheetjs-pouch
# official url is https://github.com/nickcolley/getting-started-todo/archive/master.zip
curl -LO https://docs.sheetjs.com/pouchdb/master.zip
unzip master.zip
cd getting-started-todo-master
awk 'NR>1{print p} {p = $0}' js/app.js > export_code.js
cat >> export_code.js << 'EOF'
document.getElementById("xport").addEventListener("click", function() {
console.log("clicked");
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
const aoo = doc.rows.map(r => {
const { _id, _rev, ...rest } = r.doc;
return rest;
});
const ws = XLSX.utils.json_to_sheet(aoo);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSPouch.xlsx");
});
});
})();
EOF
cp export_code.js js/app.js
sed -i.bak 's|<body>|<body><script src="https:\/\/cdn.sheetjs.com\/xlsx-latest\/package\/dist\/xlsx.full.min.js"><\/script><button id="xport">Export!<\/button>|' index.html
npm init -y
npm i --save puppeteer express@4
cat >test.js <<EOF
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('./')
});
page.on('request', req => console.log(req.url()));
await page.goto('http://localhost:7262/');
await new Promise((res,rej) => setTimeout(res, 3000));
await page.focus('#new-todo');
await page.keyboard.type('JS');
await page.keyboard.press('Enter');
await new Promise((res,rej) => setTimeout(res, 1000));
await page.focus('#new-todo');
await page.keyboard.type('Sheet');
await page.keyboard.press('Enter');
await new Promise((res,rej) => setTimeout(res, 1000));
const toggles = await page.\$\$('.toggle');
await toggles[0].click();
await new Promise((res,rej) => setTimeout(res, 1000));
// Click export button
await page.click('#xport');
await new Promise((res,rej) => setTimeout(res, 3000));
await browser.close();
process.exit();
});
EOF
for version in 9.0.0 8.0.1 7.3.1 6.4.3 5.4.5 4.0.3 3.6.0; do
echo "PouchDB $version"
sed -i.bak "s|pouchdb/3.2.0/pouchdb.min.js|npm/pouchdb@${version}/dist/pouchdb.min.js|" index.html
node test.js
npx -y xlsx-cli SheetJSPouch.xlsx | head -n 3
rm -f SheetJSPouch.xlsx
cp index.html.bak index.html
done