I do not believe that this can work from an onFetch script, this kind of script should be placed in the onRender event of the data element. In addition, formatter returns a result, so it should be:
this.setDisplayValue(df.format(row["invoicedquantity"]));
But I think it would be easier to create a calculated column as a data type of "String" in a dataset with an expression:
if(row["unittype"]=="Nos"){ Formatter.format(row["invoicedquantity"],"#,###.## Kg"); }else{ Formatter.format(row["invoicedquantity"],"#,### Kg"); }
EDIT: After taking a deeper look at this, I found a more suitable way by changing the "numberformat" property. In the onRender or onCreate script of the data element (which should be of type number ), we can do something like:
if(row["unittype"]=="Nos"){ this.getStyle().numberFormat="#,###.## Kg"; }else{ this.getStyle().numberFormat="#,### Kg"; }
This is a better approach because the data item still has a numeric data type, so if the report is exported to excel, it will be recognized as excel as a number.
source share