0

我找不到任何人通过依赖注入将 SheetJS 安装到他们的 AngularJS 应用程序中。

我刚刚在这里找到了没有依赖注入的家伙,并且是一个笨拙的。但是如何将库导入到 AngularJS 1.x 应用程序中找不到与 Angular-SheetJS 相关的文档或关于 angularJS 的任何示例

这是带有“ui.grid”示例的依赖注入:任何人都可以帮助我注入和使用 SheetJS 依赖项吗?请紧急

angular.module('upload', [ 'ui.grid', 'ui.grid.resizeColumns', 'ui.grid.pagination', 'ui.grid.selection', 'ui.grid.exporter', 'ui.grid.moveColumns', 'ui.grid.pinning']) .controller('uploadCtrl', [ '$scope', '$state', '$stateParams', 'uiGridConstants', '$localStorage', function ($scope, $state, $stateParams, uiGridConstants, $localStorage)
4

1 回答 1

0

我认为您的项目配置了延迟加载,无法加载相关服务。我建议从服务中提取您需要的功能并将它们一起编写。像这样的东西:

        function Workbook() {
        if (!(this instanceof Workbook)) return new Workbook();
        this.SheetNames = [];
        this.Sheets = {};
    }

    function sheetFromArrayUiGrid(data, columns) {
        var ws = {};
        var range = {
            s: {
                c: 10000000,
                r: 10000000
            },
            e: {
                c: 0,
                r: 0
            }
        };
        var C = 0;
        columns.forEach(function (c) {
            var v = c.displayName || c.value || columns[i].name;
            addCell(range, v, 0, C, ws);
            C++;
        }, this);
        var R = 1;
        data.forEach(function (ds) {
            C = 0;
            ds.forEach(function (d) {
                var v = d.value;
                addCell(range, v, R, C, ws);
                C++;
            });
            R++;
        }, this);
        if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
        return ws;
    }

    /**
     *
     * @param {*} data
     * @param {*} columns
     */

    function datenum(v, date1904) {
        if (date1904) v += 1462;
        var epoch = Date.parse(v);
        return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
    }

    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;
    }

    function addCell(range, value, row, col, ws) {
        if (range.s.r > row) range.s.r = row;
        if (range.s.c > col) range.s.c = col;
        if (range.e.r < row) range.e.r = row;
        if (range.e.c < col) range.e.c = col;
        var cell = {
            v: value
        };
        if (cell.v == null) cell.v = '-';
        var cell_ref = XLSX.utils.encode_cell({
            c: col,
            r: row
        });

        if (typeof cell.v === 'number') cell.t = 'n';
        else if (typeof cell.v === 'boolean') cell.t = 'b';
        else if (cell.v instanceof Date) {
            cell.t = 'n';
            cell.z = XLSX.SSF._table[14];
            cell.v = datenum(cell.v);
        }else if(cell.v ==='string'){
            cell.s=font.bold===true
        }  else cell.t = 's';

        ws[cell_ref] = cell;
    }

    function getData(grid, rowTypes, colTypes, applyCellFilters) {
        var data = [];
        var rows;
        var columns;

        switch ( rowTypes ) {
            case uiGridExporterConstants.ALL:
                rows = grid.rows;
                break;
            case uiGridExporterConstants.VISIBLE:
                rows = grid.getVisibleRows();
                break;
            case uiGridExporterConstants.SELECTED:
                if ( grid.api.selection ){
                    rows = grid.api.selection.getSelectedGridRows();
                } else {
                    gridUtil.logError('selection feature must be enabled to allow selected rows to be exported');
                }
                break;
        }

        if ( colTypes === uiGridExporterConstants.ALL ){
            columns = grid.columns;
        } else {
            var leftColumns = grid.renderContainers.left ? grid.renderContainers.left.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
            var bodyColumns = grid.renderContainers.body ? grid.renderContainers.body.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
            var rightColumns = grid.renderContainers.right ? grid.renderContainers.right.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];

            columns = leftColumns.concat(bodyColumns,rightColumns);
        }

        rows.forEach( function( row, index ) {

            if (row.exporterEnableExporting !== false) {
                var extractedRow = [];


                columns.forEach( function( gridCol, index ) {
                    if ( (gridCol.visible || colTypes === uiGridExporterConstants.ALL ) &&
                        gridCol.colDef.exporterSuppressExport !== true &&
                        grid.options.exporterSuppressColumns.indexOf( gridCol.name ) === -1 ){
                        var cellValue = applyCellFilters ? grid.getCellDisplayValue( row, gridCol ) : grid.getCellValue( row, gridCol );
                        var extractedField = { value: grid.options.exporterFieldCallback( grid, row, gridCol, cellValue ) };
                        if ( gridCol.colDef.exporterPdfAlign ) {
                            extractedField.alignment = gridCol.colDef.exporterPdfAlign;
                        }
                        extractedRow.push(extractedField);
                    }
                });

                data.push(extractedRow);
            }
        });

        return data;
    }

    function getColumnHeaders(grid, colTypes) {
        var headers = [];
        var columns;

        if ( colTypes === uiGridExporterConstants.ALL ){
            columns = grid.columns;
        } else {
            var leftColumns = grid.renderContainers.left ? grid.renderContainers.left.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
            var bodyColumns = grid.renderContainers.body ? grid.renderContainers.body.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
            var rightColumns = grid.renderContainers.right ? grid.renderContainers.right.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];

            columns = leftColumns.concat(bodyColumns,rightColumns);
        }

        columns.forEach( function( gridCol, index ) {
            if ( gridCol.colDef.exporterSuppressExport !== true &&
                grid.options.exporterSuppressColumns.indexOf( gridCol.name ) === -1 ){
              //   gridCol.colDef.headerCellTemplate = '<a target="_blank" href="http://stackoverflow.com" align="middle" ><img src="http://cdn.flaticon.com/png/256/70689.png" align="middle" title="Describe Image Link Destination" width="70" height="20" /></a>'+'</div>';


                headers.push({
                    name: gridCol.field,
                    displayName: grid.options.exporterHeaderFilter ? ( grid.options.exporterHeaderFilterUseName ? grid.options.exporterHeaderFilter(gridCol.name) : grid.options.exporterHeaderFilter(gridCol.displayName) ) : gridCol.displayName,
                    width: gridCol.drawnWidth ? gridCol.drawnWidth : gridCol.width,
                    align: gridCol.colDef.type === 'number' ? 'right' : 'left'
            });
            }
        });

        return headers;
    }


    // gridCol.colDef.headerCellTemplate = '<a target="_blank" href="http://stackoverflow.com" align="middle" ><img src="http://cdn.flaticon.com/png/256/70689.png" align="middle" title="Describe Image Link Destination" width="70" height="20" /></a>'+'</div>';

    function   exporting(sheetName, gridApi, rowTypes, colTypes) {
        var columns = gridApi.grid.options.showHeader ? getColumnHeaders(gridApi.grid, colTypes) : [];
        var data = getData(gridApi.grid, rowTypes, colTypes);
        var fileName = gridApi.grid.options.exporterExcelFilename ? gridApi.grid.options.exporterExcelFilename : 'BI_REPORT';
        fileName += '.xlsx';
        var wb = new Workbook(),
            ws = sheetFromArrayUiGrid(data, columns);
        wb.SheetNames.push(sheetName);
        wb.Sheets[sheetName] = ws;
        var wbout = XLSX.write(wb, {
            bookType: 'xlsx',
            bookSST: false,
            type: 'binary',
            cellStyles: true
        });
        wbout.
             saveAs(new Blob([s2ab(wbout)], {
            type: 'application/octet-stream'
        }), fileName);
    }
于 2017-06-17T07:27:56.193 回答