Skip to content

Commit

Permalink
feat: Applied format to date column in excel template created (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik authored Dec 8, 2023
2 parents a19d8c2 + f5c9e57 commit 70ffc85
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
34 changes: 10 additions & 24 deletions apps/api/src/app/shared/services/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand All @@ -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,
});
}
});

Expand Down
1 change: 1 addition & 0 deletions apps/api/src/app/shared/types/file.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface IExcelFileHeading {
isRequired?: boolean;
type: ColumnTypesEnum;
selectValues?: string[];
dateFormats?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions libs/shared/src/utils/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const Defaults = {
READY_STATE: 1,
CHUNK_SIZE: 100,
DATE_FORMATS: ['DD/MM/YYYY'],
DATE_FORMAT: 'DD/MM/YYYY',
};

0 comments on commit 70ffc85

Please sign in to comment.