sheetjs_sheetjs/demos/chrome
2022-05-17 17:48:05 -04:00
..
.gitignore version bump 0.12.7: chrome extension 2018-03-29 00:31:36 -04:00
content.js README cleanup in anticipation of node fetch 2022-02-05 09:04:02 -05:00
Makefile refresh browser tests 2022-05-17 17:48:05 -04:00
manifest.json version bump 0.12.7: chrome extension 2018-03-29 00:31:36 -04:00
popup.html version bump 0.12.7: chrome extension 2018-03-29 00:31:36 -04:00
popup.js version bump 0.12.8: sheetRows multiformat support 2018-04-06 02:39:48 -04:00
README.md version bump 0.12.7: chrome extension 2018-03-29 00:31:36 -04:00
table.js version bump 0.12.8: sheetRows multiformat support 2018-04-06 02:39:48 -04:00

Chrome and Chromium

This library is compatible with Chrome and Chromium extensions and should just work out of the box. Specific API support is listed in the Chrome extensions API documentation.

Generating Downloads

The writeFile function works in a Chrome or Chromium extension:

XLSX.writeFile(wb, "export.xlsx");

Under the hood, it uses the chrome.downloads API. "downloads" permission should be set in manifest.json:

"permissions": [
  "downloads"
]

Content Script Table Scraping

table_to_book and table_to_sheet can help build workbooks from DOM tables:

var tables = document.getElementsByTagName("table");
var wb = XLSX.utils.book_new();
for(var i = 0; i < tables.length; ++i) {
  var ws = XLSX.utils.table_to_sheet(tables[i]);
  XLSX.utils.book_append_sheet(wb, ws, "Table" + i);
}

Demo

The demo extension includes multiple features to demonstrate sample usage. Production extensions should include proper error handling.

Table Exporter

The content.js content script converts a table in the DOM to workbook object using the table_to_book utility function:

// event page script trigger
chrome.tabs.sendMessage(tab.id);
// content script convert
var wb = XLSX.utils.table_to_book(elt);
// event page script callback
XLSX.writeFile(wb, "export.xlsx");

Since the workbook object is a plain JS object, the object is sent back to an event page script which generates the file and attempts a download.

Bookmark Exporter

chrome.bookmarks API enables bookmark tree traversal. The "Export Bookmarks" button in the extension pop-up recursively walks the bookmark tree, pushes the bookmark URLs into a data array, and exports into a simple spreadsheet:

/* walk the bookmark tree */
function recurse_bookmarks(data, tree) {
  if(tree.url) data.push({Name: tree.title, Location: tree.url});
  (tree.children||[]).forEach(function(child) { recurse_bookmarks(data, child); });
}

/* get bookmark data */
chrome.bookmarks.getTree(function(res) {
  /* load into an array */
  var data = [];
  res.forEach(function(t) { recurse_bookmarks(data, t); });

  /* create worksheet */
  var ws = XLSX.utils.json_to_sheet(data, { header: ['Name', 'Location'] });

  /* create workbook and export */
  var wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, ws, 'Bookmarks');
  XLSX.writeFile(wb, "bookmarks.xlsx");
});

Analytics