7.8 KiB
| title | sidebar_label | pagination_prev | pagination_next | sidebar_custom_props | ||
|---|---|---|---|---|---|---|
| Sheets in PouchDB | PouchDB | demos/cli/index | demos/local/index |
|
import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';
PouchDB is a pure JavaScript database with built-in synchronization features and offline support.
SheetJS is a JavaScript library for reading and writing data from spreadsheets.
This demo uses PouchDB and SheetJS to export database snapshots to spreadsheets and import bulk data from workbooks. We'll explore the subtleties of processing arrays of objects to mesh with both libraries.
The "Complete Example" section imbues the official "Todos" demo with the ability to export the list to XLSX workbooks.
:::note Tested Deployments
This demo was tested in the following environments:
| PouchDB | Date |
|---|---|
9.0.0 |
2026-01-08 |
8.0.1 |
2026-01-08 |
7.3.1 |
2026-01-08 |
6.4.3 |
2026-01-08 |
5.4.5 |
2026-01-08 |
4.0.3 |
2026-01-08 |
3.6.0 |
2026-01-08 |
:::
Integration Details
SheetJS CE offers standalone scripts, NodeJS modules, ESM modules, and other scripts. The "Installation" section covers a number of common deployment scenarios.
PouchDB ships with standalone scripts for browser use and NodeJS modules for use in server-side scripts1.
The PouchDB constructor returns a Database object.
Importing Data
Database#bulkDocs2 is the standard approach for bulk data import. The method
accepts "arrays of objects" that can be generated using the SheetJS
sheet_to_json method.
If rows do not include the _id parameter, the database will automatically
assign an ID per row. It is strongly recommended to generate the _id directly.
This method starts from a SheetJS worksheet object and uses
data from the first sheet. read and readFile can
generate workbook objects from files.
async function push_sheet_to_pouchdb(db, ws, _id_) {
/* generate array of objects */
const aoo = XLSX.utils.sheet_to_json(ws);
/* if a prefix is specified, add a unique _id to each row based on index */
if(typeof _id_ == "string") aoo.forEach((row, idx) => row._id = _id_ + idx);
/* perform query */
return await db.bulkDocs(aoo);
}
:::note pass
Existing data can be erased with Database#destroy.
:::
Exporting Data
Database#allDocs3 is the standard approach for bulk data export. Generated
row objects have additional _id and _rev keys that should be removed.
The SheetJS json_to_sheet
method can generate a worksheet. API functions can
generate a workbook object. The writeFile method
will attempt to download the generated workbook:
function export_pouchdb_to_xlsx(db) {
/* fetch all rows, including the underlying data */
db.allDocs({include_docs: true}, function(err, doc) {
/* pull the individual data rows */
const aoo = doc.rows.map(r => {
/* `rest` will include every field from `r` except for _id and _rev */
const { _id, _rev, ...rest } = r;
return rest;
});
/* generate worksheet */
const ws = XLSX.utils.json_to_sheet(aoo);
/* generate workbook and export */
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSPouch.xlsx");
});
}
:::caution pass
json_to_sheet expects an array of "flattened" objects where each value is a
simple data type that can be stored in a spreadsheet cell. If document objects
have a nested structure, integration code should post-process the data.
"Export Tutorial" processes data from an API and computes a few text values from the nested data.
:::
Complete Example
- Download the "Working Version" from the Getting Started guide.
The ZIP file should have MD5 checksum ac4da7cb0cade1be293ba222462f109c:
curl -LO https://github.com/nickcolley/getting-started-todo/archive/master.zip
md5sum master.zip || md5 master.zip
:::note pass
If the download is unavailable, a mirror is available at https://docs.sheetjs.com/pouchdb/master.zip :
curl -LO https://docs.sheetjs.com/pouchdb/master.zip
md5sum master.zip || md5 master.zip
:::
The second command will display the checksum:
ac4da7cb0cade1be293ba222462f109c master.zip
- Unzip the
master.zipfile and enter the folder:
unzip master.zip
cd getting-started-todo-master
- Edit
index.htmlto reference the SheetJS library and add a button:
{`\
Export!- Near the end of
index.html, look for a script tag referencing a CDN:
<script src="//cdn.jsdelivr.net/pouchdb/3.2.0/pouchdb.min.js"></script>
Upgrade PouchDB by changing the src attribute to the production build4:
<script src="//cdn.jsdelivr.net/npm/pouchdb@8.0.1/dist/pouchdb.min.js"></script>
- Add a
clickevent listener near the end ofjs/app.js, within the IIFE:
if (remoteCouch) {
sync();
}
// highlight-start
document.getElementById("xport").addEventListener("click", function() {
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(ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSPouch.xlsx");
});
});
// highlight-end
})();
:::info pass
The demo UI reads the todo items in descending order:
//------------------------------VVVVVVVVVVVVVVVV (descending order)
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
redrawTodosUI(doc.rows);
});
The new callback function also specifies descending: true to ensure that the
order of todo items in the export matches the list displayed in the webpage.
:::
- Start a local web server:
npx -y http-server .
The command will display a URL (typically http://localhost:8080) which can be
opened in a web browser.
Testing
-
Access the URL from step 5 with a web browser.
-
Add two items "js" and "Sheet". Mark "Sheet" as completed. The page should look like the following screenshot:
-
Click the "Export!" text at the top of the page. The site should create an export named "SheetJSPouch.xlsx"
-
Open the file in a spreadsheet editor. It should match the following table:
| title | completed |
|---|---|
| Sheet | TRUE |
| js | FALSE |
-
See "Setting up PouchDB" in the PouchDB documentation. ↩︎
-
See "Create/update a batch of documents" in the PouchDB API documentation ↩︎
-
See "Fetch a batch of documents" in the PouchDB API documentation ↩︎
-
The "Quick Start" section of "Download" in the PouchDB website describes the recommended CDN for PouchDB scripts. ↩︎
