Page 1 of 1

Colouring table cells depends of its content

PostPosted: Sat Jun 09, 2012 12:15 am
by Adolar
Hello everybody!

I want to color the background of table cells depends of text inside the cell. The content of cells come from data binding via customXML. Data binding works very well.
The structure of document.xml after binding process looks like this:

Code: Select all
   <w:tc>
      <w:tcPr>
         ...
         <w:shd w:val="clear" w:color="auto" w:fill="FF00FF"/>
      </w:tcPr>
      <w:p w:rsidRPr="00FA37FC" w:rsidR="000F1019" w:rsidP="00EA4B55" w:rsidRDefault="00EA4616">
         <w:pPr>
            ...
         </w:pPr>
         <w:sdt>
            <w:sdtPr>
               ...
               <w:tag w:val="od:xpath=x16_1_0&amp;sequence_rating=sequence_rating"/>
               <w:dataBinding w:xpath="/testsuite[1]/tests/test[2][1]/sequences/sequence[1][1]/@rating" w:storeItemID="{8B049945-9DFE-4726-9DE9-CF5691E53858}"/>
               ...
            </w:sdtPr>
            <w:sdtContent>
               <w:r>
                  <w:t>PASSED</w:t>
               </w:r>
            </w:sdtContent>
         </w:sdt>
      </w:p>
   </w:tc>


The background of cell is FF00FF, the tag to select the cells is sequence_rating and the text content is PASSED. For change the background I fetch the cells via XPath, iterate over the cell list and try to navigate to the cell text:

Code: Select all
   String xpath = "//w:tag[contains(@w:val,'sequence_rating')]/ancestor::w:tc";
   try {
      List<Object> list = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath( xpath, false );
      for ( Object e : list ) {
         if ( e instanceof JAXBElement ) {
            JAXBElement j = ( JAXBElement ) e;
            if ( j.getValue() instanceof Tc ) {
            Tc tableCell = ( Tc ) j.getValue();
            CTShd cellShd = tableCell.getTcPr().getShd();
            /************************************************************************
            * From here I don't know how to navigate to the cell text.
            * tableCell.getContent() returns an empty list
            *
            * I want to do something like this:
            * String cellText = tabeCell.getText();  ???
            *
            * if ( cellText.equals( "PASSED" )) {
            *    cellShd.setFill( "00FF00" );
            * } else {
            *    cellShd.setFill( "FF0000" );
            * }
            *************************************************************************/
            }
         }
      }
   } catch ( Docx4JException ex ) {
   }


So I can reference the cell, but I don't know how to get the cell text. Is this the correct way to navigate to the text content of a table cell?

The "<w:tc>-<w:p>-<w:sdt>-<w:sdtContent>" constellation occur only if the table cell is the last cell in the row (except the last cell in the last row). For other cells the path to the content is "<w:tc>-<w:sdt>-<w:sdtContent>" and the navigation to the text is possible for me.

Regards,
Adolar