@ohayojp.com/components
Version:
Common business components of ohayojp.
371 lines (363 loc) • 12.2 kB
JavaScript
import { __awaiter } from 'tslib';
import { HttpClient } from '@angular/common/http';
import { Injectable, NgZone, ɵɵdefineInjectable, ɵɵinject, Directive, Input, NgModule } from '@angular/core';
import { LazyService, OhayoConfigService, OhayoUtilModule } from '@ohayojp.com/util';
import { saveAs } from 'file-saver';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* Generated from: xlsx.types.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function XlsxExportOptions() { }
if (false) {
/**
* worksheets in the workbook, e.g:
* - `{ Sheet1: { A1: { t:"n", v:10000 } } }`
* - `[['1'], [1]]`
* @type {?}
*/
XlsxExportOptions.prototype.sheets;
/**
* save file name, default: `export.xlsx`
* @type {?|undefined}
*/
XlsxExportOptions.prototype.filename;
/** @type {?|undefined} */
XlsxExportOptions.prototype.opts;
/**
* triggers when saveas
* @type {?|undefined}
*/
XlsxExportOptions.prototype.callback;
}
/**
* @record
*/
function XlsxExportSheet() { }
if (false) {
/**
* arrays to a worksheet
* @type {?}
*/
XlsxExportSheet.prototype.data;
/**
* sheet name
* @type {?|undefined}
*/
XlsxExportSheet.prototype.name;
}
/**
* @record
*/
function XlsxExportResult() { }
if (false) {
/** @type {?} */
XlsxExportResult.prototype.filename;
/** @type {?} */
XlsxExportResult.prototype.wb;
}
/**
* @fileoverview added by tsickle
* Generated from: xlsx.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class XlsxService {
/**
* @param {?} http
* @param {?} lazy
* @param {?} configSrv
* @param {?} ngZone
*/
constructor(http, lazy, configSrv, ngZone) {
this.http = http;
this.lazy = lazy;
this.ngZone = ngZone;
this.cog = (/** @type {?} */ (configSrv.merge('xlsx', {
url: '//cdn.bootcss.com/xlsx/0.15.6/xlsx.full.min.js',
modules: [],
})));
}
/**
* @private
* @return {?}
*/
init() {
return typeof XLSX !== 'undefined' ? Promise.resolve([]) : this.lazy.load([(/** @type {?} */ (this.cog.url))].concat((/** @type {?} */ (this.cog.modules))));
}
/**
* @private
* @param {?} data
* @param {?} options
* @return {?}
*/
read(data, options) {
/** @type {?} */
const ret = {};
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => {
/** @type {?} */
const wb = XLSX.read(data, options);
wb.SheetNames.forEach((/**
* @param {?} name
* @return {?}
*/
(name) => {
/** @type {?} */
const sheet = wb.Sheets[name];
ret[name] = XLSX.utils.sheet_to_json(sheet, { header: 1 });
}));
}));
return ret;
}
/**
* 导入Excel并输出JSON,支持 `<input type="file">`、URL 形式
* @param {?} fileOrUrl
* @param {?=} rABS 加载数据方式 `readAsBinaryString` (默认) 或 `readAsArrayBuffer`,[更多细节](http://t.cn/R3n63A0)
* @return {?}
*/
import(fileOrUrl, rABS = 'readAsBinaryString') {
return new Promise((/**
* @param {?} resolve
* @param {?} reject
* @return {?}
*/
(resolve, reject) => {
this.init()
.then((/**
* @return {?}
*/
() => {
// from url
if (typeof fileOrUrl === 'string') {
this.http.request('GET', fileOrUrl, { responseType: 'arraybuffer' }).subscribe((/**
* @param {?} res
* @return {?}
*/
(res) => {
this.ngZone.run((/**
* @return {?}
*/
() => resolve(this.read(new Uint8Array(res), { type: 'array' }))));
}), (/**
* @param {?} err
* @return {?}
*/
(err) => {
reject(err);
}));
return;
}
// from file
/** @type {?} */
const reader = new FileReader();
reader.onload = (/**
* @param {?} e
* @return {?}
*/
(e) => {
this.ngZone.run((/**
* @return {?}
*/
() => resolve(this.read(e.target.result, { type: 'binary' }))));
});
reader[rABS](fileOrUrl);
}))
.catch((/**
* @return {?}
*/
() => reject(`Unable to load xlsx.js`)));
}));
}
/**
* @param {?} options
* @return {?}
*/
export(options) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((/**
* @param {?} resolve
* @param {?} reject
* @return {?}
*/
(resolve, reject) => {
this.init()
.then((/**
* @return {?}
*/
() => {
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => {
/** @type {?} */
const wb = XLSX.utils.book_new();
if (Array.isArray(options.sheets)) {
((/** @type {?} */ (options.sheets))).forEach((/**
* @param {?} value
* @param {?} index
* @return {?}
*/
(value, index) => {
/** @type {?} */
const ws = XLSX.utils.aoa_to_sheet(value.data);
XLSX.utils.book_append_sheet(wb, ws, value.name || `Sheet${index + 1}`);
}));
}
else {
wb.SheetNames = Object.keys(options.sheets);
wb.Sheets = options.sheets;
}
if (options.callback)
options.callback(wb);
/** @type {?} */
const wbout = XLSX.write(wb, Object.assign({ bookType: 'xlsx', bookSST: false, type: 'array' }, options.opts));
/** @type {?} */
const filename = options.filename || 'export.xlsx';
saveAs(new Blob([wbout], { type: 'application/octet-stream' }), filename);
resolve({ filename, wb });
}));
}))
.catch((/**
* @param {?} err
* @return {?}
*/
err => reject(err)));
}));
});
}
/**
* 数据转符号名
* - `1` => `A`
* - `27` => `AA`
* - `703` => `AAA`
* @param {?} val
* @return {?}
*/
numberToSchema(val) {
/** @type {?} */
const startCode = 'A'.charCodeAt(0);
/** @type {?} */
let res = '';
do {
--val;
res = String.fromCharCode(startCode + (val % 26)) + res;
val = (val / 26) >> 0;
} while (val > 0);
return res;
}
}
XlsxService.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */
XlsxService.ctorParameters = () => [
{ type: HttpClient },
{ type: LazyService },
{ type: OhayoConfigService },
{ type: NgZone }
];
/** @nocollapse */ XlsxService.ɵprov = ɵɵdefineInjectable({ factory: function XlsxService_Factory() { return new XlsxService(ɵɵinject(HttpClient), ɵɵinject(LazyService), ɵɵinject(OhayoConfigService), ɵɵinject(NgZone)); }, token: XlsxService, providedIn: "root" });
if (false) {
/**
* @type {?}
* @private
*/
XlsxService.prototype.cog;
/**
* @type {?}
* @private
*/
XlsxService.prototype.http;
/**
* @type {?}
* @private
*/
XlsxService.prototype.lazy;
/**
* @type {?}
* @private
*/
XlsxService.prototype.ngZone;
}
/**
* @fileoverview added by tsickle
* Generated from: xlsx.directive.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class XlsxDirective {
/**
* @param {?} srv
*/
constructor(srv) {
this.srv = srv;
}
/**
* @return {?}
*/
_click() {
this.srv.export(this.data);
}
}
XlsxDirective.decorators = [
{
type: Directive, args: [{
selector: '[xlsx]',
exportAs: 'xlsx',
host: {
'(click)': '_click()',
},
},]
}
];
/** @nocollapse */
XlsxDirective.ctorParameters = () => [
{ type: XlsxService }
];
XlsxDirective.propDecorators = {
data: [{ type: Input, args: ['xlsx',] }]
};
if (false) {
/** @type {?} */
XlsxDirective.prototype.data;
/**
* @type {?}
* @private
*/
XlsxDirective.prototype.srv;
}
/**
* @fileoverview added by tsickle
* Generated from: xlsx.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const COMPONENTS = [XlsxDirective];
class XlsxModule {
}
XlsxModule.decorators = [
{
type: NgModule, args: [{
imports: [CommonModule, OhayoUtilModule],
declarations: [...COMPONENTS],
exports: [...COMPONENTS],
},]
}
];
/**
* @fileoverview added by tsickle
* Generated from: public_api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: xlsx.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { XlsxDirective, XlsxModule, XlsxService };
//# sourceMappingURL=xlsx.js.map