From f5c9e5729473a90a8ed2efd769f61e3b656cc9aa Mon Sep 17 00:00:00 2001 From: chavda-bhavik Date: Fri, 8 Dec 2023 14:47:04 +0530 Subject: [PATCH] feat: Applied format to date column in excel template created --- .../app/shared/services/file/file.service.ts | 34 ++++++------------- apps/api/src/app/shared/types/file.types.ts | 1 + .../save-sample-file.usecase.ts | 1 + libs/shared/src/utils/defaults.ts | 1 + 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/apps/api/src/app/shared/services/file/file.service.ts b/apps/api/src/app/shared/services/file/file.service.ts index 6a29671f3..216ca3782 100644 --- a/apps/api/src/app/shared/services/file/file.service.ts +++ b/apps/api/src/app/shared/services/file/file.service.ts @@ -57,22 +57,6 @@ export class ExcelFileService { error: 'Please select from the list', }); } - addDateValidation({ ws, range, isRequired }: { ws: ExcelJS.Worksheet; range: string; isRequired: boolean }) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - ws.dataValidations.add(range, { - type: 'date', - allowBlank: !isRequired, - operator: 'greaterThan', - formulae: [''], - showErrorMessage: true, - error: 'Please select a date', - errorTitle: 'Invalid Date', - showInputMessage: true, - promptTitle: 'Date', - prompt: 'Select a date', - }); - } getExcelColumnNameFromIndex(columnNumber: number) { // To store result (Excel column name) const columnName = []; @@ -101,7 +85,16 @@ export class ExcelFileService { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Data'); const headingNames = headings.map((heading) => heading.key); - worksheet.addRow(headingNames); + worksheet.columns = headings.map((heading) => { + if (heading.type === ColumnTypesEnum.DATE) + return { + header: heading.key, + key: heading.key, + style: { numFmt: heading.dateFormats?.[0] || Defaults.DATE_FORMAT }, + }; + + return { header: heading.key, key: heading.key }; + }); headings.forEach((heading, index) => { if (heading.type === ColumnTypesEnum.SELECT) { const keyName = this.addSelectSheet(workbook, heading); @@ -112,13 +105,6 @@ export class ExcelFileService { keyName, isRequired: heading.isRequired, }); - } else if (heading.type === ColumnTypesEnum.DATE) { - const columnName = this.getExcelColumnNameFromIndex(index + 1); - this.addDateValidation({ - ws: worksheet, - range: `${columnName}2:${columnName}9999`, - isRequired: heading.isRequired, - }); } }); diff --git a/apps/api/src/app/shared/types/file.types.ts b/apps/api/src/app/shared/types/file.types.ts index 9096741d4..0620ea6ff 100644 --- a/apps/api/src/app/shared/types/file.types.ts +++ b/apps/api/src/app/shared/types/file.types.ts @@ -5,4 +5,5 @@ export interface IExcelFileHeading { isRequired?: boolean; type: ColumnTypesEnum; selectValues?: string[]; + dateFormats?: string[]; } diff --git a/apps/api/src/app/shared/usecases/save-sample-file/save-sample-file.usecase.ts b/apps/api/src/app/shared/usecases/save-sample-file/save-sample-file.usecase.ts index 65c51d25d..c38c8b548 100644 --- a/apps/api/src/app/shared/usecases/save-sample-file/save-sample-file.usecase.ts +++ b/apps/api/src/app/shared/usecases/save-sample-file/save-sample-file.usecase.ts @@ -20,6 +20,7 @@ export class SaveSampleFile { type: columnItem.type as ColumnTypesEnum, selectValues: columnItem.selectValues, isRequired: columnItem.isRequired, + dateFormats: columnItem.dateFormats, })); const fileName = this.fileNameService.getSampleFileName(templateId); const sampleFileUrl = this.fileNameService.getSampleFileUrl(templateId); diff --git a/libs/shared/src/utils/defaults.ts b/libs/shared/src/utils/defaults.ts index 5079535c5..6396b21c4 100644 --- a/libs/shared/src/utils/defaults.ts +++ b/libs/shared/src/utils/defaults.ts @@ -7,4 +7,5 @@ export const Defaults = { READY_STATE: 1, CHUNK_SIZE: 100, DATE_FORMATS: ['DD/MM/YYYY'], + DATE_FORMAT: 'DD/MM/YYYY', };