Skip to content

Commit f2de966

Browse files
fix(datagrid-web): update readers to handle optional attributes and create cell formatting functions
1 parent 910d478 commit f2de966

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

packages/pluggableWidgets/datagrid-web/src/features/data-export/DSExportRequest.ts

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,9 @@ export class DSExportRequest {
264264

265265
const readers: ReadersByType = {
266266
attribute(item, props) {
267-
if (props.attribute === undefined) {
268-
return makeEmptyCell();
269-
}
267+
const data = props.attribute?.get(item);
270268

271-
const data = props.attribute.get(item);
272-
273-
if (data.status !== "available") {
269+
if (data?.status !== "available") {
274270
return makeEmptyCell();
275271
}
276272

@@ -282,53 +278,35 @@ const readers: ReadersByType = {
282278
});
283279

284280
if (value instanceof Date) {
285-
return {
286-
t: format === undefined ? "s" : "d",
287-
v: format === undefined ? data.displayValue : value,
288-
z: format
289-
};
281+
return excelDate(format === undefined ? data.displayValue : value, format);
290282
}
291283

292284
if (typeof value === "boolean") {
293-
return {
294-
t: "b",
295-
v: value,
296-
w: value ? "TRUE" : "FALSE"
297-
};
285+
return excelBoolean(value);
298286
}
299287

300288
if (value instanceof Big || typeof value === "number") {
301289
const num = value instanceof Big ? value.toNumber() : value;
302-
return {
303-
t: "n",
304-
v: num,
305-
z: format
306-
};
290+
return excelNumber(num, format);
307291
}
308292

309-
return {
310-
t: "s",
311-
v: data.displayValue ?? ""
312-
};
293+
return excelString(data.displayValue ?? "");
313294
},
314295

315296
dynamicText(item, props) {
316-
if (props.dynamicText === undefined) {
317-
return makeEmptyCell();
318-
}
319-
320-
const data = props.dynamicText.get(item);
297+
const data = props.dynamicText?.get(item);
321298

322-
switch (data.status) {
299+
switch (data?.status) {
323300
case "available":
324301
const format = getCellFormat({
325302
exportType: props.exportType,
326303
exportDateFormat: props.exportDateFormat,
327304
exportNumberFormat: props.exportNumberFormat
328305
});
329-
return { t: "s", v: data.value ?? "", z: format };
306+
307+
return excelStringFormat(data.value ?? "", format);
330308
case "unavailable":
331-
return { t: "s", v: "n/a" };
309+
return excelString("n/a");
332310
default:
333311
return makeEmptyCell();
334312
}
@@ -341,16 +319,56 @@ const readers: ReadersByType = {
341319
exportDateFormat: props.exportDateFormat,
342320
exportNumberFormat: props.exportNumberFormat
343321
});
344-
return { t: "s", v: value, z: format };
322+
323+
return excelStringFormat(value, format);
345324
}
346325
};
347326

348327
function makeEmptyCell(): ExcelCell {
349328
return { t: "s", v: "" };
350329
}
351330

331+
function excelNumber(value: number, format?: string): ExcelCell {
332+
return {
333+
t: "n",
334+
v: value,
335+
z: format
336+
};
337+
}
338+
339+
function excelString(value: string): ExcelCell {
340+
return {
341+
t: "s",
342+
v: value
343+
};
344+
}
345+
346+
function excelStringFormat(value: string, format?: string): ExcelCell {
347+
return {
348+
t: "s",
349+
v: value,
350+
z: format
351+
};
352+
}
353+
354+
function excelDate(value: string | Date, format?: string): ExcelCell {
355+
return {
356+
t: format === undefined ? "s" : "d",
357+
v: value,
358+
z: format
359+
};
360+
}
361+
362+
function excelBoolean(value: boolean): ExcelCell {
363+
return {
364+
t: "b",
365+
v: value,
366+
w: value ? "TRUE" : "FALSE"
367+
};
368+
}
369+
352370
interface DataExportProps {
353-
exportType: "text" | "number" | "date" | "boolean";
371+
exportType: "default" | "number" | "date" | "boolean";
354372
exportDateFormat?: DynamicValue<string>;
355373
exportNumberFormat?: DynamicValue<string>;
356374
}

0 commit comments

Comments
 (0)