Skip to content

Commit

Permalink
Merge branch 'main' into delete-row-small-performance-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
FauconSpartiate authored Apr 3, 2024
2 parents 9f6c559 + 89d31cf commit fc3727b
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 14 deletions.
38 changes: 38 additions & 0 deletions example/excel_example_style.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:io';
import 'package:path/path.dart';
import 'package:excel/excel.dart';

void main(List<String> args) {
//var file = "/Users/kawal/Desktop/excel/test/test_resources/example.xlsx";
//var bytes = File(file).readAsBytesSync();
var excel = Excel.createExcel();
// or
//var excel = Excel.decodeBytes(bytes);

var sheet = excel['Sheet1'];

sheet.appendRow([
IntCellValue(8),
DoubleCellValue(999.62221),
DateCellValue(
year: DateTime.now().year,
month: DateTime.now().month,
day: DateTime.now().day,
),
DateTimeCellValue.fromDateTime(DateTime.now()),
]);

// Saving the file

// String outputFile = "/Users/kawal/Desktop/git_projects/r.xlsx";
String outputFile = './example/example.xlsx';

//stopwatch.reset();
List<int>? fileBytes = excel.save();
//print('saving executed in ${stopwatch.elapsed}');
if (fileBytes != null) {
File(join(outputFile))
..createSync(recursive: true)
..writeAsBytesSync(fileBytes);
}
}
25 changes: 15 additions & 10 deletions lib/src/sheet/sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,9 @@ class Sheet {
}
if (rowIndex <= rowKey) {
_data[rowKey + 1] = _sheetData[rowKey]!;
_data[rowKey + 1]!.forEach((key, value) {
value._rowIndex++;
});
}
});
}
Expand Down Expand Up @@ -1024,8 +1027,12 @@ class Sheet {
///
/// [overwriteMergedCells] when set to [false] puts the cell value in next unique cell available and putting the value in merged cells only once.
///
void insertRowIterables(List<CellValue?> row, int rowIndex,
{int startingColumn = 0, bool overwriteMergedCells = true}) {
void insertRowIterables(
List<CellValue?> row,
int rowIndex, {
int startingColumn = 0,
bool overwriteMergedCells = true,
}) {
if (row.isEmpty || rowIndex < 0) {
return;
}
Expand All @@ -1044,9 +1051,7 @@ class Sheet {
// Normally iterating and putting the data present in the [row] as we are on the last index.

while (currentRowPosition <= maxIterationIndex) {
_putData(rowIndex, columnIndex, row[currentRowPosition]);
currentRowPosition++;
columnIndex++;
_putData(rowIndex, columnIndex++, row[currentRowPosition++]);
}
} else {
// expensive function as per time complexity
Expand All @@ -1055,15 +1060,12 @@ class Sheet {

if (_spanObjectsList.isEmpty) {
while (currentRowPosition <= maxIterationIndex) {
_putData(rowIndex, columnIndex, row[currentRowPosition]);
currentRowPosition++;
columnIndex++;
_putData(rowIndex, columnIndex++, row[currentRowPosition++]);
}
} else {
while (currentRowPosition <= maxIterationIndex) {
if (_isInsideSpanObject(_spanObjectsList, columnIndex, rowIndex)) {
_putData(rowIndex, columnIndex, row[currentRowPosition]);
currentRowPosition++;
_putData(rowIndex, columnIndex, row[currentRowPosition++]);
}
columnIndex++;
}
Expand All @@ -1086,6 +1088,9 @@ class Sheet {

cell._value = value;
cell._cellStyle = CellStyle(numberFormat: NumFormat.defaultFor(value));
if (cell._cellStyle != NumFormat.standard_0) {
_excel._styleChanges = true;
}

if ((_maxColumns - 1) < columnIndex) {
_maxColumns = columnIndex + 1;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: excel
description: A flutter and dart library for reading, creating, editing and updating excel sheets with compatible both on client and server side.
version: 4.0.3
version: 4.0.4
homepage: https://github.com/justkawal/excel
topics:
- excel
Expand Down
95 changes: 92 additions & 3 deletions test/excel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ void main() {
);
expect(
excel.tables['Sheet1']?.rows[6][1]?.value,
equals(DateTimeCellValue(
year: 2023, month: 4, day: 20, hour: 15, minute: 44, second: 13)),
equals(
DateTimeCellValue(
year: 2023,
month: 4,
day: 20,
hour: 15,
minute: 44,
second: 13,
),
),
);
expect(
excel.tables['Sheet1']?.rows[7][1]?.value,
Expand Down Expand Up @@ -131,7 +139,13 @@ void main() {
expect(
excel.tables['Sheet1']?.rows[6][1]?.value,
equals(DateTimeCellValue(
year: 2023, month: 4, day: 20, hour: 15, minute: 44, second: 13)),
year: 2023,
month: 4,
day: 20,
hour: 15,
minute: 44,
second: 13,
)),
);
expect(
excel.tables['Sheet1']?.rows[7][1]?.value,
Expand Down Expand Up @@ -352,6 +366,81 @@ void main() {
equals('Moscow'));
});

test('Saving XLSX File with appendRow', () {
var excel = Excel.createExcel();
var sheet = excel['Sheet1'];

sheet.appendRow([
IntCellValue(8),
DoubleCellValue(999.62221),
DateCellValue(year: 2023, month: 4, day: 20),
DateTimeCellValue(
year: 2023,
month: 4,
day: 20,
hour: 15,
minute: 44,
second: 13,
),
TextCellValue('value'),
]);

//stopwatch.reset();
List<int>? fileBytes = excel.save();
//print('saving executed in ${stopwatch.elapsed}');
if (fileBytes != null) {
File(Directory.current.path + '/tmp/exampleOut.xlsx')
..createSync(recursive: true)
..writeAsBytesSync(fileBytes);
}

var newFile = './tmp/exampleOut.xlsx';
var newFileBytes = File(newFile).readAsBytesSync();
var newExcel = Excel.decodeBytes(newFileBytes);

// delete tmp folder
new Directory('./tmp').delete(recursive: true);
expect(newExcel.sheets.entries.length, equals(1));
expect(newExcel.tables['Sheet1']!.maxColumns, equals(5));
expect(newExcel.tables['Sheet1']!.rows[0][0]!.value, equals(IntCellValue(8)));
expect(
newExcel.tables['Sheet1']!.rows[0][0]!.cellStyle?.numberFormat
.toString(),
equals(NumFormat.defaultNumeric.toString()));
expect(newExcel.tables['Sheet1']!.rows[0][1]!.value,
DoubleCellValue(999.62221));
expect(
newExcel.tables['Sheet1']!.rows[0][1]!.cellStyle?.numberFormat
.toString(),
equals(NumFormat.defaultFloat.toString()));
expect(newExcel.tables['Sheet1']!.rows[0][2]!.value,
DateCellValue(year: 2023, month: 4, day: 20));
expect(
newExcel.tables['Sheet1']!.rows[0][2]!.cellStyle?.numberFormat
.toString(),
equals(NumFormat.defaultDate.toString()));
expect(
newExcel.tables['Sheet1']!.rows[0][3]!.value,
DateTimeCellValue(
year: 2023,
month: 4,
day: 20,
hour: 15,
minute: 44,
second: 13,
));
expect(
newExcel.tables['Sheet1']!.rows[0][3]!.cellStyle?.numberFormat
.toString(),
equals(NumFormat.defaultDateTime.toString()));
expect(
newExcel.tables['Sheet1']!.rows[0][4]!.value, TextCellValue('value'));
expect(
newExcel.tables['Sheet1']!.rows[0][4]!.cellStyle?.numberFormat
.toString(),
equals(NumFormat.standard_0.toString()));
});

test('Saving XLSX File with superscript', () {
var file = './test/test_resources/superscriptExample.xlsx';
var bytes = File(file).readAsBytesSync();
Expand Down

0 comments on commit fc3727b

Please sign in to comment.