1

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.

mariuss
  • 1,177
  • 2
  • 14
  • 30
sticky_elbows
  • 1,344
  • 1
  • 16
  • 30
  • 1
    It's not related to your question, but note that instead of `cell.getAddress()` and such, you can write `cell.address`. Cleaner and more convenient. – ddekany Oct 10 '18 at 21:07

1 Answers1

0

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.

B-Wieg
  • 181
  • 14
  • You're absolutely right @B-Wieg, I tried to print out the value of cell and its only the loop counter. I guess I confused it with for:each loop in java :) – sticky_elbows Oct 11 '18 at 06:36