Skip to content

Commit

Permalink
Add new plugin to handle excel files (#2343)
Browse files Browse the repository at this point in the history
* Add new plugin to handle excel files

* Run format
  • Loading branch information
SheetJSDev authored and KyleAMathews committed Oct 5, 2017
1 parent d1f34cb commit 6b402fb
Show file tree
Hide file tree
Showing 15 changed files with 599 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/using-excel/.eslintrc
@@ -0,0 +1,8 @@
{
"env": {
"browser": true
},
"globals": {
"graphql": false
}
}
3 changes: 3 additions & 0 deletions examples/using-excel/.gitignore
@@ -0,0 +1,3 @@
public
.cache
node_modules
3 changes: 3 additions & 0 deletions examples/using-excel/README.md
@@ -0,0 +1,3 @@
# using-excel

https://using-excel.gatsbyjs.org
16 changes: 16 additions & 0 deletions examples/using-excel/gatsby-config.js
@@ -0,0 +1,16 @@
module.exports = {
siteMetadata: {
title: `gatsby-example-using-excel`,
description: `Blazing-fast React.js static site generator`,
},
plugins: [
`gatsby-transformer-excel`,
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/data`,
name: `data`,
},
},
],
}
18 changes: 18 additions & 0 deletions examples/using-excel/package.json
@@ -0,0 +1,18 @@
{
"name": "using-excel",
"private": true,
"description": "Gatsby example site using using-excel",
"author": "SheetJS <dev@sheetjs.com>",
"dependencies": {
"gatsby": "latest",
"gatsby-link": "latest",
"gatsby-source-filesystem": "latest",
"gatsby-transformer-excel": "latest"
},
"license": "MIT",
"main": "n/a",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build"
}
}
Binary file added examples/using-excel/src/data/letters.xlsx
Binary file not shown.
73 changes: 73 additions & 0 deletions examples/using-excel/src/pages/index.js
@@ -0,0 +1,73 @@
import React from "react"

class IndexComponent extends React.Component {
render() {
const data1 = this.props.data.allLettersXlsxSheet1.edges
const data2 = this.props.data.allLettersXlsxSheet2.edges
return (
<div>
<table>
<thead>
<tr>
<th colSpan="2">Sheet1</th>
</tr>
<tr>
<th>Letter</th>
<th>ASCII Value</th>
</tr>
</thead>
<tbody>
{data1.map((row, i) => (
<tr key={`${row.node.value} ${i}`}>
<td>{row.node.letter}</td>
<td>{row.node.value}</td>
</tr>
))}
</tbody>
</table>
<table>
<thead>
<tr>
<th colSpan="2">Sheet2</th>
</tr>
<tr>
<th>Letter</th>
<th>ASCII Value</th>
</tr>
</thead>
<tbody>
{data2.map((row, i) => (
<tr key={`${row.node.value} ${i}`}>
<td>{row.node.letter}</td>
<td>{row.node.value}</td>
</tr>
))}
</tbody>
</table>
</div>
)
}
}

export default IndexComponent

export const IndexQuery = graphql`
query IndexQuery {
allLettersXlsxSheet1 {
edges {
node {
letter
value
}
}
}
allLettersXlsxSheet2 {
edges {
node {
letter
value
}
}
}
}
`
3 changes: 3 additions & 0 deletions packages/gatsby-transformer-excel/.gitignore
@@ -0,0 +1,3 @@
/*.js
!index.js
yarn.lock
34 changes: 34 additions & 0 deletions packages/gatsby-transformer-excel/.npmignore
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
96 changes: 96 additions & 0 deletions packages/gatsby-transformer-excel/README.md
@@ -0,0 +1,96 @@
# gatsby-transformer-excel

Parses Excel files into JSON arrays.

## Install

`npm install --save gatsby-transformer-excel`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
`gatsby-transformer-excel`,
]
```

## Parsing algorithm

The parsing is powered by the [SheetJS / js-xlsx](https://git.io/xlsx) library.
Each row of each worksheet is converted into a node whose keys are determined
by the first row and whose type is determined by the name of the worksheet.

So if your project has a `letters.xlsx` with two worksheets:

```
------ Sheet1 ------
/| A | B |
-+---------+-------+
1| letter | value |
-+---------+-------+
2| a | 97 |
-+---------+-------+
3| b | 98 |
------ Sheet2 ------
/| A | B |
-+---------+-------+
1| letter | value |
-+---------+-------+
2| A | 65 |
-+---------+-------+
3| B | 66 |
```

the following nodes would be created:

```javascript
[
{ letter: 'a', value: 97, type: 'LettersXlsxSheet1' },
{ letter: 'b', value: 98, type: 'LettersXlsxSheet1' },
{ letter: 'A', value: 65, type: 'LettersXlsxSheet2' },
{ letter: 'B', value: 66, type: 'LettersXlsxSheet2' },
]
```

## How to query

You'd be able to query your letters like:

```graphql
{
allLettersXlsxSheet1 {
edges {
node {
letter
value
}
}
}
}
```

Which would return:

```javascript
{
allLettersXlsxSheet1: {
edges: [
{
node: {
letter: 'a'
value: 97
}
},
{
node: {
letter: 'b'
value: 98
}
},
]
}
}
```

1 change: 1 addition & 0 deletions packages/gatsby-transformer-excel/index.js
@@ -0,0 +1 @@
// noop
26 changes: 26 additions & 0 deletions packages/gatsby-transformer-excel/package.json
@@ -0,0 +1,26 @@
{
"name": "gatsby-transformer-excel",
"version": "0.1.0",
"description": "Gatsby transformer plugin for Excel spreadsheets",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__",
"prepublish": "cross-env NODE_ENV=production npm run build"
},
"keywords": [
"gatsby",
"excel",
"gatsby-plugin"
],
"author": "SheetJS <dev@sheetjs.com>",
"license": "MIT",
"dependencies": {
"babel-runtime": "^6.26.0",
"xlsx": "^0.11.5"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"cross-env": "^5.0.5"
}
}

0 comments on commit 6b402fb

Please sign in to comment.