11import { isAvailable } from "@mendix/widget-plugin-platform/framework/is-available" ;
22import Big from "big.js" ;
3- import { ListValue , ObjectItem , ValueStatus } from "mendix" ;
3+ import { DynamicValue , ListValue , ObjectItem , ValueStatus } from "mendix" ;
44import { createNanoEvents , Emitter , Unsubscribe } from "nanoevents" ;
55import { ColumnsType , ShowContentAsEnum } from "../../../typings/DatagridProps" ;
66
7- /** Represents a single Excel cell (SheetJS compatible, simplified ) */
7+ /** Represents a single Excel cell (SheetJS compatible) */
88interface ExcelCell {
99 /** Cell type: 's' = string, 'n' = number, 'b' = boolean, 'd' = date */
1010 t : "s" | "n" | "b" | "d" ;
@@ -275,13 +275,17 @@ const readers: ReadersByType = {
275275 }
276276
277277 const value = data . value ;
278+ const format = getCellFormat ( {
279+ exportType : props . exportType ,
280+ exportDateFormat : props . exportDateFormat ,
281+ exportNumberFormat : props . exportNumberFormat
282+ } ) ;
278283
279284 if ( value instanceof Date ) {
280285 return {
281- t : "d" , // date cell
286+ t : "d" ,
282287 v : value ,
283- z : "dd/mm/yyyy hh:mm" , // Excel date format
284- w : value . toISOString ( ) . split ( "T" ) [ 0 ] // human-readable fallback
288+ z : format
285289 } ;
286290 }
287291
@@ -293,22 +297,18 @@ const readers: ReadersByType = {
293297 } ;
294298 }
295299
296- // Number (Big or JS number)
297300 if ( value instanceof Big || typeof value === "number" ) {
298301 const num = value instanceof Big ? value . toNumber ( ) : value ;
299302 return {
300303 t : "n" ,
301304 v : num ,
302- z : '"$"#,##0.00_);\\("$"#,##0.00\\)' ,
303- w : num . toLocaleString ( undefined , { minimumFractionDigits : 2 } )
305+ z : format
304306 } ;
305307 }
306308
307- // Default: string (ensure fallback is a string)
308309 return {
309310 t : "s" ,
310- v : data . displayValue ?? "" ,
311- w : data . displayValue ?? ""
311+ v : data . displayValue ?? ""
312312 } ;
313313 } ,
314314
@@ -321,23 +321,49 @@ const readers: ReadersByType = {
321321
322322 switch ( data . status ) {
323323 case "available" :
324- return { t : "s" , v : data . value ?? "" , w : data . value ?? "" } ;
324+ const format = getCellFormat ( {
325+ exportType : props . exportType ,
326+ exportDateFormat : props . exportDateFormat ,
327+ exportNumberFormat : props . exportNumberFormat
328+ } ) ;
329+ return { t : "s" , v : data . value ?? "" , z : format } ;
325330 case "unavailable" :
326- return { t : "s" , v : "n/a" , w : "n/a" } ;
331+ return { t : "s" , v : "n/a" } ;
327332 default :
328333 return makeEmptyCell ( ) ;
329334 }
330335 } ,
331336
332337 customContent ( item , props ) {
333338 const value = props . exportValue ?. get ( item ) . value ?? "" ;
334- return { t : "s" , v : value , w : value } ;
339+ const format = getCellFormat ( {
340+ exportType : props . exportType ,
341+ exportDateFormat : props . exportDateFormat ,
342+ exportNumberFormat : props . exportNumberFormat
343+ } ) ;
344+ return { t : "s" , v : value , z : format } ;
335345 }
336346} ;
337347
338- // Helper for empty cells
339348function makeEmptyCell ( ) : ExcelCell {
340- return { t : "s" , v : "" , w : "" } ;
349+ return { t : "s" , v : "" } ;
350+ }
351+
352+ interface DataExportProps {
353+ exportType : "text" | "number" | "date" | "boolean" ;
354+ exportDateFormat ?: DynamicValue < string > ;
355+ exportNumberFormat ?: DynamicValue < string > ;
356+ }
357+
358+ function getCellFormat ( { exportType, exportDateFormat, exportNumberFormat } : DataExportProps ) : string | undefined {
359+ switch ( exportType ) {
360+ case "date" :
361+ return exportDateFormat ?. status === "available" ? exportDateFormat . value : "mm/dd/yyyy" ;
362+ case "number" :
363+ return exportNumberFormat ?. status === "available" ? exportNumberFormat . value : undefined ;
364+ default :
365+ return undefined ;
366+ }
341367}
342368
343369function createRowReader ( columns : ColumnsType [ ] ) : RowReader {
0 commit comments