Translating Node js-xlsx to Browser #665

Closed
opened 2017-05-23 21:45:58 +00:00 by johnothetree · 3 comments
johnothetree commented 2017-05-23 21:45:58 +00:00 (Migrated from github.com)

I'm having problems translating a working node script to work in browser (Angular 1.6 front-end, to be more exact).

Node:

var XLSX = require('xlsx')
var ws_name = "connect";
var wb = XLSX.readFile("test.xlsm", {bookVBA:true}); 
var ws = XLSX.utils.aoa_to_sheet([["sessionId", "objectId"]]); 
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
XLSX.writeFile(wb, "newpage.xlsm", {bookType:"xlsm"});

So far for the browser side, i've added the

I'm having problems translating a working node script to work in browser (Angular 1.6 front-end, to be more exact). Node: ```js var XLSX = require('xlsx') var ws_name = "connect"; var wb = XLSX.readFile("test.xlsm", {bookVBA:true}); var ws = XLSX.utils.aoa_to_sheet([["sessionId", "objectId"]]); wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; XLSX.writeFile(wb, "newpage.xlsm", {bookType:"xlsm"}); ``` So far for the browser side, i've added the <script> tag for the bower_component required, and i've tried switching from readFile() to read() (since readFile requires the readFileSync in node), but i'm unsure of how to get it to work correctly given the examples on here. What i'm trying to do: We have an xlsm file that has some VBA code ready to use. I need to open up the file, make a new sheet called "connect", put 2 fields within (sessionId and objectId), then save it as a new file name for downloading to the client (haven't started working with the FileSaver.js stuff yet, but that seems fairly straight forward). What i need to know is how to read in the file, since writing seems to be the same based on the examples given here. Thanks!
SheetJSDev commented 2017-05-23 22:06:53 +00:00 (Migrated from github.com)

If you have a fixed template, you can expose it at some endpoint (e.g. /static/test.xlsm route would download the original template) and have your page download the file with XHR. The following code block combines the "Browser download file (ajax)" example with the "Browser download file" example :

var url = "/static/test.xlsm"; // <-- change this to the route for your template
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(e) {
  var arraybuffer = oReq.response;
  var data = new Uint8Array(arraybuffer);
  var arr = new Array();
  for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
  var bstr = arr.join("");

  var wb = XLSX.read(bstr, {type:"binary", bookVBA:true});

  /* at this point, `wb` is a workbook */
  var ws_name = "connect";
  var ws = XLSX.utils.aoa_to_sheet([["sessionId", "objectId"]]); 
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;

  /* The following generates a download for "newpage.xlsm" using filesaver.js */
  var wopts = { bookType:'xlsm', type:'binary' };
  var wbout = XLSX.write(wb, wopts);
  saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "newpage.xlsm");
}

function s2ab(s) {
  var buf = new ArrayBuffer(s.length);
  var view = new Uint8Array(buf);
  for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}


oReq.send();

FYI use triple backticks for multi-line code blocks in github:

```js
var XLSX = require('xlsx')
```
If you have a fixed template, you can expose it at some endpoint (e.g. `/static/test.xlsm` route would download the original template) and have your page download the file with XHR. The following code block combines the ["Browser download file (ajax)" example](https://github.com/sheetjs/js-xlsx#parsing-workbooks) with the ["Browser download file" example](https://github.com/sheetjs/js-xlsx#writing-workbooks) : ```js var url = "/static/test.xlsm"; // <-- change this to the route for your template var oReq = new XMLHttpRequest(); oReq.open("GET", url, true); oReq.responseType = "arraybuffer"; oReq.onload = function(e) { var arraybuffer = oReq.response; var data = new Uint8Array(arraybuffer); var arr = new Array(); for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]); var bstr = arr.join(""); var wb = XLSX.read(bstr, {type:"binary", bookVBA:true}); /* at this point, `wb` is a workbook */ var ws_name = "connect"; var ws = XLSX.utils.aoa_to_sheet([["sessionId", "objectId"]]); wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; /* The following generates a download for "newpage.xlsm" using filesaver.js */ var wopts = { bookType:'xlsm', type:'binary' }; var wbout = XLSX.write(wb, wopts); saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "newpage.xlsm"); } function s2ab(s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } oReq.send(); ``` FYI use triple backticks for multi-line code blocks in github: ```js var XLSX = require('xlsx') ```
johnothetree commented 2017-05-24 15:21:24 +00:00 (Migrated from github.com)

That works perfectly! Thank you guys so much <3

That works perfectly! Thank you guys so much <3
MedinaGitHub commented 2019-03-05 02:42:22 +00:00 (Migrated from github.com)

i have this error

Error: Invalid HTML: could not find

i have this error Error: Invalid HTML: could not find <table>
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#665
No description provided.