manikk / js-file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

js-file

JSFile handles file creation, downloading and reading in the browser using the SheetJS/JS-XLXS & SheetJS/JS-XLX libraries.

Dependencies

js-file depends on Lodash, js-xlsx, js-xls

Installation

bower install js-file --save
<script src='bower_components/js-file/dist/js-file.min.js'></script>

Getting Started

To create an xlxs workbook

Create an instance of window.JSFile.Workbook, and pass the following data as the first argument:

[
    // repeat for each sheet
    {
        "name": "Sheet Name",
        "headers": [        
            // repeat for each header row
            [
                // repeat for each header
                {
                    "value": "First Name" // alternatives: displayName, display_name, name, label 
                }
            ]
        ],
        "data": [
            // repeat for each row
            [            
                // repeat for each column
                {
                    "value": "John"
                }
            ]
        ],
        "columns": [        
            // repeat for each column
            {
                "type": "string"
            }
        ]
    }
];

To download an xlxs workbook

Call window.JSFile.FileDownloader.downloadWorkbook with the result of the above method as the first argument, and whatever you want the file to be called as the second argument.

JSFile.Workbook

Model used to create workbook.

JSFile.FileDownloader API

downloadWorkbook(workbook, filename, file_extension)

Initiates a download when passed an instance of the JSFile.Workbook model.

param type description
workbook JSFile.Workbook Workbook created using JSFile.FileCreator.createWorkbook
filename string Name of the file (May include extension)
file_extension string Extension of the file (Optional)

initiateFileDownloadFallback(bob, filename)

Externally provided fallback method when Modernizr.adownload fails

param type description
blob Blob file blobs
filename string filename incuding extension

JSFile.FileReader API

getFileType(type)

Returns a file type (either xlxs, xlx, csv, ods or txt) by matching provided argument to list of known file types.

param type description
type string File type to use when matching against list

getWorksheetNames(file_data, file_type)

Returns a list of worksheet names.

param type description
file_data object Data to read and analyse
file_type string Type of file_data

fileToArray(file_data, file_type)

Converts a file into a javascript array. Currently supports xlxs, xlx, csv, ods, txt.

param type description
file_data object Data to read and convert to array
file_type string Type of file_data
worksheet_has_headings array boolean
group_by string how to group the data (row or column) *coming soon

About

License:MIT License


Languages

Language:JavaScript 100.0%