@@ -450,38 +450,75 @@ private JSONArray getDashboardWidgetsJson(String templateString) {
450450 }
451451 }
452452
453- private void setFormattedCellValue (Workbook wb , int precision , String type , Cell cell , String stringifiedValue ) {
453+ private void setFormattedCellValue (Workbook wb , JSONObject visualizationType , String type , Cell cell , String stringifiedValue ) throws JSONException {
454454 CreationHelper creationHelper = wb .getCreationHelper ();
455455
456456 DateFormat dateFormat = new SimpleDateFormat (DATE_FORMAT , getLocale ());
457457
458458 switch (type ) {
459459 case "int" :
460460 if (!stringifiedValue .trim ().isEmpty ()) {
461- cell .getCellStyle ().setDataFormat (getFormat (precision , creationHelper ));
462- cell .setCellValue (Integer .parseInt (stringifiedValue ));
461+ cell .getCellStyle ().setDataFormat (getFormat (visualizationType , creationHelper ));
462+ if (visualizationType .has ("prefix" ) || visualizationType .has ("suffix" )) {
463+ stringifiedValue = visualizationType .optString ("prefix" , "" ) + stringifiedValue ;
464+ if (visualizationType .has ("precision" )) {
465+ int precision = visualizationType .getInt ("precision" );
466+ if (precision > 0 ) {
467+ stringifiedValue += "." + "0" .repeat (precision );
468+ }
469+ stringifiedValue += visualizationType .optString ("suffix" , "" );
470+ }
471+ cell .setCellValue (stringifiedValue );
472+ } else {
473+ cell .setCellValue (Integer .parseInt (stringifiedValue ));
474+ }
463475 }
464476 break ;
465477 case "float" :
466478 if (!stringifiedValue .trim ().isEmpty ()) {
467- cell .getCellStyle ().setDataFormat (getFormat (precision , creationHelper ));
468- cell .setCellValue (Double .parseDouble (stringifiedValue ));
479+ cell .getCellStyle ().setDataFormat (getFormat (visualizationType , creationHelper ));
480+ if (visualizationType .has ("prefix" ) || visualizationType .has ("suffix" )) {
481+ if (visualizationType .has ("precision" )) {
482+ int precision = visualizationType .getInt ("precision" );
483+ double doubleValue = Double .parseDouble (stringifiedValue );
484+ double factor = Math .pow (10 , precision );
485+ doubleValue = Math .round (doubleValue * factor ) / factor ;
486+ stringifiedValue = String .format (String .format ("%%.%df" , precision ), doubleValue );
487+ }
488+ stringifiedValue = visualizationType .optString ("prefix" , "" ) + stringifiedValue ;
489+ stringifiedValue = stringifiedValue + visualizationType .optString ("suffix" , "" );
490+ cell .setCellValue (stringifiedValue );
491+ } else {
492+ cell .setCellValue (Double .parseDouble (stringifiedValue ));
493+ }
469494 }
470495 break ;
471496 case "date" :
472497 try {
473498 if (!stringifiedValue .trim ().isEmpty ()) {
474- Date date = dateFormat .parse (stringifiedValue );
475- cell .setCellValue (date );
499+ cell .getCellStyle ().setDataFormat (getFormat (visualizationType , creationHelper ));
500+ if (visualizationType .has ("prefix" ) || visualizationType .has ("suffix" )) {
501+ stringifiedValue = visualizationType .optString ("prefix" , "" ) + stringifiedValue ;
502+ stringifiedValue = stringifiedValue + visualizationType .optString ("suffix" , "" );
503+ cell .setCellValue (stringifiedValue );
504+ } else {
505+ Date date = dateFormat .parse (stringifiedValue );
506+ cell .setCellValue (date );
507+ }
476508 }
477509 } catch (Exception e ) {
478510 LOGGER .debug ("Date will be exported as string due to error: " , e );
479511 cell .setCellValue (stringifiedValue );
480512 }
513+ break ;
481514 case "string" :
482515 if (!stringifiedValue .trim ().isEmpty ()) {
516+ if (visualizationType .has ("prefix" ) || visualizationType .has ("suffix" )) {
517+ stringifiedValue = visualizationType .optString ("prefix" , "" ) + stringifiedValue ;
518+ stringifiedValue = stringifiedValue + visualizationType .optString ("suffix" , "" );
519+ }
483520 try {
484- cell .getCellStyle ().setDataFormat (getFormat (precision , creationHelper ));
521+ cell .getCellStyle ().setDataFormat (getFormat (visualizationType , creationHelper ));
485522 cell .setCellValue (Integer .parseInt (stringifiedValue ));
486523 } catch (Exception e ) {
487524 cell .setCellValue (stringifiedValue );
@@ -494,10 +531,12 @@ private void setFormattedCellValue(Workbook wb, int precision, String type, Cell
494531 }
495532 }
496533
497- private short getFormat (int precision , CreationHelper helper ) {
534+ private short getFormat (JSONObject visualizationType , CreationHelper helper ) throws JSONException {
498535 String format = "0" ;
499- if (precision != -1 ) {
500- format = getNumberFormatByPrecision (precision , format );
536+ if (visualizationType != null ) {
537+ if (visualizationType .has ("precision" )) {
538+ format = getNumberFormatByPrecision (visualizationType .getInt ("precision" ), format );
539+ }
501540 return helper .createDataFormat ().getFormat (format );
502541 }
503542 return helper .createDataFormat ().getFormat (format );
@@ -737,18 +776,18 @@ private CellStyle buildCols(Workbook wb, Sheet sheet, JSONObject settings, JSONA
737776 }
738777 cell .setCellStyle (cellStyle );
739778 }
740- int precision = getPrecisionByColumn (settings , column );
779+ JSONObject visualizationType = getVisualizationTypeByColumn (settings , column );
741780
742781 if (r < rows .length () - numberOfSummaryRows ) {
743- setFormattedCellValue (wb , precision , type , cell , stringifiedValue );
782+ setFormattedCellValue (wb , visualizationType , type , cell , stringifiedValue );
744783 } else {
745784 if (isSummaryColumnVisible (getDashboardHiddenColumnsList (settings , "hideFromSummary" ), column )) {
746785 String label = "" ;
747786 if (colIndex .equals ("column_1" )) {
748787 label = summaryRowsLabels .get (r - (rows .length () - numberOfSummaryRows )).concat (" " );
749788 }
750789 cell .setCellStyle (cellStyle );
751- setFormattedCellValue (wb , precision , type , cell , label .concat (stringifiedValue ));
790+ setFormattedCellValue (wb , visualizationType , type , cell , label .concat (stringifiedValue ));
752791 }
753792 }
754793 }
@@ -800,41 +839,6 @@ private Row createDashboardHeaderColumnNames(Sheet sheet, Map<String, String> gr
800839 }
801840 }
802841
803- protected int getPrecisionByColumn (JSONObject settings , JSONObject column ) {
804- try {
805- JSONObject visualization = getJsonObjectUtils ().getVisualizationFromSettings (settings );
806-
807- if (visualization == null || !visualization .has ("visualizationTypes" ))
808- return -1 ;
809-
810- JSONObject visualizationTypes = visualization .getJSONObject ("visualizationTypes" );
811-
812- JSONArray types = visualizationTypes .getJSONArray ("types" );
813-
814- for (int i = 0 ; i < types .length (); i ++) {
815- JSONObject type = types .getJSONObject (i );
816- JSONArray target ;
817- try {
818- target = type .getJSONArray ("target" );
819- } catch (JSONException e ) {
820- target = new JSONArray ();
821- target .put (type .getString ("target" ));
822- }
823-
824- if (type .has ("precision" )) {
825- if (target .toString ().contains (column .getString ("id" )) || target .toString ().contains ("all" )) {
826- return type .getInt ("precision" );
827- } else {
828- return -1 ;
829- }
830- }
831- }
832- } catch (Exception ignored ) {
833- return -1 ;
834- }
835- return -1 ;
836- }
837-
838842 public CellStyle buildPoiCellStyle (Style style , XSSFFont font , Workbook wb ) {
839843 CellStyle cellStyle = wb .createCellStyle ();
840844
0 commit comments