--- title: Eleventy pagination_prev: demos/net/index pagination_next: demos/mobile/index --- import current from '/version.js'; import CodeBlock from '@theme/CodeBlock'; [Eleventy](https://www.11ty.dev/docs) is a telemetry-free static site generator. The data pipeline can be augmented with custom data file parsers. [SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing data from spreadsheets. This demo uses Eleventy and SheetJS to pull data from a spreadsheet and display the content in a page. We'll explore how to load SheetJS libraries in a custom data file format parser and generate arrays of objects for use in pages. The following diagram depicts the workbook waltz: ```mermaid flowchart LR file[(workbook\nfile)] subgraph SheetJS operations buffer(NodeJS\nBuffer) aoo(array of\nobjects) end html{{HTML\nTABLE}} file --> |.eleventy.js\ncustom parser| buffer buffer --> |.eleventy.js\ncustom parser| aoo aoo --> |index.njk\ntemplate| html ``` :::tip No Telemetry The author has publicly stated that Eleventy does not embed any telemetry or data collection.[^1] ::: ## Integration Details The [SheetJS NodeJS module](/docs/getting-started/installation/nodejs) can be loaded in `.eleventy.js` and used in custom data file format parsers. ### Data File Parser Custom data file parsers must be registered in `.eleventy.js` The Eleventy config `addDataExtension` method[^2] accepts a list of file extensions and a parser configuration object. The parser object must include the options `read: true` and `encoding: null` . Eleventy will read files and pass raw `Buffer` objects to the parser callback. The `parser` callback can parse the raw `Buffer` data with the SheetJS `read` method[^3]. The method returns a workbook object[^4]. In this example, the parser will use the SheetJS `sheet_to_json` method[^5] to generate an array of objects from the data in the first worksheet: ```js title=".eleventy.js" const XLSX = require("xlsx"); /* list of file extensions */ const exts = [ "numbers", "xlsx", "xlsb", "xls" ].join(", "); module.exports = (eleventyConfig) => { eleventyConfig.addDataExtension(exts, { /* read file and pass raw Buffer object to parser */ // highlight-next-line encoding: null, read: true, /* parser callback */ parser: (contents) => { /* contents is the data stored as a Buffer */ // highlight-next-line const wb = XLSX.read(contents); /* generate array of row objects from first worksheet */ return XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); } }); }; ``` ### Usage Spreadsheet files added in the `_data` subdirectory are accessible from template files using the name stem. For example, [`pres.numbers`](https://sheetjs.com/pres.numbers) can be accessed using the variable `pres` in a template: ```liquid title="index.njk"
Name | Index | {% for row in pres %}
---|---|
{{ row.Name }} | {{ row.Index }} |