Skip to content

Commit

Permalink
Merge pull request #1 from Siemienik/master
Browse files Browse the repository at this point in the history
Fixation propose
  • Loading branch information
jacekkoziol authored Jan 14, 2021
2 parents 003a150 + 44e3a5a commit de5d4c1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Renderer } from 'xlsx-renderer';
import * as Excel from 'exceljs';
import {CellTemplateDebugPool} from "xlsx-renderer/lib/CellTemplateDebugPool";

export const VM1: object = {
projects: [
Expand Down Expand Up @@ -55,8 +56,14 @@ export const VM1: object = {
};

export class GenerateXLSXFile {
private renderer: Renderer;
constructor(private templateName: string, private viewModel: any, private buttonId: string) {
const btn: HTMLElement | null = document.getElementById(buttonId);
// todo @siemienik, add information about Debug possibilities into Readme
// todo @siemienik, add information about extending CellTemplatePool.
// todo @siemienik, add logging cell address into console.log inside CellTemplateDebugPool.match();
this.renderer = new Renderer(new CellTemplateDebugPool());

console.log('Init');

if (btn) {
Expand All @@ -75,15 +82,21 @@ export class GenerateXLSXFile {
console.log('exportXLSX view model:: this.viewModel');
try {
const xlsxBlob: Blob = await this.onRetrieveTemplate();
const reader: FileReader = new FileReader();
reader.readAsArrayBuffer(xlsxBlob);
const fileReader: FileReader = new FileReader();
fileReader.readAsArrayBuffer(xlsxBlob);

fileReader.addEventListener('loadend', async (e: ProgressEvent<FileReader>) => {
const templateFileBuffer = fileReader.result;
if (templateFileBuffer instanceof ArrayBuffer) {
// todo @siemeinik, Add information about correct template factory
// todo @siemienik, Add feature which detect that template and output objects is same (Consider about thrown an error or warning, or do clone if possible and works properly)
// todo @siemienik, Add possibility to load from fileBuffer.
const templateFactory = () => { // All this logic must be provided into xlsx-renderer as a function
const workbook: Excel.Workbook = new Excel.Workbook();
return workbook.xlsx.load(templateFileBuffer);
};

reader.addEventListener('loadend', async (e: ProgressEvent<FileReader>) => {
if (reader.result instanceof ArrayBuffer) {
const renderer: Renderer = new Renderer();
const workbook: Excel.Workbook = new Excel.Workbook();
await workbook.xlsx.load(reader.result);
const result: Excel.Workbook = await renderer.render(() => Promise.resolve(workbook), this.viewModel);
const result: Excel.Workbook = await this.renderer.render(templateFactory, this.viewModel);
const buffer: Excel.Buffer = await result.xlsx.writeBuffer()
this.saveBlobToFile(new Blob([buffer]), `${Date.now()}_result_report.xlsx`);
}
Expand Down

0 comments on commit de5d4c1

Please sign in to comment.