Skip to content

Commit

Permalink
Merge pull request #15 from vitoramaral10/feature/change_excel_plugin
Browse files Browse the repository at this point in the history
Feature/change excel plugin
  • Loading branch information
vitoramaral10 authored Jun 26, 2024
2 parents 5ef43ce + 76a5908 commit 44199ea
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 87 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 17 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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):<br />
# 1.convert()<br />
- automatically lets you pick an excel file and returns a string containing the entire converted json
<br />
For implementation have a look at the attached example file in the github repository.<br />
We welcome contributions to this project. Please read the [Contributing Guidelines](CONTRIBUTING.md) for more information.
12 changes: 9 additions & 3 deletions example/lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},
),
Expand Down
28 changes: 22 additions & 6 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
131 changes: 76 additions & 55 deletions lib/excel_to_json.dart
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
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';

/// This is the main project class.
class ExcelToJson {
/// Use this method to convert the file to a json.
Future<String?> convert() async {
final Excel? excel = await _getFile();

if (excel != null) {
final List<String> tables = _getTables(excel);

int index = 0;
final Map<String, dynamic> json = {};

for (final String table in tables) {
List<Data?> keys = [];
json.addAll({table: []});

for (final List<Data?> row in excel.tables[table]?.rows ?? []) {
try {
if (index == 0) {
keys = row;
index++;
} else {
final Map<String, dynamic> temp = _getRows(keys, row);

json[table].add(temp);
try {
final Excel? excel = await _getFile();

if (excel != null) {
final List<String> tables = _getTables(excel);

int index = 0;
final Map<String, dynamic> json = {};

for (final String table in tables) {
List<Data?> keys = [];
json.addAll({table: []});

for (final List<Data?> row in excel.tables[table]?.rows ?? []) {
try {
if (index == 0) {
keys = row;
index++;
} else {
final Map<String, dynamic> 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<String, dynamic> _getRows(final List<Data?> keys, final List<Data?> row) {
Expand All @@ -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++;
Expand All @@ -90,17 +106,22 @@ class ExcelToJson {
}

Future<Excel?> _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;
}
}
}
Loading

0 comments on commit 44199ea

Please sign in to comment.