Search code examples
javalistfreemarkerxssf

FreeMarker: left-hand operand: Expected a hash, but this has evaluated to a number (wrapper: f.t.SimpleNumber)


I am using freemarker 2.3.28 and trying to assign a value to a variable but I get the error in the title

<#list 0..row.getLastCellNum()-1 as cell>
        <#assign cellValue = cell.getStringCellValue()>
        <#assign cellAddress = cell.getAddress().toString()>
        <#if someCondition>                                         
            <td style='background-color:orange'>${cellValue}</td>
        <#else>
            <td>${cellValue}</td>
        </#if>
</#list>

The error occurs at the second and third lines. The variable row is a XSSFRow object which is passed in inside a List that I am iterating through with an outer list (<#list rows as row>) which is not shown here.

Why is this happening and how can I resolve this?

Thank you.


Solution

  • It seems to me that cell is never being assigned to an actual cell object. Your <#list 0..row.getLastCellNum()-1 as cell> line is just going to assign the loop counter number to the cell variable. If you changed it to this you'd be ok?:

    <#list 0..row.getLastCellNum()-1 as idx>
        <#assign cell = cell.getCell(idx)>
        <#assign cellValue = cell.getStringCellValue()>
        <#assign cellAddress = cell.getAddress().toString()>
        <#if someCondition>                                         
            <td style='background-color:orange'>${cellValue}</td>
        <#else>
            <td>${cellValue}</td>
        </#if>
    </#list>
    

    I didn't see anything in the POI javadoc that exposed getting all the cells from the row otherwise that would be simpler.