2018-01-09 07:36:02 +00:00
|
|
|
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
|
|
|
/* vim: set ts=2: */
|
|
|
|
import { Component } from '@angular/core';
|
2021-10-03 01:41:36 +00:00
|
|
|
import { File } from '@ionic-native/file/ngx';
|
2018-01-09 07:36:02 +00:00
|
|
|
import * as XLSX from 'xlsx';
|
|
|
|
|
|
|
|
|
|
|
|
type AOA = any[][];
|
|
|
|
|
|
|
|
@Component({
|
2021-10-03 01:41:36 +00:00
|
|
|
selector: 'app-home',
|
|
|
|
//templateUrl: 'home.page.html',
|
|
|
|
styleUrls: ['home.page.scss'],
|
2018-01-09 07:36:02 +00:00
|
|
|
template: `
|
2021-10-03 01:41:36 +00:00
|
|
|
<ion-header>
|
|
|
|
<ion-toolbar>
|
|
|
|
<ion-title>SheetJS Ionic Demo</ion-title>
|
|
|
|
</ion-toolbar>
|
|
|
|
</ion-header>
|
|
|
|
|
|
|
|
<ion-content [fullscreen]="true">
|
|
|
|
<ion-header collapse="condense">
|
|
|
|
<ion-toolbar>
|
|
|
|
<ion-title>SheetJS Demo</ion-title>
|
|
|
|
</ion-toolbar>
|
|
|
|
</ion-header>
|
2018-01-09 07:36:02 +00:00
|
|
|
|
|
|
|
<ion-grid>
|
|
|
|
<ion-row *ngFor="let row of data">
|
|
|
|
<ion-col *ngFor="let val of row">
|
|
|
|
{{val}}
|
|
|
|
</ion-col>
|
|
|
|
</ion-row>
|
|
|
|
</ion-grid>
|
|
|
|
</ion-content>
|
|
|
|
|
2021-10-03 01:41:36 +00:00
|
|
|
<ion-footer padding>
|
2018-01-09 07:36:02 +00:00
|
|
|
<input type="file" (change)="onFileChange($event)" multiple="false" />
|
|
|
|
<button ion-button color="secondary" (click)="import()">Import Data</button>
|
|
|
|
<button ion-button color="secondary" (click)="export()">Export Data</button>
|
|
|
|
</ion-footer>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
export class HomePage {
|
|
|
|
data: any[][] = [[1,2,3],[4,5,6]];
|
2021-10-03 01:41:36 +00:00
|
|
|
constructor(public file: File) {}
|
2018-01-09 07:36:02 +00:00
|
|
|
|
2021-10-03 01:41:36 +00:00
|
|
|
read(ab: ArrayBuffer) {
|
2018-01-09 07:36:02 +00:00
|
|
|
/* read workbook */
|
2021-10-03 01:41:36 +00:00
|
|
|
const wb: XLSX.WorkBook = XLSX.read(new Uint8Array(ab), {type: 'array'});
|
2018-01-09 07:36:02 +00:00
|
|
|
|
|
|
|
/* grab first sheet */
|
|
|
|
const wsname: string = wb.SheetNames[0];
|
|
|
|
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
|
|
|
|
|
|
|
|
/* save data */
|
2021-10-03 01:41:36 +00:00
|
|
|
this.data = (XLSX.utils.sheet_to_json(ws, {header: 1}) as AOA);
|
2018-01-09 07:36:02 +00:00
|
|
|
};
|
|
|
|
|
2018-02-03 20:46:32 +00:00
|
|
|
write(): XLSX.WorkBook {
|
2018-01-09 07:36:02 +00:00
|
|
|
/* generate worksheet */
|
|
|
|
const ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(this.data);
|
|
|
|
|
|
|
|
/* generate workbook and add the worksheet */
|
|
|
|
const wb: XLSX.WorkBook = XLSX.utils.book_new();
|
|
|
|
XLSX.utils.book_append_sheet(wb, ws, 'SheetJS');
|
|
|
|
|
2018-02-03 20:46:32 +00:00
|
|
|
return wb;
|
2018-01-09 07:36:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* File Input element for browser */
|
|
|
|
onFileChange(evt: any) {
|
|
|
|
/* wire up file reader */
|
2021-10-03 01:41:36 +00:00
|
|
|
const target: DataTransfer = (evt.target as DataTransfer);
|
|
|
|
if (target.files.length !== 1) { throw new Error('Cannot use multiple files'); }
|
2018-01-09 07:36:02 +00:00
|
|
|
const reader: FileReader = new FileReader();
|
|
|
|
reader.onload = (e: any) => {
|
2021-10-03 01:41:36 +00:00
|
|
|
const ab: ArrayBuffer = e.target.result;
|
|
|
|
this.read(ab);
|
2018-01-09 07:36:02 +00:00
|
|
|
};
|
2021-10-03 01:41:36 +00:00
|
|
|
reader.readAsArrayBuffer(target.files[0]);
|
2018-01-09 07:36:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Import button for mobile */
|
|
|
|
async import() {
|
|
|
|
try {
|
|
|
|
const target: string = this.file.documentsDirectory || this.file.externalDataDirectory || this.file.dataDirectory || '';
|
|
|
|
const dentry = await this.file.resolveDirectoryUrl(target);
|
|
|
|
const url: string = dentry.nativeURL || '';
|
2021-10-03 01:41:36 +00:00
|
|
|
alert(`Attempting to read SheetJSIonic.xlsx from ${url}`);
|
|
|
|
const ab: ArrayBuffer = await this.file.readAsArrayBuffer(url, 'SheetJSIonic.xlsx');
|
|
|
|
this.read(ab);
|
2018-01-09 07:36:02 +00:00
|
|
|
} catch(e) {
|
|
|
|
const m: string = e.message;
|
2021-10-03 01:41:36 +00:00
|
|
|
alert(m.match(/It was determined/) ? 'Use File Input control' : `Error: ${m}`);
|
2018-01-09 07:36:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Export button */
|
|
|
|
async export() {
|
2018-02-03 20:46:32 +00:00
|
|
|
const wb: XLSX.WorkBook = this.write();
|
2021-10-03 01:41:36 +00:00
|
|
|
const filename = 'SheetJSIonic.xlsx';
|
2018-01-09 07:36:02 +00:00
|
|
|
try {
|
2018-02-03 20:46:32 +00:00
|
|
|
/* generate Blob */
|
|
|
|
const wbout: ArrayBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
|
|
|
|
|
|
|
|
/* find appropriate path for mobile */
|
2018-01-09 07:36:02 +00:00
|
|
|
const target: string = this.file.documentsDirectory || this.file.externalDataDirectory || this.file.dataDirectory || '';
|
|
|
|
const dentry = await this.file.resolveDirectoryUrl(target);
|
|
|
|
const url: string = dentry.nativeURL || '';
|
2018-02-03 20:46:32 +00:00
|
|
|
|
|
|
|
/* attempt to save blob to file */
|
2021-10-03 01:41:36 +00:00
|
|
|
await this.file.writeFile(url, filename, wbout, {replace: true});
|
2018-01-09 07:36:02 +00:00
|
|
|
alert(`Wrote to SheetJSIonic.xlsx in ${url}`);
|
|
|
|
} catch(e) {
|
2018-02-03 20:46:32 +00:00
|
|
|
if(e.message.match(/It was determined/)) {
|
|
|
|
/* in the browser, use writeFile */
|
|
|
|
XLSX.writeFile(wb, filename);
|
2021-10-03 01:41:36 +00:00
|
|
|
} else {
|
|
|
|
alert(`Error: ${e.message}`);
|
2018-02-03 20:46:32 +00:00
|
|
|
}
|
2018-01-09 07:36:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|