Skip to content

Commit

Permalink
fix: cell style for default cell value #300 (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: Alessandro La Tona <[email protected]>
  • Loading branch information
allato and Alessandro La Tona authored Jan 11, 2024
1 parent cbf6701 commit 8a73ac2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
12 changes: 11 additions & 1 deletion example/excel_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,17 @@ void main(List<String> args) {
});
print('appending executed in ${stopwatch.elapsed}');

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

bool isSet = excel.setDefaultSheet(sheet.sheetName);
// isSet is bool which tells that whether the setting of default sheet is successful or not.
if (isSet) {
Expand Down
12 changes: 5 additions & 7 deletions lib/src/number_format/num_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class NumFormatMaintainer {
sealed class NumFormat {
final String formatCode;

static const defaultNumeric = standard_0;
static const defaultNumeric = standard_1;
static const defaultFloat = standard_2;
static const defaultBool = standard_0;
static const defaultDate = standard_14;
static const defaultTime = standard_20;
Expand Down Expand Up @@ -163,12 +164,9 @@ sealed class NumFormat {
bool accepts(CellValue? value);

static NumFormat defaultFor(CellValue? value) => switch (value) {
null ||
FormulaCellValue() ||
IntCellValue() ||
DoubleCellValue() ||
TextCellValue() =>
NumFormat.standard_0,
null || FormulaCellValue() || TextCellValue() => NumFormat.standard_0,
IntCellValue() => NumFormat.defaultNumeric,
DoubleCellValue() => NumFormat.defaultFloat,
DateCellValue() => NumFormat.defaultDate,
BoolCellValue() => NumFormat.defaultBool,
TimeCellValue() => NumFormat.defaultTime,
Expand Down
1 change: 1 addition & 0 deletions lib/src/sheet/sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ class Sheet {
}

cell._value = value;
cell._cellStyle = CellStyle(numberFormat: NumFormat.defaultFor(value));

if ((_maxColumns - 1) < columnIndex) {
_maxColumns = columnIndex + 1;
Expand Down
6 changes: 3 additions & 3 deletions test/excel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ void main() {
final b4 = sheet.cell(CellIndex.indexByString('B4'));
expect(b4.value, equals(DoubleCellValue(13.37)));
expect(
b4.cellStyle?.numberFormat ?? NumFormat.standard_0,
equals(NumFormat.standard_0),
b4.cellStyle?.numberFormat ?? NumFormat.defaultFloat,
equals(NumFormat.defaultFloat),
);

final b5 = sheet.cell(CellIndex.indexByString('B5'));
Expand All @@ -213,7 +213,7 @@ void main() {
expect(b6.value, equals(null));
expect(
b6.cellStyle?.numberFormat,
equals(NumFormat.defaultDate),
equals(NumFormat.standard_0),
);

final b7 = sheet.cell(CellIndex.indexByString('B7'));
Expand Down

0 comments on commit 8a73ac2

Please sign in to comment.