diff --git a/CHANGELOG.md b/CHANGELOG.md index a87fc93..8f0c84a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,19 @@ # Changelog +## [v1.4.0] - 26 June 2024 + +## What's Changed + +- Update excel dependency to version 4.0.3 by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/pull/15 +- Update contributing guidelines and code of conduct by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/pull/15 + ## [v1.3.0] - 25 June 2024 ## What's Changed -* Merge tag 'v1.2.0' into develop by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/pull/13 -* Update flutter by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/pull/14 -* Fixed CVE-2024-4068 by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/security/dependabot/2 + +- Merge tag 'v1.2.0' into develop by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/pull/13 +- Update flutter by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/pull/14 +- Fixed CVE-2024-4068 by @vitoramaral10 in https://github.com/vitoramaral10/excel_to_json/security/dependabot/2 ## [v1.2.0](https://github.com/vitoramaral10/excel_to_json/compare/v1.1.0...v1.2.0) - 6 September 2023 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7898192..47fc39b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1 +1,17 @@ -a +# Contributing + +## How to contribute + +Want to contribute to the project? We will be proud to highlight you as one of our collaborators. Here are some points where you can contribute and make excel to json even better. + +- Helping to translate the readme into other languages. +- Adding documentation to the readme (a lot of Get's functions haven't been documented yet). +- Write articles or make videos teaching how to use Get (they will be inserted in the Readme and in the future in our Wiki). +- Offering PRs for code/tests. +- Including new functions. + +Any contribution is welcome! + +## Code of Conduct + +Please read our [Code of Conduct](CODE_OF_CONDUCT.md) for more information. diff --git a/README.md b/README.md index cced343..b155d2d 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,35 @@ -# excel_to_json +# Excel to Json + +A package that converts `XLSX` files to `JSON` files. + +## Warning + +This project only recognizes `XLSX` files and converts them into json files. + +## Installation + +```bash + flutter pub add excel_to_json ``` -THIS PROJECT DON'T RECOGNIZE EXCEL FORMULES + +## Usage + +```dart +import 'package:excel_to_json/excel_to_json.dart'; + +void main() { + final excelToJson = ExcelToJson(); + + String? excel = await excelToJson.convert(); +} ``` -If you know how to solve this problem, please help this project. +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. -A package that allows you to transform your excel to the following format: -1. Excel To JSON +## Community -## Getting Started +### How to contribute -At current the package allows you to use the following function (note: currently xlsx file type gives not perfect results):
-# 1.convert()
- - automatically lets you pick an excel file and returns a string containing the entire converted json -
-For implementation have a look at the attached example file in the github repository.
+We welcome contributions to this project. Please read the [Contributing Guidelines](CONTRIBUTING.md) for more information. diff --git a/example/lib/home.dart b/example/lib/home.dart index c3c9217..dc18305 100644 --- a/example/lib/home.dart +++ b/example/lib/home.dart @@ -13,9 +13,15 @@ class Home extends StatelessWidget { child: ElevatedButton( child: const Text("PRESS TO UPLOAD EXCEL AND CONVERT TO JSON"), onPressed: () async { - String? excel = await ExcelToJson().convert(); - if (kDebugMode) { - print(excel); + try { + String? excel = await ExcelToJson().convert(); + if (kDebugMode) { + print(excel); + } + } on Exception catch (e) { + if (kDebugMode) { + print(e); + } } }, ), diff --git a/example/pubspec.lock b/example/pubspec.lock index e7bc85a..50e2c20 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: archive - sha256: cb6a278ef2dbb298455e1a713bda08524a175630ec643a242c399c932a0a1f7d + sha256: "7b875fd4a20b165a3084bd2d210439b22ebc653f21cea4842729c0c30c82596b" url: "https://pub.dev" source: hosted - version: "3.6.1" + version: "3.4.9" args: dependency: transitive description: @@ -153,14 +153,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.5" - excel_facility: + excel: dependency: transitive description: - name: excel_facility - sha256: "8766c481efa1896178417475fcf90027e0c5c3efd33c9049b728b0a3638d07d0" + name: excel + sha256: "86958dd0771d4ce372cc8332d17d18d803bbcef6ab6d12ddd071699e4c232573" url: "https://pub.dev" source: hosted - version: "1.0.1" + version: "4.0.3" excel_to_json: dependency: "direct main" description: @@ -263,6 +263,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.2" + js: + dependency: transitive + description: + name: js + sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf + url: "https://pub.dev" + source: hosted + version: "0.7.1" json_annotation: dependency: transitive description: @@ -415,6 +423,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + pointycastle: + dependency: transitive + description: + name: pointycastle + sha256: "4be0097fcf3fd3e8449e53730c631200ebc7b88016acecab2b0da2f0149222fe" + url: "https://pub.dev" + source: hosted + version: "3.9.1" process: dependency: transitive description: diff --git a/lib/excel_to_json.dart b/lib/excel_to_json.dart index a0e934e..12551a7 100644 --- a/lib/excel_to_json.dart +++ b/lib/excel_to_json.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'dart:developer'; -import 'package:excel_facility/excel_facility.dart'; +import 'package:excel/excel.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/foundation.dart'; @@ -9,41 +9,47 @@ import 'package:flutter/foundation.dart'; class ExcelToJson { /// Use this method to convert the file to a json. Future convert() async { - final Excel? excel = await _getFile(); - - if (excel != null) { - final List tables = _getTables(excel); - - int index = 0; - final Map json = {}; - - for (final String table in tables) { - List keys = []; - json.addAll({table: []}); - - for (final List row in excel.tables[table]?.rows ?? []) { - try { - if (index == 0) { - keys = row; - index++; - } else { - final Map temp = _getRows(keys, row); - - json[table].add(temp); + try { + final Excel? excel = await _getFile(); + + if (excel != null) { + final List tables = _getTables(excel); + + int index = 0; + final Map json = {}; + + for (final String table in tables) { + List keys = []; + json.addAll({table: []}); + + for (final List row in excel.tables[table]?.rows ?? []) { + try { + if (index == 0) { + keys = row; + index++; + } else { + final Map temp = _getRows(keys, row); + + if (temp.isNotEmpty) { + json[table].add(temp); + } + } + } on Exception catch (ex) { + log(ex.toString()); + + rethrow; } - } on Exception catch (ex) { - log(ex.toString()); - - rethrow; } + index = 0; } - index = 0; + + return jsonEncode(json); } - return jsonEncode(json); + return null; + } on Exception { + rethrow; } - - return null; } Map _getRows(final List keys, final List row) { @@ -55,21 +61,31 @@ class ExcelToJson { if (key != null && key.value != null) { tk = key.value.toString(); - if ([ - CellType.String, - CellType.int, - CellType.double, - CellType.bool, - ].contains(row[index]?.cellType)) { - if (row[index]?.value == 'true') { - temp[tk] = true; - } else if (row[index]?.value == 'false') { - temp[tk] = false; - } else { - temp[tk] = row[index]?.value; + if (row[index] != null && row[index]!.value != null) { + final value = row[index]!.value; + + switch (value) { + case null: + temp.addAll({tk: null}); + case TextCellValue(): + temp.addAll({tk: value.value}); + case FormulaCellValue(): + temp.addAll({tk: value.formula}); + case IntCellValue(): + temp.addAll({tk: value.value}); + case BoolCellValue(): + temp.addAll({tk: value.value}); + case DoubleCellValue(): + temp.addAll({tk: value.value}); + case DateCellValue(): + temp.addAll({tk: value.toString()}); + case TimeCellValue(): + temp.addAll({tk: value.toString()}); + case DateTimeCellValue(): + temp.addAll({tk: value.toString()}); + default: + temp.addAll({tk: value.toString()}); } - } else if (row[index]?.cellType == CellType.Formula) { - temp[tk] = row[index]?.value.toString(); } index++; @@ -90,17 +106,22 @@ class ExcelToJson { } Future _getFile() async { - final FilePickerResult? file = await FilePicker.platform.pickFiles( - withData: true, - type: FileType.custom, - allowedExtensions: ['xlsx', 'csv', 'xls'], - ); - if (file != null && file.files.isNotEmpty) { - final Uint8List bytes = file.files.first.bytes!; - - return Excel.decodeBytes(bytes); - } else { - return null; + try { + final FilePickerResult? file = await FilePicker.platform.pickFiles( + withData: true, + type: FileType.custom, + allowedExtensions: ['xlsx'], + ); + + if (file != null && file.files.isNotEmpty) { + final Uint8List bytes = file.files.first.bytes!; + + return Excel.decodeBytes(bytes); + } else { + return null; + } + } on Exception { + rethrow; } } } diff --git a/pubspec.lock b/pubspec.lock index 7ca8eeb..307a4ec 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,10 +5,10 @@ packages: dependency: transitive description: name: archive - sha256: cb6a278ef2dbb298455e1a713bda08524a175630ec643a242c399c932a0a1f7d + sha256: "7b875fd4a20b165a3084bd2d210439b22ebc653f21cea4842729c0c30c82596b" url: "https://pub.dev" source: hosted - version: "3.6.1" + version: "3.4.9" async: dependency: transitive description: @@ -49,6 +49,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.18.0" + convert: + dependency: transitive + description: + name: convert + sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" + url: "https://pub.dev" + source: hosted + version: "3.1.1" cross_file: dependency: transitive description: @@ -73,14 +81,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.5" - excel_facility: + excel: dependency: "direct main" description: - name: excel_facility - sha256: "8766c481efa1896178417475fcf90027e0c5c3efd33c9049b728b0a3638d07d0" + name: excel + sha256: "86958dd0771d4ce372cc8332d17d18d803bbcef6ab6d12ddd071699e4c232573" url: "https://pub.dev" source: hosted - version: "1.0.1" + version: "4.0.3" fake_async: dependency: transitive description: @@ -136,6 +144,14 @@ packages: description: flutter source: sdk version: "0.0.0" + js: + dependency: transitive + description: + name: js + sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf + url: "https://pub.dev" + source: hosted + version: "0.7.1" leak_tracker: dependency: transitive description: @@ -272,6 +288,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + pointycastle: + dependency: transitive + description: + name: pointycastle + sha256: "4be0097fcf3fd3e8449e53730c631200ebc7b88016acecab2b0da2f0149222fe" + url: "https://pub.dev" + source: hosted + version: "3.9.1" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index a8c7222..94d0809 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: excel_to_json description: A package that allows you to transform excel files to JSON format for friendly use in your project. -version: 1.3.0 +version: 1.4.0 homepage: https://github.com/vitoramaral10/excel_to_json environment: @@ -9,7 +9,7 @@ environment: flutter: ">=1.17.0" dependencies: - excel_facility: ^1.0.1 + excel: ^4.0.3 file_picker: ^8.0.5 flutter: sdk: flutter