Traverse Cells API

  • Last update:  2020-12-17
  • I. Overview

    Use cases: sometimes users may have a demand for processing every single cell in a report. For this purpose, is there a way to traverse all the cells using codes?

    Introduction: the method report.cellIterator( )Finereport provides has access to all cells that are placed inside the Iterator of Report, which makes it possible to do cell traversal as with any other Iterator objects, so as to process the cells or take other actions.

    In this way, you can traverse all or part of the cells as needed. The specific method and a practical use case will be demonstrated hereinafter.

    II. Principle

    1. Obtain The Report 

    Obtain the Report (the container of cells) before cell traversal. Here we take Group.cpt as an example:

    // Read and save the template as WorkBook object  
    workbook = TemplateWorkBookIO.readTemplateWorkBook("/doc/Primary/GroupReport/Group.cpt");


    2. Traverse Cells 

    // Traverse cells  
    int col = 0, row = 0;
    byte direction = 0;
    java.util.Iterator it = report.cellIterator();
    while (it.hasNext()) {
        TemplateCellElement cell = (TemplateCellElement) it.next();
        // Get the row and column numbers and exchange values  
        col = cell.getColumn();
        row = cell.getRow();
        cell.setColumn(row);
        cell.setRow(col);
        // Acquire the expansion direction of the original cell, with 0 standing for vertical while 1 for horizontal.  
        direction = cell.getCellExpandAttr().getDirection();
        if (direction == 0) {
            cell.getCellExpandAttr().setDirection((byte) 1);
        } else if (direction == 1) {
            cell.getCellExpandAttr().setDirection((byte) 0);
        }
        // Add the altered cell into a new WorkSheet  
        newworksheet.addCellElement(cell);
    }

    III. Example 

    1. Switch Reports Horizontally And Vertically

    To make it happen, use the input variable to decide whether a horizontal-vertical switch is needed. If so, go on with cell traversal and exchange the numbers of columns and rows.

    Please see the Group.cpt below:

    1.png

    Load the template into a class, and control the horizontal/vertical direction using the value of parameter "change".

    • If the "change" value is 0 or empty, display without change.

    • If the "change" value is 1, switch the columns and rows of the report, and convert it into a web report.

    Please see the detailed code here:

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/demo/ChangeRowAndCol.java


    2. Release and Preview 

    Place the complied class file ChangeRowAndCol.class into %FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\demo.

    Startup the server and go to the following URL

    http://localhost:8075/webroot/decision/view/report?viewlet=com.fr.demo.ChangeRowAndCol, with its result as shown below:

    1607069030792962.png

    Add &change=1 to the end of the URL:

    http://localhost:8075/webroot/decision/view/report?viewlet=com.fr.demo.ChangeRowAndCol&change=1 and see the result as follows:

    1607069047413425.png

    So, the feature of switching report direction has been implemented.


    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy