sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

angular2 app cpexcel not found

Open Adtiv opened this issue 10 years ago • 5 comments

getting a 404 error for cpexcel.js and fs.js in my Anuglar2 app using the Angular2 cli. I can see cpexcel in my vendor folder but for some reason its not finding it. I downloaded the library through npm

Adtiv avatar Aug 10 '16 20:08 Adtiv

I got the same error, have not found a work around yet :/

tonymk avatar Sep 02 '16 12:09 tonymk

I've been using the library by including the cdn files in index.html

Adtiv avatar Sep 02 '16 18:09 Adtiv

I am having the same issue. I am using Typescript and I really liked having the typings available, but this is not possible to use because importing the typings will automatically import the library. Very annoying to jump through all these hoops when most libraries are very easy to include into a an A2 project.

Here is how I solved:

In angular-cli.json, I added the following:

      "scripts": [
        "../node_modules/xlsx/dist/xlsx.core.min.js"
      ],

Then I copied the typings index.ds.t (from @types/xlsx) file contents into my project.

From here, I was able to use by importing the following:

declare var XLSX: any;
import { ICell, IWorkBook, IWorkSheet, IWorkSheetCell, IRange } from './models/XLSX.models';

Then here is an example function from one of my Angular 2 services to create a worksheet:

  createSheetFromNestedArrays(header: any[], data: any[][], options?: {offsetRows?: number, offsetCols?: number}): IWorkSheet {
    let ws: IWorkSheet = {};
    const offsetRows = options && options.offsetRows ? options.offsetRows : 0;
    const offsetCols = options && options.offsetCols ? options.offsetCols : 0;
    let endRow = offsetRows;
    let endCol = 0;
    const startCol = offsetCols;
    let currRow = offsetRows;
    let currCol = startCol;
    var range: IRange = {s: {c:currCol, r:currRow}, e: {c:0, r:0 }};
    let maxCols: number[] = [];

    header.forEach(headerVal => {
      this.addCellToWs({c: currCol, r: currRow}, headerVal, ws);
      currCol ++;
    });
    // Increment row
    currRow ++;
    // reset column
    currCol = startCol;
    // Complete body
    data.forEach(rowArray => {
      rowArray.forEach(colData => {
        this.addCellToWs({c: currCol, r: currRow}, colData, ws);
        currCol ++;
        endRow ++;
      });
      currRow ++;
      maxCols.push(currCol);
      currCol = startCol;
    });
    range.e.r = endRow;
    range.e.c = Math.max(...maxCols);
    this.log.debug('ws', ws);
    this.log.debug('range', range);
    ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
  }

paustint avatar Jan 05 '17 04:01 paustint

The current recommendation is to use the minified files in the dist directory. We will be adding a demo shortly

SheetJSDev avatar Mar 23 '17 17:03 SheetJSDev

@paustint would it be easier to split off the definitions to something outside the module? I think there are DT defs but I cannot speak to the quality

SheetJSDev avatar Mar 23 '17 17:03 SheetJSDev