textSpan property

TextSpan get textSpan

Implementation

TextSpan get textSpan {
  bool getBool(XmlElement element) {
    return bool.tryParse(element.getAttribute('val') ?? '') ?? true;
  }

  int getDouble(XmlElement element) {
    // Should be double
    return double.parse(element.getAttribute('val')!).toInt();
  }

  String? text;
  List<TextSpan>? children;

  /// SharedStringItem
  /// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sharedstringitem?view=openxml-3.0.1
  assert(node.localName == 'si'); //18.4.8 si (String Item)

  for (final child in node.childElements) {
    switch (child.localName) {
      /// Text
      /// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.text?view=openxml-3.0.1
      case 't': //18.4.12 t (Text)
        text = (text ?? '') + child.innerText;
        break;

      /// Rich Text Run
      /// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.run?view=openxml-3.0.1
      case 'r': //18.4.4 r (Rich Text Run)
        var style = CellStyle();
        for (final runChild in child.childElements) {
          switch (runChild.localName) {
            /// RunProperties
            /// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.runproperties?view=openxml-3.0.1
            case 'rPr':
              for (final runProperty in runChild.childElements) {
                switch (runProperty.localName) {
                  case 'b': //18.8.2 b (Bold)
                    style = style.copyWith(boldVal: getBool(runProperty));
                    break;
                  case 'i': //18.8.26 i (Italic)
                    style = style.copyWith(italicVal: getBool(runProperty));
                    break;
                  case 'u': //18.4.13 u (Underline)
                    style = style.copyWith(
                        underlineVal:
                            runProperty.getAttribute('val') == 'double'
                                ? Underline.Double
                                : Underline.Single);
                    break;
                  case 'sz': //18.4.11 sz (Font Size)
                    style =
                        style.copyWith(fontSizeVal: getDouble(runProperty));
                    break;
                  case 'rFont': //18.4.5 rFont (Font)
                    style = style.copyWith(
                        fontFamilyVal: runProperty.getAttribute('val'));
                    break;
                  case 'color': //18.3.1.15 color (Data Bar Color)
                    style = style.copyWith(
                        fontColorHexVal:
                            runProperty.getAttribute('rgb')?.excelColor);
                    break;
                }
              }
              break;

            /// Text
            case 't': //18.4.12 t (Text)
              if (children == null) children = [];
              children.add(TextSpan(text: runChild.innerText, style: style));
              break;
          }
        }
        break;

      /// Phonetic Run
      /// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.phoneticrun?view=openxml-3.0.1
      case 'rPh': //18.4.6 rPh (Phonetic Run)
        break;
    }
  }

  return TextSpan(text: text, children: children);
}