Skip to content

Commit

Permalink
Merge pull request #450 from jvalue/issue-449
Browse files Browse the repository at this point in the history
Switched SheetJS to ExcelJS
  • Loading branch information
rhazn authored Sep 7, 2023
2 parents 5259d90 + 0f87116 commit ea180f6
Show file tree
Hide file tree
Showing 6 changed files with 857 additions and 87 deletions.
30 changes: 18 additions & 12 deletions NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,19 +621,25 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## SheetJS
SheetJS Community Edition -- https://sheetjs.com/
## exceljs
The MIT License (MIT)

Copyright (C) 2012-present SheetJS LLC
Copyright (c) 2014-2019 Guyon Roche

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

http://www.apache.org/licenses/LICENSE-2.0
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 25 additions & 22 deletions libs/extensions/tabular/exec/src/lib/xlsx-interpreter-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

import { strict as assert } from 'assert';

import * as R from '@jvalue/jayvee-execution';
import {
AbstractBlockExecutor,
Expand All @@ -14,7 +12,7 @@ import {
implementsStatic,
} from '@jvalue/jayvee-execution';
import { IOType } from '@jvalue/jayvee-language-server';
import * as xlsx from 'xlsx';
import * as exceljs from 'exceljs';

@implementsStatic<BlockExecutorClass>()
export class XLSXInterpreterExecutor extends AbstractBlockExecutor<
Expand All @@ -32,26 +30,31 @@ export class XLSXInterpreterExecutor extends AbstractBlockExecutor<
context: ExecutionContext,
): Promise<R.Result<Workbook>> {
context.logger.logDebug(`Reading from XLSX file`);
const workBookFromFile = xlsx.read(file.content, {
dense: true,
raw: true,
});
const workBookFromFile = new exceljs.Workbook();
await workBookFromFile.xlsx.load(file.content);

const workbook = new Workbook();
for (const workSheetName of workBookFromFile.SheetNames) {
const workSheet = workBookFromFile.Sheets[workSheetName];
assert(
workSheet !== undefined,
`Failed to read sheet ${workSheetName} from Workbook.`,
);

/** Extract sheet into array of array structure as described in https://github.com/SheetJS/sheetjs/issues/1258#issuecomment-419129919 */
const workSheetDataArray: string[][] = xlsx.utils.sheet_to_json(
workSheet,
{ header: 1, raw: true, rawNumbers: false, defval: '' },
);

workbook.addSheet(workSheetDataArray, workSheetName);
}

workBookFromFile.eachSheet((workSheet) => {
const workSheetDataArray: string[][] = [];
workSheet.eachRow((row, rowNumber) => {
const cellValues: string[] = [];

// ExcelJS Rows and Columns are indexed from 1
// We reduce their index to match Sheets being zero indexed
row.eachCell(
{ includeEmpty: true },
(cell: exceljs.Cell, colNumber: number) => {
cellValues[colNumber - 1] = cell.text;
},
);

workSheetDataArray[rowNumber - 1] = cellValues;
});

workbook.addSheet(workSheetDataArray, workSheet.name);
});

return Promise.resolve(R.ok(workbook));
}
}
Loading

0 comments on commit ea180f6

Please sign in to comment.