justkawal / excel

Excel Library for Flutter and Dart - https://pub.dev/packages/excel
MIT License
403 stars 212 forks source link

Help: Determining the type of a cell #327

Closed danielithomas closed 4 months ago

danielithomas commented 4 months ago

Hi, prior to the v4 breaking change I could do something like this to check and get a value from a cell:

String safeGetStringFromDataItem(Data? item) {
    String returnVal = "";
    if (item != null) {
      if (item.value != null) {
        if (item.cellType == CellType.String) {
          returnVal = Utility.safeGetStringValue(item.value.toString());
        }
      }
    }
    return returnVal;
  }

Since v4 "cellType" has gone away. I tried a modified version of the case statement (in the readme):

// printing cell-type
print('CellType: ' + switch(cell.value) {
  null => 'empty cell',
  TextCellValue() => 'text',
  FormulaCellValue() => 'formula',
  IntCellValue() => 'int',
  BoolCellValue() => 'bool',
  DoubleCellValue() => 'double',
  DateCellValue() => 'date',
  TimeCellValue => 'time',
  DateTimeCellValue => 'date with time',
});

But things are just not working for me. Fully accepting that the problem is going to be PEBKAC, but struggling to figure this one out. Any help in understanding and managing cellTypes more effectively would be greatly appreciated.

danielithomas commented 4 months ago

... Aaand I'll answer my own question:

Check cell.value.runtimeType