Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Applied format to date column in excel template created #428

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
};
Loading