processWordTable method

MsTable processWordTable(
  1. ProcessWordTableParams params
)

Function for processing table in word

Implementation

MsTable processWordTable(ProcessWordTableParams params) {
  String tblStyle = "";
  String rightFromText = "";
  String bottomFromText = "";
  String vertAnchor = "";
  String tblpY = "";
  String tblWidth = "";
  String tblWType = "";
  String tblLook = "";
  int seqNo = 0;
  var chkTblStyle = params.tableElement.findAllElements("w:tblStyle");
  if (chkTblStyle.isNotEmpty) {
    var tempTblStyle = chkTblStyle.first.getAttribute("w:val");
    if (tempTblStyle != null) {
      tblStyle = tempTblStyle;
    }
  }
  var chkTblPr = params.tableElement.findAllElements("w:tblpPr");
  if (chkTblPr.isNotEmpty) {
    var tempRightFromText = chkTblPr.first.getAttribute("w:rightFromText");
    if (tempRightFromText != null) {
      rightFromText = tempRightFromText;
    }
    var tempBottomFromText = chkTblPr.first.getAttribute("w:bottomFromText");
    if (tempBottomFromText != null) {
      bottomFromText = tempBottomFromText;
    }
    var tempVertAnchor = chkTblPr.first.getAttribute("w:vertAnchor");
    if (tempVertAnchor != null) {
      vertAnchor = tempVertAnchor;
    }
    var tempTblpY = chkTblPr.first.getAttribute("w:tblpY");
    if (tempTblpY != null) {
      tblpY = tempTblpY;
    }
  }
  var chkTblW = params.tableElement.findAllElements("w:tblW");
  if (chkTblW.isNotEmpty) {
    var tempTblW = chkTblW.first.getAttribute("w:w");
    if (tempTblW != null) {
      tblWidth = tempTblW;
    }
    var tempWType = chkTblW.first.getAttribute("w:type");
    if (tempWType != null) {
      tblWType = tempWType;
    }
  }
  MsTable table = MsTable(seqNo, tblStyle, rightFromText, bottomFromText, vertAnchor, tblpY, tblWidth, tblWType, tblLook);
  seqNo++;
  final rows = params.tableElement.findAllElements('w:tr');

  for (var row in rows) {
    bool isFirstRow = false;
    bool isLastRow = false;
    bool isFirstCol = false;
    bool isLastCol = false;
    final chkCnfStyle = row.findAllElements("w:cnfStyle");
    if (chkCnfStyle.isNotEmpty) {
      var cnfStyleVal = chkCnfStyle.first.getAttribute("w:val");
      if (cnfStyleVal != null && cnfStyleVal.startsWith("1")) {
        isFirstRow = true;
      }
      if (cnfStyleVal != null && cnfStyleVal.substring(1, 2) == "1") {
        isLastRow = true;
      }
      if (cnfStyleVal != null && cnfStyleVal.substring(2, 3) == "1") {
        isFirstCol = true;
      }
      if (cnfStyleVal != null && cnfStyleVal.substring(3, 4) == "1") {
        isLastCol = true;
      }
    }
    MsTableRow tableRow = MsTableRow(isFirstRow, isLastRow, isFirstCol, isLastCol);
    var chkGridSpan = row.findAllElements("w:gridSpan");
    if (chkGridSpan.isNotEmpty) {
      var tempGridSpan = chkGridSpan.first.getAttribute("w:val");
      if (tempGridSpan != null) {
        tableRow.gridSpan = int.parse(tempGridSpan);
      }
    }
    final cells = row.findAllElements('w:tc');
    if (table.colNums != cells.length && table.colNums < cells.length) {
      table.colNums = cells.length;
    }

    for (var cell in cells) {
      int cellWidth = 0;
      final cellWidthElement = cell.findAllElements("w:tcW");
      if (cellWidthElement.isNotEmpty) {
        var tempCellWidth = cellWidthElement.first.getAttribute("w:w");
        if (tempCellWidth != null) {
          cellWidth = int.parse(tempCellWidth);
        }
      }
      final cellData = cell.findAllElements('w:t');
      String colText = "";
      for (var cellText in cellData) {
        colText += cellText.innerText;
      }
      MsTableCell tableCell = MsTableCell(colText, cellWidth);
      tableRow.cells.add(tableCell);
    }
    table.rows.add(tableRow);
  }
  return table;
}