xlsx2json

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2016 License: BSD-2-Clause Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Report Card License

xlsx2json

This is a simple command line utility for converting XML based Excel files to useful JSON objects. It uses Geoff Teale's xlsx golang package alone with Robert Krimen's Otto JavaScript interpretor.

The basic idea is that xlsx2json reads an Excel file and outputs a JSON expression for each row of the spreadsheet. The headings of the columns become the key names and the value is cell's content. If you provide a mapping functions written in JavaScript that will be use to process a row into a JSON object.

Like most Unix command line utilities xlsx2json will write to standard output (or standard error if there is a problem). Input is always assumed to be an Excel file's content, output is JSON blobs (one row is one blob).

Basic usage

    xlsx2json myfile.xlsx

This will write a series of JSON blobs to standard out (assuming no error, errors are written to standard error).

Because excel spreadsheets are typically complete documents the entire sheet(s) is read in before being converted and written out.

Controlling output

You can control the out through providing a JavaScript mapping function. The utility will execute a function a specified callback at the each row encountered. The entire row is always processed in one step. This allows you to make calculations and before the JSON output is rendered. The JavaScript callback should accept a row object as a parameter and return an object structured with a Path attribute, Source attribute (a JSON expression of the rendered content) and an Error attribute. If Path is empty then standard output is assumed. Otherwise xlsx2json will assume you want to write the JSON content in Source to the filepath provided. If Error is not an empty string then that will be written to standard Error and no output will be written to Path (or standard out) for that row.

Here's a basic example for writing myfile.xlsx as a series of JSON files mapped with myobjects.js

    xlsx2json -js row2obj.js -callback row2obj myfile.xlsx

row2obj.js might look something like this--

    /*
     *  A simplistic example of processing an Excel row with JavaScript
     *  The excel file has a Name, Email, Age and Journeyman Status columns.
     *  This script will generate an object per row with the following fields--
     *
     *  + Name (string)
     *  + Email (string)
     *  + Age (numeric value)
     *  + Journeyman (boolean)
     *
     */
    
    var i = 0;
    
    // Use the "Name" column to determine the filename by making it file system name friendly
    function slugify(s) {
        return 'testout/' + encodeURI(s.replace(" ", "_")) + '.json';
    }
    
    // Given a row object write the contents to a JSON blob file.
    // @param row - row comes in as an object with column names for property values
    // @return an object with a path, source and error values. path and error are strings
    // source is the modified object representing the updated object structure used for the final
    // JSON output.
    function row2obj(row) {
        if (row.Name === undefined) {
            return {"Path":"", "Source": "", "Error": "Missing Name property"}
        }
        // Add the count
        i++;
        row.Count = i;
    
        // Convert "%d" into a numeric value
        a = parseInt(row.Age, 10);
        row.Age = a;
        
        // Convert "1" and "0" to true and false
        if (row["Journeyman Status"] !== undefined && row["Journeyman Status"] == "1") {
            row.Journeyman = true;
        } else {
            row.Journeyman = false;
        }
        delete row["Journeyman Status"];
        return {
            "path": slugify(row.Name),
            "source": row,
            "error": ""
        };
    }

Installation

The golang compiler (version 1.7.3 or better) needs to be installed.

Clone the source repository on Github. Then change to the directory where your cloned repository is.

    git clone https://github.com/rsdoiel/xlsx2json
    cd xlsx2json

Next you need to have the xlsx and otto packages available. They are "go get"-able.

    go get -u github.com/tealeg/xlsx
    go get -u github.com/robertkrimen/otto

Then you can run

    go build -o bin/xlsx2json cmds/xlsx2json/xlsx2json.go

To create the xlsx2json command utility.

You can also just run "make" as their is a simple makefile for building and installation.

Installation

xlsx2json can be installed with the go get command.

    go get github.com/rsdoiel/xlsx2json/...

Documentation

Overview

Package xlsx2json wraps the github.com/tealag/xlsx package (used under a BSD License) and a fork of Robert Krimen's Otto Javascript engine (under an MIT License) providing an scriptable xlsx2json exporter, explorer and importer utility.

@author R. S. Doiel, <rsdoiel@gmail.com>

Copyright (c) 2016, R. S. Doiel All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

View Source
const (
	Version = "v0.0.4"

	LicenseText = `` /* 1302-byte string literal not displayed */

)

Version is the library and utilty version number

Variables

This section is empty.

Functions

func Run

func Run(js *ostdlib.JavaScriptVM, inputFilename string, sheetNo int, jsCallback string) ([]string, error)

Run runs the xlsx2json transform with optional JavaScript support. Continued processing can be achieved with subsequent calls to the JS VM. It returns the VM, an array of JSON encoded blobs and error.

Types

This section is empty.

Directories

Path Synopsis
cmds
xlsx2json command
xlsx2json package wraps the github.com/tealag/xlsx package (used under a BSD License) and a fork of Robert Krimen's Otto Javascript engine (under an MIT License) providing an scriptable xlsx2json exporter, explorer and importer utility.
xlsx2json package wraps the github.com/tealag/xlsx package (used under a BSD License) and a fork of Robert Krimen's Otto Javascript engine (under an MIT License) providing an scriptable xlsx2json exporter, explorer and importer utility.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址