processSpreadSheetStyles method

void processSpreadSheetStyles(
  1. ArchiveFile stylesFile,
  2. List<SSStyle> spreadSheetStyles
)

Implementation

void processSpreadSheetStyles(ArchiveFile stylesFile, List<SSStyle> spreadSheetStyles) {
  final fileContent = utf8.decode(stylesFile.content);
  final ssStylesDoc = xml.XmlDocument.parse(fileContent);
  var styleRoot = ssStylesDoc.findAllElements("styleSheet");
  List<NumFormat> numFormats = [];
  List<SSFont> fonts = [];
  List<SSFill> fills = [];
  List<SSBorder> borders = [];
  if (styleRoot.isNotEmpty) {
    var chkNumFormats = styleRoot.first.findAllElements("numFmts");
    if (chkNumFormats.isNotEmpty) {
      var tempNumFormats = chkNumFormats.first.findAllElements("numFmt");
      if (tempNumFormats.isNotEmpty) {
        for (var numFmt in tempNumFormats) {
          var numId = numFmt.getAttribute("numFmtId");
          var fmtCode = numFmt.getAttribute("formatCode");
          if (numId != null && fmtCode != null) {
            numFormats.add(NumFormat(numId, fmtCode));
          }
        }
      }
    }
    var chkFonts = styleRoot.first.findAllElements("fonts");
    if (chkFonts.isNotEmpty) {
      var tempFonts = chkFonts.first.findAllElements("font");
      if (tempFonts.isNotEmpty) {
        for (var font in tempFonts) {
          int sz = 0;
          String clrTheme = "";
          String clrTint = "";
          String name = "";
          var chkSz = font.findAllElements("sz");
          if (chkSz.isNotEmpty) {
            var tempSz = chkSz.first.getAttribute("val");
            if (tempSz != null) {
              sz = int.parse(tempSz);
            }
          }
          var chkClrTheme = font.findAllElements("color");
          if (chkClrTheme.isNotEmpty) {
            var tempColor = chkClrTheme.first.getAttribute("theme");
            if (tempColor != null) {
              clrTheme = tempColor;
            }
            var tempClrTint = chkClrTheme.first.getAttribute("tint");
            if (tempClrTint != null) {
              clrTint = tempClrTint;
            }
          }
          var chkName = font.findAllElements("name");
          if (chkName.isNotEmpty) {
            var tempName = chkName.first.getAttribute("val");
            if (tempName != null) {
              name = tempName;
            }
          }
          fonts.add(SSFont(fonts.length.toString(), name, sz, clrTheme, clrTint));
        }
      }
    }
    var chkFills = styleRoot.first.findAllElements("fills");
    if (chkFills.isNotEmpty) {
      var tempFills = chkFills.first.findAllElements("fill");
      if (tempFills.isNotEmpty) {
        for (var fill in tempFills) {
          String type = "";
          String clrTheme = "";
          String clrTint = "";
          String clrIndex = "";
          var chkPattern = fill.findAllElements("patternFill");
          if (chkPattern.isNotEmpty) {
            var tempType = chkPattern.first.getAttribute("patternType");
            if (tempType != null) {
              type = tempType;
            }
            var chkFgClr = chkPattern.first.findAllElements("fgColor");
            if (chkFgClr.isNotEmpty) {
              var tempTheme = chkFgClr.first.getAttribute("theme");
              if (tempTheme != null) {
                clrTheme = tempTheme;
              }
              var tempTint = chkFgClr.first.getAttribute("tint");
              if (tempTint != null) {
                clrTint = tempTint;
              }
            }
            var chkBgClr = chkPattern.first.findAllElements("bgColor");
            if (chkBgClr.isNotEmpty) {
              var tempIndex = chkBgClr.first.getAttribute("indexed");
              if (tempIndex != null) {
                clrIndex = tempIndex;
              }
            }
          }
          fills.add(SSFill(fills.length.toString(), type, clrTheme, clrTint, clrIndex));
        }
      }
    }
    var chkBorder = styleRoot.first.findAllElements("borders");
    if (chkBorder.isNotEmpty) {
      var allBorder = chkBorder.first.findAllElements("border");
      if (allBorder.isNotEmpty) {
        for (var border in allBorder) {
          String leftStyle = "";
          String leftClrTheme = "";
          String leftClrTint = "";
          String rightStyle = "";
          String rightClrTheme = "";
          String rightClrTint = "";
          String topStyle = "";
          String topClrTheme = "";
          String topClrTint = "";
          String bottomStyle = "";
          String bottomClrTheme = "";
          String bottomClrTint = "";
          var chkLeft = border.findAllElements("left");
          if (chkLeft.isNotEmpty) {
            var tempStyle = chkLeft.first.getAttribute("style");
            if (tempStyle != null) {
              leftStyle = tempStyle;
            }
            var chkClr = chkLeft.first.findAllElements("color");
            if (chkClr.isNotEmpty) {
              var tempTheme = chkClr.first.getAttribute("theme");
              if (tempTheme != null) {
                leftClrTheme = tempTheme;
              }
              var tempTint = chkClr.first.getAttribute("tint");
              if (tempTint != null) {
                leftClrTint = tempTint;
              }
            }
          }
          var chkRight = border.findAllElements("right");
          if (chkRight.isNotEmpty) {
            var tempStyle = chkRight.first.getAttribute("style");
            if (tempStyle != null) {
              rightStyle = tempStyle;
            }
            var chkClr = chkRight.first.findAllElements("color");
            if (chkClr.isNotEmpty) {
              var tempTheme = chkClr.first.getAttribute("theme");
              if (tempTheme != null) {
                rightClrTheme = tempTheme;
              }
              var tempTint = chkClr.first.getAttribute("tint");
              if (tempTint != null) {
                rightClrTint = tempTint;
              }
            }
          }
          var chkTop = border.findAllElements("top");
          if (chkTop.isNotEmpty) {
            var tempStyle = chkTop.first.getAttribute("style");
            if (tempStyle != null) {
              topStyle = tempStyle;
            }
            var chkClr = chkTop.first.findAllElements("color");
            if (chkClr.isNotEmpty) {
              var tempTheme = chkClr.first.getAttribute("theme");
              if (tempTheme != null) {
                topClrTheme = tempTheme;
              }
              var tempTint = chkClr.first.getAttribute("tint");
              if (tempTint != null) {
                topClrTint = tempTint;
              }
            }
          }
          var chkBottom = border.findAllElements("left");
          if (chkBottom.isNotEmpty) {
            var tempStyle = chkBottom.first.getAttribute("style");
            if (tempStyle != null) {
              bottomStyle = tempStyle;
            }
            var chkClr = chkBottom.first.findAllElements("color");
            if (chkClr.isNotEmpty) {
              var tempTheme = chkClr.first.getAttribute("theme");
              if (tempTheme != null) {
                bottomClrTheme = tempTheme;
              }
              var tempTint = chkClr.first.getAttribute("tint");
              if (tempTint != null) {
                bottomClrTint = tempTint;
              }
            }
          }

          borders.add(SSBorder(borders.length.toString(), leftStyle, leftClrTheme, leftClrTint, rightStyle, rightClrTheme, rightClrTint, topStyle,
              topClrTheme, topClrTint, bottomStyle, bottomClrTheme, bottomClrTint));
        }
      }
    }

    var chkCellXfs = styleRoot.first.findAllElements("cellXfs");
    if (chkCellXfs.isNotEmpty) {
      var xfs = chkCellXfs.first.findAllElements("xf");
      if (xfs.isNotEmpty) {
        for (var xf in xfs) {
          NumFormat numFormat = NumFormat("", "");
          SSFont ssFont = SSFont("", "", 0, "", "");
          SSFill ssFill = SSFill("", "", "", "", "");
          SSBorder border = SSBorder("", "", "", "", "", "", "", "", "", "", "", "", "");
          String alignmentVer = "", alignmentHorizontal = "", alignmentWrapText = "";
          var tempNumId = xf.getAttribute("numFmtId");
          if (tempNumId != null) {
            var tempNumFmt = numFormats.firstWhereOrNull((numFmt) {
              return numFmt.id == tempNumId;
            });
            if (tempNumFmt != null) {
              numFormat = tempNumFmt;
            }
          }
          var tempFontId = xf.getAttribute("fontId");
          if (tempFontId != null) {
            var tempFont = fonts.firstWhereOrNull((fnt) {
              return fnt.id == tempFontId;
            });
            if (tempFont != null) {
              ssFont = tempFont;
            }
          }
          var tempFillId = xf.getAttribute("fillId");
          if (tempFillId != null) {
            var tempFill = fills.firstWhereOrNull((fill) {
              return fill.id == tempFillId;
            });
            if (tempFill != null) {
              ssFill = tempFill;
            }
          }
          var tempBorderId = xf.getAttribute("borderId");
          if (tempBorderId != null) {
            var tempBorder = borders.firstWhereOrNull((brd) {
              return brd.id == tempBorderId;
            });
            if (tempBorder != null) {
              border = tempBorder;
            }
          }
          var chkAlignment = xf.findAllElements("alignment");
          if (chkAlignment.isNotEmpty) {
            var tempHorizontal = chkAlignment.first.getAttribute("horizontal");
            if (tempHorizontal != null) {
              alignmentHorizontal = tempHorizontal;
            }
            var tempVertical = chkAlignment.first.getAttribute("vertical");
            if (tempVertical != null) {
              alignmentVer = tempVertical;
            }
            var tempWrapText = chkAlignment.first.getAttribute("wrapText");
            if (tempWrapText != null) {
              alignmentWrapText = tempWrapText;
            }
          }
          spreadSheetStyles.add(SSStyle(
              spreadSheetStyles.length.toString(), numFormat, ssFont, ssFill, border, alignmentVer, alignmentHorizontal, alignmentWrapText));
        }
      }
    }
  }
}