diff --git a/src/app/modules/angular-slickgrid/models/fieldType.enum.ts b/src/app/modules/angular-slickgrid/models/fieldType.enum.ts index f31c870c1..0aed42ad5 100644 --- a/src/app/modules/angular-slickgrid/models/fieldType.enum.ts +++ b/src/app/modules/angular-slickgrid/models/fieldType.enum.ts @@ -1,8 +1,17 @@ export enum FieldType { + /** unknown type */ unknown, + + /** string type */ string, + + /** boolean type (true/false) */ boolean, + + /** integer number type (1,2,99) */ integer, + + /** float number (with decimal) type */ float, /** number includes Integer and Float */ @@ -11,52 +20,52 @@ export enum FieldType { /** new Date(), javascript Date object */ date, - /** Format: 'YYYY-MM-DD' => 2001-01-01 */ + /** Format: 'YYYY-MM-DD' => 2001-02-28 */ dateIso, - /** Format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' => 2001-01-01T14:00:00.123Z */ + /** Format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' => 2001-02-28T14:00:00.123Z */ dateUtc, /** new Date(), javacript Date Object with Time */ dateTime, - /** Format: 'YYYY-MM-DD HH:mm:ss' => 2001-01-01 14:01:01 */ + /** Format: 'YYYY-MM-DD HH:mm:ss' => 2001-02-28 14:01:01 */ dateTimeIso, - /** Format: 'YYYY-MM-DD h:mm:ss a' => 2001-01-01 11:01:01 pm */ + /** Format: 'YYYY-MM-DD h:mm:ss a' => 2001-02-28 11:01:01 pm */ dateTimeIsoAmPm, - /** Format: 'YYYY-MM-DD h:mm:ss A' => 2001-01-01 11:01:01 PM */ + /** Format: 'YYYY-MM-DD h:mm:ss A' => 2001-02-28 11:01:01 PM */ dateTimeIsoAM_PM, - /** Format: 'YYYY-MM-DD HH:mm' => 2001-01-01 14:01 */ + /** Format: 'YYYY-MM-DD HH:mm' => 2001-02-28 14:01 */ dateTimeShortIso, - /** Format (Euro): 'DD/MM/YYYY' => 02/28/2001 */ + /** Format (Euro): 'DD/MM/YYYY' => 28/02/2001 */ dateEuro, - /** Format (Euro): 'D/M/YY' => 2/28/12 */ + /** Format (Euro): 'D/M/YY' => 28/2/12 */ dateEuroShort, - /** Format (Euro): 'DD/MM/YYYY HH:mm' => 02/28/2001 13:01 */ + /** Format (Euro): 'DD/MM/YYYY HH:mm' => 28/02/2001 13:01 */ dateTimeShortEuro, /** Format (Euro): 'DD/MM/YYYY HH:mm:ss' => 02/28/2001 13:01:01 */ dateTimeEuro, - /** Format (Euro): 'DD/MM/YYYY hh:mm:ss a' => 02/28/2001 11:01:01 pm */ + /** Format (Euro): 'DD/MM/YYYY hh:mm:ss a' => 28/02/2001 11:01:01 pm */ dateTimeEuroAmPm, - /** Format (Euro): 'DD/MM/YYYY hh:mm:ss A' => 02/28/2001 11:01:01 PM */ + /** Format (Euro): 'DD/MM/YYYY hh:mm:ss A' => 28/02/2001 11:01:01 PM */ dateTimeEuroAM_PM, - /** Format (Euro): 'D/M/YY H:m:s' => 2/28/14 14:1:2 */ + /** Format (Euro): 'D/M/YY H:m:s' => 28/2/14 14:1:2 */ dateTimeEuroShort, - /** Format (Euro): 'D/M/YY h:m:s a' => 2/28/14 1:2:10 pm */ + /** Format (Euro): 'D/M/YY h:m:s a' => 28/2/14 1:2:10 pm */ dateTimeEuroShortAmPm, - /** Format (Euro): 'D/M/YY h:m:s A' => 2/28/14 14:1:1 PM */ + /** Format (Euro): 'D/M/YY h:m:s A' => 28/2/14 14:1:1 PM */ dateTimeEuroShortAM_PM, /** Format: 'MM/DD/YYYY' => 02/28/2001 */ diff --git a/src/app/modules/angular-slickgrid/services/__tests__/excelExport.service.spec.ts b/src/app/modules/angular-slickgrid/services/__tests__/excelExport.service.spec.ts index 6321ac21c..eb3e0e16d 100644 --- a/src/app/modules/angular-slickgrid/services/__tests__/excelExport.service.spec.ts +++ b/src/app/modules/angular-slickgrid/services/__tests__/excelExport.service.spec.ts @@ -1,5 +1,6 @@ import { TestBed } from '@angular/core/testing'; import { TranslateModule, TranslateService } from '@ngx-translate/core'; +import * as moment from 'moment-mini'; import { FileType, @@ -984,6 +985,261 @@ describe('ExcelExportService', () => { }); }); }); + + describe('useCellFormatByFieldType method', () => { + it('should return a date time format when using FieldType.dateTime and a Date object as input', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '2012-02-28 15:07:59'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTime); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeIso', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '2012-02-28 15:07:59'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeIso); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeShortIso', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '2012-02-28 15:07'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeShortIso); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeIsoAmPm', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '2012-02-28 03:07:59 pm'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeIsoAmPm); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeIsoAM_PM', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '2012-02-28 03:07:59 PM'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeIsoAM_PM); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateEuro', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/02/2012'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateEuro); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateEuroShort', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/2/12'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateEuroShort); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeEuro', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/02/2012 15:07:59'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuro); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeShortEuro', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/02/2012 15:07'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeShortEuro); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeEuroAmPm', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/02/2012 03:07:59 pm'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroAmPm); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeEuroAM_PM', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/02/2012 03:07:59 PM'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroAM_PM); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeEuroShort', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/2/12 15:7:59'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroShort); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeEuroShortAmPm', async () => { + const input = '2012-02-28 15:07:59'; + const expectedDate = '28/2/12 3:7:59 pm'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeEuroShortAmPm); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateUs', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '02/28/2012'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateUs); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateUsShort', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '2/28/12'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateUsShort); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeUs', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '02/28/2012 15:07:59'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUs); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeShortUs', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '02/28/2012 15:07'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeShortUs); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeUsAmPm', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '02/28/2012 03:07:59 pm'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsAmPm); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeUsAM_PM', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '02/28/2012 03:07:59 PM'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsAM_PM); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeUsShort', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '2/28/12 15:7:59'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsShort); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.dateTimeUsShortAmPm', async () => { + const input = new Date('2012-02-28 15:07:59'); + const expectedDate = '2/28/12 3:7:59 pm'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateTimeUsShortAmPm); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + xit('should return a date time format when using FieldType.dateUtc', async () => { + const input = moment('2013-05-23T17:55:00.325').utcOffset(420); // timezone that is +7 UTC hours + const expectedDate = '2013-05-24T04:55:00.325+07:00'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.dateUtc); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + + it('should return a date time format when using FieldType.date', async () => { + const input = new Date('2012-02-28T23:01:52.103Z'); + const expectedDate = '2012-02-28'; + + service.init(gridStub, dataViewStub); + await service.exportToExcel(mockExportExcelOptions); + const output = service.useCellFormatByFieldType(input, FieldType.date); + + expect(output).toEqual({ metadata: { style: 5 }, value: expectedDate }); + }); + }); }); describe('without ngx-translate', () => { diff --git a/src/app/modules/angular-slickgrid/services/excelExport.service.ts b/src/app/modules/angular-slickgrid/services/excelExport.service.ts index d295f0c64..9860aeab9 100644 --- a/src/app/modules/angular-slickgrid/services/excelExport.service.ts +++ b/src/app/modules/angular-slickgrid/services/excelExport.service.ts @@ -183,6 +183,53 @@ export class ExcelExportService { } } + /** use different Excel Stylesheet Format as per the Field Type */ + useCellFormatByFieldType(data: string | Date | moment_.Moment, fieldType: FieldType): ExcelCellFormat | string { + let outputData: ExcelCellFormat | string | Date | moment_.Moment = data; + switch (fieldType) { + case FieldType.dateTime: + case FieldType.dateTimeIso: + case FieldType.dateTimeShortIso: + case FieldType.dateTimeIsoAmPm: + case FieldType.dateTimeIsoAM_PM: + case FieldType.dateEuro: + case FieldType.dateEuroShort: + case FieldType.dateTimeEuro: + case FieldType.dateTimeShortEuro: + case FieldType.dateTimeEuroAmPm: + case FieldType.dateTimeEuroAM_PM: + case FieldType.dateTimeEuroShort: + case FieldType.dateTimeEuroShortAmPm: + case FieldType.dateUs: + case FieldType.dateUsShort: + case FieldType.dateTimeUs: + case FieldType.dateTimeShortUs: + case FieldType.dateTimeUsAmPm: + case FieldType.dateTimeUsAM_PM: + case FieldType.dateTimeUsShort: + case FieldType.dateTimeUsShortAmPm: + case FieldType.dateUtc: + case FieldType.date: + case FieldType.dateIso: + outputData = data; + if (data) { + const defaultDateFormat = mapMomentDateFormatWithFieldType(fieldType); + const isDateValid = moment(data as string, defaultDateFormat, false).isValid(); + const outputDate = (data && isDateValid) ? moment(data as string).format(defaultDateFormat) : data; + const dateFormatter = this._stylesheet.createFormat({ format: defaultDateFormat }); + outputData = { value: outputDate, metadata: { style: dateFormatter.id } }; + } + break; + case FieldType.number: + const val = isNaN(+data) ? null : data; + outputData = { value: val, metadata: { style: this._stylesheetFormats.numberFormatter.id } }; + break; + default: + outputData = data; + } + return outputData as string; + } + // ----------------------- // Private functions // ----------------------- @@ -429,51 +476,4 @@ export class ExcelExportService { return outputStrings; } - - /** use different Excel Stylesheet Format as per the Field Type */ - private useCellFormatByFieldType(data: string, fieldType: FieldType): ExcelCellFormat | string { - let outputData: ExcelCellFormat | string = data; - switch (fieldType) { - case FieldType.dateTime: - case FieldType.dateTimeIso: - case FieldType.dateTimeShortIso: - case FieldType.dateTimeIsoAmPm: - case FieldType.dateTimeIsoAM_PM: - case FieldType.dateEuro: - case FieldType.dateEuroShort: - case FieldType.dateTimeEuro: - case FieldType.dateTimeShortEuro: - case FieldType.dateTimeEuroAmPm: - case FieldType.dateTimeEuroAM_PM: - case FieldType.dateTimeEuroShort: - case FieldType.dateTimeEuroShortAmPm: - case FieldType.dateUs: - case FieldType.dateUsShort: - case FieldType.dateTimeUs: - case FieldType.dateTimeShortUs: - case FieldType.dateTimeUsAmPm: - case FieldType.dateTimeUsAM_PM: - case FieldType.dateTimeUsShort: - case FieldType.dateTimeUsShortAmPm: - case FieldType.dateUtc: - case FieldType.date: - case FieldType.dateIso: - outputData = data; - if (data) { - const defaultDateFormat = mapMomentDateFormatWithFieldType(fieldType); - const isDateValid = moment(data as string, defaultDateFormat, false).isValid(); - const outputDate = (data && isDateValid) ? moment(data as string).format(defaultDateFormat) : data; - const dateFormatter = this._stylesheet.createFormat({ format: defaultDateFormat }); - outputData = { value: outputDate, metadata: { style: dateFormatter.id } }; - } - break; - case FieldType.number: - const val = isNaN(+data) ? null : data; - outputData = { value: val, metadata: { style: this._stylesheetFormats.numberFormatter.id } }; - break; - default: - outputData = data; - } - return outputData; - } }