Skip to content

Commit

Permalink
version bump 0.12.6: BrtUid
Browse files Browse the repository at this point in the history
- `BrtUid` record (fixes SheetJS#1044 h/t @gustavosimil)
- `sheet_to_json` allow default for errors (fixes SheetJS#1035 h/t @arijitkanrar)
- docs and demos update
  • Loading branch information
SheetJSDev committed Mar 19, 2018
1 parent 975c7fb commit dc2128c
Show file tree
Hide file tree
Showing 35 changed files with 577 additions and 294 deletions.
3 changes: 1 addition & 2 deletions .spelling
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ Duktape
Nashorn

- demos/angular/README.md
angular-ui-grid
ui-grid
AngularJS

- demos/angular2/README.md
angular-cli
Expand Down
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ In the browser, just add a script tag:
<details>
<summary><b>CDN Availability</b> (click to show)</summary>

| CDN | URL |
|-----------:|:-----------------------------------------|
| `unpkg` | <https://unpkg.com/xlsx/> |
| `jsDelivr` | <https://jsdelivr.com/package/npm/xlsx> |
| `CDNjs` | <http://cdnjs.com/libraries/xlsx> |
| `packd` | <https://bundle.run/xlsx?name=XLSX> |
| CDN | URL |
|-----------:|:-------------------------------------------|
| `unpkg` | <https://unpkg.com/xlsx/> |
| `jsDelivr` | <https://jsdelivr.com/package/npm/xlsx> |
| `CDNjs` | <http://cdnjs.com/libraries/xlsx> |
| `packd` | <https://bundle.run/xlsx@latest?name=XLSX> |

`unpkg` makes the latest version available at:

Expand Down Expand Up @@ -190,7 +190,7 @@ $ bower install js-xlsx
The [`demos` directory](demos/) includes sample projects for:

**Frameworks and APIs**
- [`angular 1.x`](demos/angular/)
- [`angularjs`](demos/angular/)
- [`angular 2 / 4 / 5 and ionic`](demos/angular2/)
- [`meteor`](demos/meteor/)
- [`react and react-native`](demos/react/)
Expand Down Expand Up @@ -345,15 +345,32 @@ The `table_to_book` and `table_to_sheet` utility functions take a DOM TABLE
element and iterate through the child nodes.

```js
var worksheet = XLSX.utils.table_to_book(document.getElementById('tableau'));
var workbook = XLSX.utils.table_to_book(document.getElementById('tableau'));
/* DO SOMETHING WITH workbook HERE */
```

Multiple tables on a web page can be converted to individual worksheets:

```js
/* create new workbook */
var workbook = XLSX.utils.book_new();

/* convert table 'table1' to worksheet named "Sheet1" */
var ws1 = XLSX.utils.table_to_book(document.getElementById('table1'));
XLSX.utils.book_append_sheet(workbook, ws1, "Sheet1");

/* convert table 'table2' to worksheet named "Sheet2" */
var ws2 = XLSX.utils.table_to_book(document.getElementById('table2'));
XLSX.utils.book_append_sheet(workbook, ws2, "Sheet2");

/* workbook now has 2 worksheets */
```

Alternatively, the HTML code can be extracted and parsed:

```js
var htmlstr = document.getElementById('tableau').outerHTML;
var worksheet = XLSX.read(htmlstr, {type:'string'});
var workbook = XLSX.read(htmlstr, {type:'string'});
```

</details>
Expand All @@ -362,7 +379,7 @@ var worksheet = XLSX.read(htmlstr, {type:'string'});
<summary><b>Browser download file (ajax)</b> (click to show)</summary>

Note: for a more complete example that works in older browsers, check the demo
at <http://oss.sheetjs.com/js-xlsx/ajax.html>). The [`xhr` demo](demos/xhr/)
at <http://oss.sheetjs.com/js-xlsx/ajax.html>. The [`xhr` demo](demos/xhr/)
includes more examples with `XMLHttpRequest` and `fetch`.

```js
Expand Down Expand Up @@ -566,7 +583,7 @@ var desired_value = (desired_cell ? desired_cell.v : undefined);
<summary><b>Adding a new worksheet to a workbook</b> (click to show)</summary>

This example uses [`XLSX.utils.aoa_to_sheet`](#array-of-arrays-input) to make a
worksheet and appends the new worksheet to the workbook:
sheet and `XLSX.utils.book_append_sheet` to append the sheet to the workbook:

```js
var new_ws_name = "SheetJS";
Expand All @@ -578,14 +595,27 @@ var ws_data = [
];
var ws = XLSX.utils.aoa_to_sheet(ws_data);

/* Add the sheet name to the list */
wb.SheetNames.push(ws_name);
/* Add the worksheet to the workbook */
XLSX.utils.book_append_sheet(wb, ws, ws_name);
```

</details>

/* Load the worksheet object */
wb.Sheets[ws_name] = ws;
<details>
<summary><b>Creating a new workbook from scratch</b> (click to show)</summary>

The workbook object contains a `SheetNames` array of names and a `Sheets` object
mapping sheet names to sheet objects. The `XLSX.utils.book_new` utility function
creates a new workbook object:

```js
/* create a new blank workbook */
var wb = XLSX.utils.book_new();
```

The new workbook is blank and contains no worksheets. The write functions will
error if the workbook is empty.

</details>


Expand Down
5 changes: 5 additions & 0 deletions bin/xlsx.njs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ program
.option('-E, --eth', 'emit ETH to <sheetname> or <file>.eth (Ethercalc)')
.option('-t, --txt', 'emit TXT to <sheetname> or <file>.txt (UTF-8 TSV)')
.option('-r, --rtf', 'emit RTF to <sheetname> or <file>.txt (Table RTF)')
.option('-z, --dump', 'dump internal representation as JSON')

.option('-F, --field-sep <sep>', 'CSV field separator', ",")
.option('-R, --row-sep <sep>', 'CSV row separator', "\n")
Expand Down Expand Up @@ -158,6 +159,10 @@ if(program.listSheets) {
console.log((wb.SheetNames||[]).join("\n"));
process.exit(0);
}
if(program.dump) {
console.log(JSON.stringify(wb));
process.exit(0);
}

/* full workbook formats */
workbook_formats.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) {
Expand Down
2 changes: 1 addition & 1 deletion bits/01_version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
XLSX.version = '0.12.5';
XLSX.version = '0.12.6';
2 changes: 1 addition & 1 deletion bits/48_stybin.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function parse_sty_bin(data, themes, opts) {
case 0x046A: /* 'BrtSlicerStyleElement' */
case 0x0200: /* 'BrtTableStyleElement' */
case 0x082F: /* 'BrtTimelineStyleElement' */
/* case 'BrtUid' */
case 0x0C00: /* 'BrtUid' */
break;

case 0x0023: /* 'BrtFRTBegin' */
Expand Down
3 changes: 2 additions & 1 deletion bits/58_cmntbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function parse_comments_bin(data, opts)/*:Array<RawComment>*/ {
if(!c.t) c.t = "";
delete c.rfx; out.push(c); break;

/* case 'BrtUid': */
case 0x0C00: /* 'BrtUid' */
break;

case 0x0023: /* 'BrtFRTBegin' */
pass = true; break;
Expand Down
2 changes: 1 addition & 1 deletion bits/68_wsbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ function parse_ws_bin(data, _opts, idx, rels, wb/*:WBWBProps*/, themes, styles)/

case 0x01E5: /* 'BrtWsFmtInfo' */
break;
/* case 'BrtUid' */
case 0x00AF: /* 'BrtAFilterDateGroupItem' */
case 0x0284: /* 'BrtActiveX' */
case 0x0271: /* 'BrtBigName' */
Expand Down Expand Up @@ -620,6 +619,7 @@ function parse_ws_bin(data, _opts, idx, rels, wb/*:WBWBProps*/, themes, styles)/
case 0x0413: /* 'BrtSparkline' */
case 0x01AC: /* 'BrtTable' */
case 0x00AA: /* 'BrtTop10Filter' */
case 0x0C00: /* 'BrtUid' */
case 0x0032: /* 'BrtValueMeta' */
case 0x0816: /* 'BrtWebExtension' */
case 0x0415: /* 'BrtWsFmtInfoEx14' */
Expand Down
2 changes: 1 addition & 1 deletion bits/70_csheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ function parse_cs_bin(data, opts, idx/*:number*/, rels, wb/*::, themes, styles*/
if(val.name) wb.Sheets[idx].CodeName = val.name;
break;

/* case 'BrtUid': */
case 0x0232: /* 'BrtBkHim' */
case 0x028C: /* 'BrtCsPageSetup' */
case 0x029D: /* 'BrtCsProtection' */
case 0x02A7: /* 'BrtCsProtectionIso' */
case 0x0227: /* 'BrtLegacyDrawing' */
case 0x0228: /* 'BrtLegacyDrawingHF' */
case 0x01DC: /* 'BrtMargins' */
case 0x0C00: /* 'BrtUid' */
break;

case 0x0023: /* 'BrtFRTBegin' */
Expand Down
4 changes: 2 additions & 2 deletions bits/73_wbbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ function parse_wb_bin(data, opts)/*:WorkbookFile*/ {
case 0x0169: /* 'BrtPlaceholderName' */
break;

/* case 'BrtModelTimeGroupingCalcCol' */
/* case 'BrtUid' */
/* case 'BrtModelTimeGroupingCalcCol' */
case 0x0C00: /* 'BrtUid' */
case 0x0C01: /* 'BrtRevisionPtr' */
case 0x0817: /* 'BrtAbsPath15' */
case 0x0216: /* 'BrtBookProtection' */
Expand Down
1 change: 1 addition & 0 deletions bits/77_parsetab.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ var XLSBRecordEnum = {
/*::[*/0x085B/*::]*/: { n:"BrtBeginModelTimeGrouping" },
/*::[*/0x085C/*::]*/: { n:"BrtEndModelTimeGrouping" },
/*::[*/0x085D/*::]*/: { n:"BrtModelTimeGroupingCalcCol" },
/*::[*/0x0C00/*::]*/: { n:"BrtUid" },
/*::[*/0x0C01/*::]*/: { n:"BrtRevisionPtr" },
/*::[*/0xFFFF/*::]*/: { n:"" }
};
Expand Down
2 changes: 1 addition & 1 deletion bits/90_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/) {
v = val.v;
switch(val.t){
case 'z': if(v == null) break; continue;
case 'e': continue;
case 'e': v = void 0; break;
case 's': case 'd': case 'b': case 'n': break;
default: throw new Error('unrecognized type ' + val.t);
}
Expand Down
2 changes: 1 addition & 1 deletion demos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ can be installed with Bash on Windows or with `cygwin`.
### Included Demos

**Frameworks and APIs**
- [`angular 1.x`](angular/)
- [`angularjs`](angular/)
- [`angular 2 / 4 / 5 and ionic`](angular2/)
- [`meteor`](meteor/)
- [`react and react-native`](react/)
Expand Down
83 changes: 67 additions & 16 deletions demos/angular/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Angular 1
# AngularJS

The `xlsx.core.min.js` and `xlsx.full.min.js` scripts are designed to be dropped
into web pages with script tags:
Expand All @@ -7,14 +7,57 @@ into web pages with script tags:
<script src="xlsx.full.min.js"></script>
```

Strictly speaking, there should be no need for an angular demo! You can proceed
as you would with any other browser-friendly library. To make this meaningful,
we chose to show an integration with a common angular table component.
Strictly speaking, there should be no need for an Angular demo! You can proceed
as you would with any other browser-friendly library.


## Array of Objects

A common data table is often stored as an array of objects:

```js
$scope.data = [
{ Name: "Bill Clinton", Index: 42 },
{ Name: "GeorgeW Bush", Index: 43 },
{ Name: "Barack Obama", Index: 44 },
{ Name: "Donald Trump", Index: 45 }
];
```

This neatly maps to a table with `ng-repeat`:

```html
<table id="sjs-table">
<tr><th>Name</th><th>Index</th></tr>
<tr ng-repeat="row in data">
<td>{{row.Name}}</td>
<td>{{row.Index}}</td>
</tr>
</table>
```

The `$http` service can request binary data using the `"arraybuffer"` response
type coupled with `XLSX.read` with type `"array"`:

```js
$http({
method:'GET',
url:'https://sheetjs.com/pres.xlsx',
responseType:'arraybuffer'
}).then(function(data) {
var wb = XLSX.read(data.data, {type:"array"});
var d = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
$scope.data = d;
}, function(err) { console.log(err); });
```

The HTML table can be directly exported with `XLSX.utils.table_to_book`:

```js
var wb = XLSX.utils.table_to_book(document.getElementById('sjs-table'));
XLSX.writeFile(wb, "export.xlsx");
```

This demo uses angular-ui-grid to display a data table. The ui-grid does not
provide any way to modify the import button, so the demo includes a simple
directive for a HTML File Input control. It also includes a sample service for
export which adds an item to the export menu.

## Import Directive

Expand All @@ -35,10 +78,10 @@ app.directive("importSheetJs", [SheetJSImportDirective]);
- Define the directive:

```js
var SheetJSImportDirective = function() {
function SheetJSImportDirective() {
return {
scope: { },
link: function ($scope, $elm, $attrs) {
scope: { opts: '=' },
link: function ($scope, $elm) {
$elm.on('change', function (changeEvent) {
var reader = new FileReader();

Expand All @@ -54,12 +97,9 @@ var SheetJSImportDirective = function() {
});
}
};
};
}
```

The demo `SheetJSImportDirective` follows the prescription from the README for
File input controls using `readAsBinaryString`, converting to a suitable
representation and updating the scope.

## Export Service

Expand All @@ -85,11 +125,22 @@ XLSX.utils.book_append_sheet(wb, ws, "Presidents");
XLSX.writeFile(wb, "sheetjs.xlsx");
```

## Demo

`grid.html` uses `angular-ui-grid` to display a table. The library does not
provide any way to modify the import button, so the demo includes a simple
directive for a HTML File Input control. It also includes a sample service for
export which adds an item to the export menu.

The demo `SheetJSImportDirective` follows the prescription from the README for
File input controls using `readAsBinaryString`, converting to a suitable
representation and updating the scope.

`SheetJSExportService` exposes export functions for `XLSB` and `XLSX`. Other
formats are easily supported by changing the `bookType` variable. It grabs
values from the grid, builds an array of arrays, generates a workbook and forces
a download. By setting the `filename` and `sheetname` options in the ui-grid
a download. By setting the `filename` and `sheetname` options in the `ui-grid`
options, the output can be controlled.


[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)
Loading

0 comments on commit dc2128c

Please sign in to comment.