Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpexcel.js problem #76

Closed
vlysytsia opened this issue Jun 27, 2014 · 26 comments
Closed

cpexcel.js problem #76

vlysytsia opened this issue Jun 27, 2014 · 26 comments

Comments

@vlysytsia
Copy link

Hi! I try use js-xlsx, bower and glup, but when I added 2 files to my progect

require('../vendors/js-xlsx/dist/jszip.js');
require('../vendors/js-xlsx/xlsx.js');

I got this error

{ [Error: module "./cptable" not found from "F:.......\vendors\js-xlsx\dist\cpexcel.js"]
plugin: 'gulp-browserify',
showStack: undefined,
name: 'Error',
message: 'module "./cptable" not found from "F:\.......\vendors\js-xlsx\dist\cpexcel.js"',
fileName: undefined,
lineNumber: undefined

I have cpexcel.js file in 'dist' folder.
Any idia how I can fix it? Thanks!

@vlysytsia vlysytsia changed the title bower problem cpexcel.js problem Jun 27, 2014
@SheetJSDev
Copy link
Contributor

The script is pulled from the codepage module, which itself is the concatenation of two scripts (the one with the tables and the one with the utilities)

The script was designed to run in the browser and in node. There is some logic in the utils script that calls require if the table is not defined. If you evaluate the script in a browser, the table is defined at the very beginning, so require is never run.

I'm not too familiar with browserify. Is there a way to indicate that the require call won't run? As a stop-gap measure, replace lines 803-809 with just the else clause:

cptable = factory(cptable);

@vlysytsia
Copy link
Author

Thanks for your answer, but I use bower and load scripts from github, so I can't edit these scripts.

@SheetJSDev
Copy link
Contributor

@VolodymyrL I pushed a change that tricks browserify. Does js-xlsx#0.7.6-h work for you?

@vlysytsia
Copy link
Author

@SheetJSDev I pulled updates but still have the same error - module "./cptable" not found from "F:.......\vendors\js-xlsx\dist\cpexcel.js"]

@SheetJSDev
Copy link
Contributor

Are you sure you pulled 0.7.6-h? The relevant line has been changed in 0.7.6-h. This is a simple browserify test on OSX 10.9.3:

$ bower install js-xlsx#0.7.6-h
bower js-xlsx#0.7.6-h           cached git://github.com/SheetJS/js-xlsx.git#0.7.6-h
bower js-xlsx#0.7.6-h         validate 0.7.6-h against git://github.com/SheetJS/js-xlsx.git#0.7.6-h
bower js-xlsx#0.7.6-h          install js-xlsx#0.7.6-h

js-xlsx#0.7.6-h bower_components/js-xlsx
$ grep require bower_components/js-xlsx/dist/cpexcel.js
    if(typeof require !== "undefined"){
      var cpt = require('./cpt' + 'able');        ### <---- that's the updated line
$ browserify --version
4.2.0
$ cat t.js
var XLSX = require('xlsx')
console.log(XLSX.version)
$ browserify -o tt.js t.js; echo $?
0                                                 ### <---- success

@vlysytsia
Copy link
Author

I resturted solution and got this results and this new error

{ [Error: module "jszip" not found from "F:\Work\CoENode\public\vendors\js-xlsx\xlsx.js"]

browserify --version
4.1.10

bower install js-xlsx#0.7.6-h
bower js-xlsx#0.7.6-h not-cached git://github.com/SheetJS/js-xlsx.git#0.7.6-h
bower js-xlsx#0.7.6-h resolve git://github.com/SheetJS/js-xlsx.git#0.7.6-h
bower js-xlsx#0.7.6-h download https://github.com/SheetJS/js-xlsx/archive/v0.7.6-h.tar.gz
bower js-xlsx#0.7.6-h extract archive.tar.gz
bower js-xlsx#0.7.6-h resolved git://github.com/SheetJS/js-xlsx.git#0.7.6-h
bower js-xlsx#0.7.6-h install js-xlsx#0.7.6-h

js-xlsx#0.7.6-h public\vendors\js-xlsx

grep require vendors/js-xlsx/dist/cpexcel.js
if(typeof require !== "undefined"){
var cpt = require('./cpt' + 'able');

@SheetJSDev
Copy link
Contributor

Adjusted the dependency but did not adjust the main repository :/ Please check against js-xlsx#0.7.6-i

Thanks for following up

@vlysytsia
Copy link
Author

browserify create vendors.js file without error but I got another errors in browser and it broke my app

Uncaught Error: Cannot find module 'jszip' vendors.js:1
Uncaught object vendors.js:1857

probably problem in the modules building

Thanks for update!

@SheetJSDev
Copy link
Contributor

@VolodymyrL Sorry for the general confusion, I don't generally use browserify. I usually just add script tags :)

@SheetJSDev
Copy link
Contributor

Were you able to resolve the issue?

Sent from my iPad

On Jun 27, 2014, at 11:38 AM, volodymyrl notifications@github.com wrote:

browserify create vendors.js file without error but I got another errors in browser and it broke my app

Uncaught Error: Cannot find module 'jszip' vendors.js:1
Uncaught object vendors.js:1857

probably problem in the modules building

Thanks for update!


Reply to this email directly or view it on GitHub.

@vlysytsia
Copy link
Author

@SheetJSDev , no yet

@SheetJSDev
Copy link
Contributor

Can you share a sample gulp + browserify example so I can look further?

@vlysytsia
Copy link
Author

@SheetJSDev I will try share sample example tomorrow.

And another questions, can you add example how use js-xlsx with standard input type=file. I try implemented it by myself but got errors. I will try repeat it and send error to you. Thanks!

@SheetJSDev
Copy link
Contributor

I would think the standard approach (add an event listener for the change event of the input element, get the file at event.target.files[0], and continue with the html5 drag and drop code) would work.

I just took the drag and drop code and changed a few names (target instead of dataTransfer), and this appears to work in chrome:

<input type="file" id="files" name="files"/>

<script>
function handleFile(e) {
  var files = e.target.files;
  var i,f;
  for (i = 0, f = files[i]; i != files.length; ++i) {
    var reader = new FileReader();
    var name = f.name;
    reader.onload = function(e) {
      var data = e.target.result;

      /* if binary string, read with type 'binary' */
      var wb = XLSX.read(data, {type: 'binary'});

      /* DO SOMETHING WITH workbook HERE */
      console.log(wb);
    };
    reader.readAsBinaryString(f);
  }
}

/* set up drag-and-drop event */
document.getElementById('files').addEventListener('change', handleFile, false);
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.7/xlsx.core.min.js"></script>

@vlysytsia
Copy link
Author

Thanks @SheetJSDev !

I had error with .addEventListener. Added jquery and sheet_to_json function. May will be helpful for someone

<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.7/xlsx.core.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
  function handleFile(e) {
    var files = e.target.files;
    var i,f;
    for (i = 0, f = files[i]; i != files.length; ++i) {
      var reader = new FileReader();
      var name = f.name;
      reader.onload = function(e) {
        var data = e.target.result;

        /* if binary string, read with type 'binary' */
        var result;
        var workbook = XLSX.read(data, {type: 'binary'});

        /* DO SOMETHING WITH workbook HERE */
        workbook.SheetNames.forEach(function(sheetName) {
            var roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
            if(roa.length > 0){
                result = roa;
            }
        });
        console.log(workbook);
        console.log(result);
      };
      reader.readAsBinaryString(f);
    }
  }

  /* set up drag-and-drop event */
  $(document).ready(function(){
    $('#files').change(handleFile);
  });
</script>


<input type="file" id="files" name="files"/>

@vlysytsia vlysytsia reopened this Jul 1, 2014
@vlysytsia
Copy link
Author

Hi @SheetJSDev did you add this fixes #76 (comment) to xlsx.core.min.js? If I try add this file to my project I get error 'Error: module "./dist/cpexcel" not found from' again.

@SheetJSDev
Copy link
Contributor

The original error was about cptable from cpexcel.js. Is this the same as the original issue?

@vlysytsia
Copy link
Author

Yes, first I inclused jszip.js and xlsx.js, but now I try add xlsx.core.min.js and get this error in cpexcel.js

@SheetJSDev
Copy link
Contributor

The dist files need to be readjusted since the relative paths are taken from the root directory of the repo rather than the dist path. I will push an update soon

@vlysytsia
Copy link
Author

Thanks

@SheetJSDev
Copy link
Contributor

We pushed version 0.8.7 with modified require paths. Please check whether this resolves the issue.

@jayabhar
Copy link

jayabhar commented Mar 5, 2017

I am still getting the same error.

Cannot resolve file or directory ./cptable in ./node_modules/xlsx/dist

@SheetJSDev
Copy link
Contributor

@jayabhar can you share a sample? I just checked and the demos seem to work

@jayabhar
Copy link

jayabhar commented Mar 6, 2017

I had another issue with my local configuration. With the latest version 0.8.7 along with web-pack demo config changes, it works perfect. Thanks for the support.

@jayabhar
Copy link

jayabhar commented Mar 6, 2017 via email

@SheetJSDev
Copy link
Contributor

@jayabhar Workbook object needs a SheetNames array of strings and a Sheets object whose keys are sheet names and whose values are worksheets. see https://github.com/SheetJS/js-xlsx#workbook--worksheet--cell-object-description for a description.

As a simple example, here's a workbook with 2 worksheets:

var wb = {SheetNames:[], Sheets:{}};

/* first worksheet */
var sheet1 = {};

/* text */
sheet1.A1 = {t:'s', v:"one"};
sheet1.A2 = {t:'s', v:"two"};

/* numbers */
sheet1.B1 = {t:'n', v:1};
sheet1.B2 = {t:'n', v:2};

/* set sheet range */
sheet1['!ref'] = "A1:B2"; 

/* add to workbook */
wb.SheetNames.push("Sheet1"); wb.Sheets["Sheet1"] = sheet1;

/* second worksheet */
var sheet2 = {};
sheet2.A1 = {t:'n', v:3};
sheet2.B1 = {t:'s', v:"three"};
sheet2.A2 = {t:'s', v:"four"};
sheet2.B2 = {t:'n', v:4};
sheet2['!ref'] = "A1:B2"; 
wb.SheetNames.push("Sheet2"); wb.Sheets["Sheet2"] = sheet2;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants