From 409581b317616d9e1cb133914651f6b8a0839dbd Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 15 May 2017 11:48:44 -0400 Subject: [PATCH] TS write test run `make tslint` to run TS checks --- Makefile | 7 +- tests/write.js | 21 +++--- types/index.d.ts | 56 +++++++++++----- types/tslint.json | 8 +++ types/write.ts | 153 ++++++++++++++++++++++++++++++++++++++++++++ types/xlsx-tests.ts | 14 ++-- 6 files changed, 224 insertions(+), 35 deletions(-) create mode 100644 types/tslint.json create mode 100644 types/write.ts diff --git a/Makefile b/Makefile index 9abaf2e..9dcd6a0 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ dist-deps: ## Copy dependencies for distribution aux: $(AUXTARGETS) .PHONY: bytes -bytes: ## display minified and gzipped file sizes +bytes: ## Display minified and gzipped file sizes for i in dist/xlsx.min.js dist/xlsx.{core,full}.min.js; do printj "%-30s %7d %10d" $$i $$(wc -c < $$i) $$(gzip --best --stdout $$i | wc -c); done .PHONY: graph @@ -156,6 +156,11 @@ old-lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks @jscs $(TARGET) $(AUXTARGETS) if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi +.PHONY: tslint +tslint: $(TARGET) ## Run typescript checks + #@npm install dtslint typescript + @npm run-script dtslint + .PHONY: flow flow: lint ## Run flow checker @flow check --all --show-all-errors diff --git a/tests/write.js b/tests/write.js index 775ef61..dc33eb8 100644 --- a/tests/write.js +++ b/tests/write.js @@ -1,11 +1,11 @@ /* writing feature test -- look for TEST: in comments */ -/* vim: set ts=2: */ +/* vim: set ts=2 ft=javascript: */ /* original data */ var data = [ - [1,2,3], + [1, 2, 3], [true, false, null, "sheetjs"], - ["foo","bar",new Date("2014-02-19T14:30Z"), "0.3"], + ["foo", "bar", new Date("2014-02-19T14:30Z"), "0.3"], ["baz", null, "qux", 3.14159], ["hidden"], ["visible"] @@ -26,15 +26,16 @@ var wsrows = [ {hpx: 16}, // "pixels" , {hpx: 24}, - {hidden:true}, // hide row - {hidden:false} + {hidden: true}, // hide row + {hidden: false} ]; console.log("Sheet Name: " + ws_name); -console.log("Data: "); for(var i=0; i!=data.length; ++i) console.log(data[i]); -console.log("Columns :"); for(i=0; i!=wscols.length;++i) console.log(wscols[i]); - - +console.log("Data: "); +var i = 0; +for(i = 0; i != data.length; ++i) console.log(data[i]); +console.log("Columns :"); +for(i = 0; i != wscols.length; ++i) console.log(wscols[i]); /* require XLSX */ if(typeof XLSX === "undefined") { try { XLSX = require('./'); } catch(e) { XLSX = require('../'); } } @@ -166,7 +167,7 @@ console.log(ws); 'sheetjs.prn', 'sheetjs.dif' ].forEach(function(r) { - if(typeof r == 'string') { + if(typeof r === 'string') { /* write file */ XLSX.writeFile(wb, r); /* test by reading back files */ diff --git a/types/index.d.ts b/types/index.d.ts index 999ec69..b5c6515 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,4 @@ -// Project: https://github.com/SheetJS/js-xlsx -// Definitions by: themauveavenger , Wolfgang Faust +/* index.d.ts (C) 2015-present SheetJS and contributors */ // TypeScript Version: 2.2 /** Attempts to read filename and parse */ @@ -14,15 +13,20 @@ export function write(data: WorkBook, opts?: WritingOptions): any; export const utils: Utils; export interface Properties { - LastAuthor?: string; + Title?: string; + Subject?: string; Author?: string; + Manager?: string; + Company?: string; + Category?: string; + Keywords?: string; + Comments?: string; + LastAuthor?: string; CreatedDate?: Date; ModifiedDate?: Date; Application?: string; AppVersion?: string; - Company?: string; DocSecurity?: string; - Manager?: string; HyperlinksChanged?: boolean; SharedDoc?: boolean; LinksUpToDate?: boolean; @@ -138,7 +142,7 @@ export interface WritingOptions { * Type of Workbook * @default 'xlsx' */ - bookType?: 'xlsx' | 'xlsm' | 'xlsb' | 'ods' | 'biff2' | 'fods' | 'csv'; + bookType?: 'xlsx' | 'xlsm' | 'xlsb' | 'biff2' | 'xlml' | 'ods' | 'fods' | 'csv' | 'txt' | 'sylk' | 'html' | 'dif' | 'prn'; /** * Name of Worksheet for single-sheet formats @@ -169,7 +173,13 @@ export interface WorkBook { * an object storing the standard properties. wb.Custprops stores custom properties. * Since the XLS standard properties deviate from the XLSX standard, XLS parsing stores core properties in both places. */ - Props: Properties; + Props?: Properties; + + Workbook?: WBProps; +} + +export interface WBProps { + Sheets?: any[]; } export interface ColInfo { @@ -180,7 +190,7 @@ export interface ColInfo { /** * width in Excel's "Max Digit Width", width*256 is integral */ - width: number; + width?: number; /** * width in screen pixels */ @@ -314,7 +324,7 @@ export interface Sheet { * object representing the worksheet */ export interface WorkSheet extends Sheet { - [cell: string]: WorkSheetCell | any; + [cell: string]: CellObject | any; '!cols'?: ColInfo[]; '!rows'?: RowInfo[]; '!merges'?: Range[]; @@ -326,9 +336,9 @@ export interface WorkSheet extends Sheet { * The Excel data type for a cell. * b Boolean, n Number, e error, s String, d Date */ -export type ExcelDataType = 'b' | 'n' | 'e' | 's' | 'd'; +export type ExcelDataType = 'b' | 'n' | 'e' | 's' | 'd' | 'z'; -export interface WorkSheetCell { +export interface CellObject { /** * The raw value of the cell. */ @@ -386,7 +396,7 @@ export interface WorkSheetCell { s?: object; } -export interface Cell { +export interface CellAddress { /** Column number */ c: number; /** Row number */ @@ -395,12 +405,14 @@ export interface Cell { export interface Range { /** Starting cell */ - s: Cell; + s: CellAddress; /** Ending cell */ - e: Cell; + e: CellAddress; } export interface Utils { + /* --- Cell Address Utilities --- */ + /** converts an array of arrays of JS data to a worksheet. */ aoa_to_sheet(data: T[], opts?: any): WorkSheet; @@ -410,26 +422,36 @@ export interface Utils { range?: any; header?: "A"|number|string[]; }): T[]; + /** Generates delimiter-separated-values output */ sheet_to_csv(worksheet: WorkSheet, options?: { FS: string, RS: string }): string; + /** Generates a list of the formulae (with value fallbacks) */ sheet_to_formulae(worksheet: WorkSheet): any; + /* --- Cell Address Utilities --- */ + /** Converts 0-indexed cell address to A1 form */ - encode_cell(cell: Cell): string; + encode_cell(cell: CellAddress): string; + /** Converts 0-indexed row to A1 form */ encode_row(row: number): string; + /** Converts 0-indexed column to A1 form */ encode_col(col: number): string; + /** Converts 0-indexed range to A1 form */ - encode_range(s: Cell, e: Cell): string; + encode_range(s: CellAddress, e: CellAddress): string; /** Converts A1 cell address to 0-indexed form */ - decode_cell(address: string): Cell; + decode_cell(address: string): CellAddress; + /** Converts A1 row to 0-indexed form */ decode_row(row: string): number; + /** Converts A1 column to 0-indexed form */ decode_col(col: string): number; + /** Converts A1 range to 0-indexed form */ decode_range(range: string): Range; } diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..43b1589 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "whitespace": false, + "no-sparse-arrays": false, + "no-consecutive-blank-lines": false + } +} diff --git a/types/write.ts b/types/write.ts new file mode 100644 index 0000000..a45f9b8 --- /dev/null +++ b/types/write.ts @@ -0,0 +1,153 @@ +/* writing feature test -- look for TEST: in comments */ +/* vim: set ts=2 ft=javascript: */ + +/* original data */ +let data = [ + [1, 2, 3], + [true, false, null, "sheetjs"], + ["foo", "bar", new Date("2014-02-19T14:30Z"), "0.3"], + ["baz", null, "qux", 3.14159], + ["hidden"], + ["visible"] +]; + +const ws_name = "SheetJS"; + +let wscols = [ + {wch: 6}, // "characters" + {wpx: 50}, // "pixels" + , + {hidden: true} // hide column +]; + +/* At 96 PPI, 1 pt = 1 px */ +let wsrows = [ + {hpt: 12}, // "points" + {hpx: 16}, // "pixels" + , + {hpx: 24}, + {hidden: true}, // hide row + {hidden: false} +]; + +console.log("Sheet Name: " + ws_name); +console.log("Data: "); +let i = 0; +for(i = 0; i !== data.length; ++i) console.log(data[i]); +console.log("Columns :"); +for(i = 0; i !== wscols.length; ++i) console.log(wscols[i]); + +/* require XLSX */ +import XLSX = require('xlsx'); + +/* blank workbook constructor */ + +let wb: XLSX.WorkBook = { SheetNames: [], Sheets: {} }; + + +/* convert an array of arrays in JS to a CSF spreadsheet */ +let ws = XLSX.utils.aoa_to_sheet(data, {cellDates:true}); + +/* TEST: add worksheet to workbook */ +wb.SheetNames.push(ws_name); +wb.Sheets[ws_name] = ws; + +/* TEST: simple formula */ +ws['C1'].f = "A1+B1"; +ws['C2'] = {t:'n', f:"A1+B1"}; + +/* TEST: single-cell array formula */ + +ws['D1'] = {t:'n', f:"SUM(A1:C1*A1:C1)", F:"D1:D1"}; + + +/* TEST: multi-cell array formula */ +ws['E1'] = {t:'n', f:"TRANSPOSE(A1:D1)", F:"E1:E4"}; +ws['E2'] = {t:'n', F:"E1:E4"}; +ws['E3'] = {t:'n', F:"E1:E4"}; +ws['E4'] = {t:'n', F:"E1:E4"}; +ws["!ref"] = "A1:E6"; + +/* TEST: column props */ +ws['!cols'] = wscols; + +/* TEST: row props */ +ws['!rows'] = wsrows; + +/* TEST: hyperlink note: Excel does not automatically style hyperlinks */ +ws['A3'].l = { Target: "http://sheetjs.com", Tooltip: "Visit us " }; + +/* TEST: built-in format */ +ws['B1'].z = "0%"; // Format Code 9 + +/* TEST: custom format */ +const custfmt = "\"This is \"\\ 0.0"; +ws['C2'].z = custfmt; + +/* TEST: page margins */ +ws['!margins'] = { left:1.0, right:1.0, top:1.0, bottom:1.0, header:0.5, footer:0.5 }; + +console.log("JSON Data:"); +console.log(XLSX.utils.sheet_to_json(ws, {header:1})); + +/* TEST: hidden sheets */ +wb.SheetNames.push("Hidden"); +wb.Sheets["Hidden"] = XLSX.utils.aoa_to_sheet(["Hidden".split(""), [1,2,3]]); +wb.Workbook = {Sheets:[]}; +wb.Workbook.Sheets[1] = {Hidden:1}; + +/* TEST: properties */ +wb.Props = { + Title: "SheetJS Test", + Subject: "Tests", + Author: "Devs at SheetJS", + Manager: "Sheet Manager", + Company: "SheetJS", + Category: "Experimentation", + Keywords: "Test", + Comments: "Nothing to say here", + LastAuthor: "Not SheetJS", + CreatedDate: new Date(2017,1,19) +}; + +/* TEST: comments */ + +ws['A4'].c = []; +ws['A4'].c.push({a:"SheetJS",t:"I'm a little comment, short and stout!\n\nWell, Stout may be the wrong word"}); + + +/* TEST: sheet protection */ +ws['!protect'] = { + password:"password", + /* enable formatting rows and columns */ + formatRows:false, + formatColumns:false, + /* disable editing objects and scenarios */ + objects:true, + scenarios:true +}; + +console.log("Worksheet Model:"); +console.log(ws); + +const filenames: Array<[string]|[string, XLSX.WritingOptions]> = [ + ['sheetjs.xlsx', {bookSST:true}], + ['sheetjs.xlsm'], + ['sheetjs.xlsb'], + ['sheetjs.xls', {bookType:'biff2'}], + ['sheetjs.xml.xls', {bookType:'xlml'}], + ['sheetjs.ods'], + ['sheetjs.fods'], + ['sheetjs.slk'], + ['sheetjs.csv'], + ['sheetjs.txt'], + ['sheetjs.prn'], + ['sheetjs.dif'] +]; + +filenames.forEach((r) => { + /* write file */ + XLSX.writeFile(wb, r[0], r[1]); + /* test by reading back files */ + XLSX.readFile(r[0]); +}); diff --git a/types/xlsx-tests.ts b/types/xlsx-tests.ts index ab6fcc2..fc1b13d 100644 --- a/types/xlsx-tests.ts +++ b/types/xlsx-tests.ts @@ -1,11 +1,11 @@ -import xlsx = require('xlsx'); +import XLSX = require('xlsx'); -const options: xlsx.ParsingOptions = { +const options: XLSX.ParsingOptions = { cellDates: true }; -const workbook = xlsx.readFile('test.xlsx', options); -const otherworkbook = xlsx.readFile('test.xlsx', {type: 'file'}); +const workbook = XLSX.readFile('test.xlsx', options); +const otherworkbook = XLSX.readFile('test.xlsx', {type: 'file'}); console.log(workbook.Props.Author); @@ -20,6 +20,6 @@ interface Tester { age: number; } -const jsonvalues: Tester[] = xlsx.utils.sheet_to_json(firstworksheet); -const csv = xlsx.utils.sheet_to_csv(firstworksheet); -const formulae = xlsx.utils.sheet_to_formulae(firstworksheet); +const jsonvalues: Tester[] = XLSX.utils.sheet_to_json(firstworksheet); +const csv = XLSX.utils.sheet_to_csv(firstworksheet); +const formulae = XLSX.utils.sheet_to_formulae(firstworksheet);