readAllSheets method

Future<void> readAllSheets(
  1. SpreadSheet spreadSheet,
  2. List<Relationship> relationShips,
  3. Archive archive
)

Function for reading all details of the sheets

Implementation

Future<void> readAllSheets(SpreadSheet spreadSheet, List<Relationship> relationShips, Archive archive) async {
  for (int i = 0; i < spreadSheet.sheets.length; i++) {
    var sheetRelation = relationShips.firstWhereOrNull((rel) {
      return rel.id == spreadSheet.sheets[i].rId;
    });
    if (sheetRelation != null) {
      var sheetFile = archive.singleWhere((archiveFile) {
        return archiveFile.name.endsWith(sheetRelation.target);
      });
      if (sheetFile.isFile) {
        final fileContent = utf8.decode(sheetFile.content);
        final workbookDoc = xml.XmlDocument.parse(fileContent);
        List<Map<String, String>> mergeCells = [];
        List<String> mergedCells = [];
        var chkMergeCells = workbookDoc.findAllElements("mergeCells");
        if (chkMergeCells.isNotEmpty) {
          var tempMergeCells = chkMergeCells.first.findAllElements("mergeCell");
          if (tempMergeCells.isNotEmpty) {
            for (var mergeCell in tempMergeCells) {
              var ref = mergeCell.getAttribute("ref");
              if (ref != null) {
                var fromTo = ref.split(":");
                if (fromTo.length > 1) {
                  mergeCells.add({"from": fromTo[0], "to": fromTo[1]});
                  mergedCells.addAll(getAllMergedCells(fromTo[0], fromTo[1]));
                }
              }
            }
          }
        }

        MsSsTable table = MsSsTable();
        var cols = workbookDoc.findAllElements("cols");
        if (cols.isNotEmpty) {
          var col = cols.first.findAllElements("col");
          if (col.isNotEmpty) {
            List<MsSsCol> colList = [];
            for (var tempCol in col) {
              int min = 0;
              int max = 0;
              double width = 0;
              int customWidth = 0;
              var tempMin = tempCol.getAttribute("min");
              if (tempMin != null) {
                min = int.parse(tempMin);
              }
              var tempMax = tempCol.getAttribute("max");
              if (tempMax != null) {
                max = int.parse(tempMax);
              }
              var tempWidth = tempCol.getAttribute("width");
              if (tempWidth != null) {
                width = double.parse(tempWidth);
              }
              var tempCustWidth = tempCol.getAttribute("customWidth");
              if (tempCustWidth != null) {
                customWidth = int.parse(tempCustWidth);
              }
              colList.add(MsSsCol(min, max, width, customWidth));
            }
            table.cols.addAll(colList);
          }
        }
        var sheetData = workbookDoc.findAllElements("sheetData");
        if (sheetData.isNotEmpty) {
          var rows = sheetData.first.findAllElements("row");
          List<MsSsRow> rowList = [];
          if (rows.isNotEmpty) {
            for (var row in rows) {
              var msSsRow = await compute(getRows, GetRowsParams(row, mergeCells, mergedCells));
              rowList.add(msSsRow);
            }
          }

          table.rows.addAll(rowList);
        }
        spreadSheet.sheets[i].tables.add(table);
      }
    }
  }
}