From 4a88b6a38adf2a08b9514e8155894780aade1981 Mon Sep 17 00:00:00 2001 From: Tomek Mackowski Date: Sat, 11 Dec 2010 22:28:48 +0000 Subject: [PATCH] re #42 Adding methods for conversion of date and time from Excel to SAP format. Including also unit tests for the conversion methods. git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@76 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049 --- ZA2X/CLAS/ZCL_EXCEL_COMMON.slnk | 277 +++++++++++++++++++++++++------- 1 file changed, 215 insertions(+), 62 deletions(-) diff --git a/ZA2X/CLAS/ZCL_EXCEL_COMMON.slnk b/ZA2X/CLAS/ZCL_EXCEL_COMMON.slnk index e4dfd6d..63436f0 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_COMMON.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_COMMON.slnk @@ -1,17 +1,18 @@ - + class ZCL_EXCEL_COMMON definition public final create public . -public section. *"* public components of class ZCL_EXCEL_COMMON *"* do not include other source files here!!! +public section. class-data C_EXCEL_NUMFMT_OFFSET type INT1 value 164. "#EC NOTEXT . class-data C_SPRAS_EN type SPRAS value 'E'. "#EC NOTEXT . class-data O_CONV type ref to CL_ABAP_CONV_OUT_CE . + constants C_EXCEL_BASELINE_DATE type D value '19000101'. "#EC NOTEXT class-methods GET_FIELDCATALOG importing @@ -53,13 +54,23 @@ public section. !I_COLUMNROW type STRING exporting !E_COLUMN type ZEXCEL_CELL_COLUMN_ALPHA - !E_ROW type ZEXCEL_CELL_ROW . + !E_ROW type ZEXCEL_CELL_ROW . + class-methods EXCEL_STRING_TO_DATE + importing + !IP_VALUE type ZEXCEL_CELL_VALUE + returning + value(EP_VALUE) type D . + class-methods EXCEL_STRING_TO_TIME + importing + !IP_VALUE type ZEXCEL_CELL_VALUE + returning + value(EP_VALUE) type T . *"* protected components of class ZCL_EXCEL_COMMON *"* do not include other source files here!!! protected section. - private section. -*"* private components of class ZCL_EXCEL_COMMON + *"* private components of class ZCL_EXCEL_COMMON *"* do not include other source files here!!! +private section. class-data C_EXCEL_COL_MODULE type INT2 value 64. "#EC NOTEXT . @@ -91,10 +102,8 @@ protected section. *----------------------------------------------------------------------* * *----------------------------------------------------------------------* -CLASS zcl_excel_common_test DEFINITION FOR TESTING - DURATION SHORT - RISK LEVEL HARMLESS -. +CLASS zcl_excel_common_test DEFINITION FOR TESTING. "#AU Risk_Level Harmless + "#AU Duration Short *?#<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"> *?<asx:values> *?<TESTCLASS_OPTIONS> @@ -120,6 +129,8 @@ CLASS zcl_excel_common_test DEFINITION FOR TESTING f_cut TYPE REF TO zcl_excel_common. "class under test METHODS: convert_columnrow2column_a_row FOR TESTING. + METHODS: date_conversions FOR TESTING. + METHODS: time_conversions FOR TESTING. ENDCLASS. "zcl_Excel_Common_Test @@ -145,13 +156,13 @@ CLASS zcl_excel_common_test IMPLEMENTATION. e_row = e_row ). - cl_abap_unit_assert=>assert_equals( + cl_aunit_assert=>assert_equals( act = e_column exp = 'AB' msg = 'Testing value e_Column' * level = ). - cl_abap_unit_assert=>assert_equals( + cl_aunit_assert=>assert_equals( act = e_row exp = 344 msg = 'Testing value e_Row' @@ -168,13 +179,13 @@ CLASS zcl_excel_common_test IMPLEMENTATION. e_row = e_row ). - cl_abap_unit_assert=>assert_equals( + cl_aunit_assert=>assert_equals( act = e_column exp = 'B' msg = 'Testing value e_Column' * level = ). - cl_abap_unit_assert=>assert_equals( + cl_aunit_assert=>assert_equals( act = e_row exp = 3 msg = 'Testing value e_Row' @@ -182,19 +193,126 @@ CLASS zcl_excel_common_test IMPLEMENTATION. ). ENDMETHOD. "convert_Columnrow2column_A_Row + METHOD date_conversions. + DATA: lv_date TYPE d, + lv_cell TYPE zexcel_cell_value. +* test date conversions. Expected values generated using Excel 2010 + lv_date = '20100101'. + lv_cell = zcl_excel_common=>date_to_excel_string( lv_date ). + cl_aunit_assert=>assert_equals( + act = lv_cell + exp = '40179' + msg = 'Wrong date conversion' ). + lv_date = zcl_excel_common=>excel_string_to_date( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_date + exp = '20100101' + msg = 'Wrong date conversion' ). + + lv_date = '20080229'. + lv_cell = zcl_excel_common=>date_to_excel_string( lv_date ). + cl_aunit_assert=>assert_equals( + act = lv_cell + exp = '39507' + msg = 'Wrong date conversion' ). + + lv_date = zcl_excel_common=>excel_string_to_date( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_date + exp = '20080229' + msg = 'Wrong date conversion' ). + + lv_date = '19991231'. + lv_cell = zcl_excel_common=>date_to_excel_string( lv_date ). + cl_aunit_assert=>assert_equals( + act = lv_cell + exp = '36525' + msg = 'Wrong date conversion' ). + + lv_date = zcl_excel_common=>excel_string_to_date( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_date + exp = '19991231' + msg = 'Wrong date conversion' ). + ENDMETHOD. "date_conversions + + METHOD time_conversions. + DATA: lv_time TYPE t, + lv_cell TYPE zexcel_cell_value, + lv_value TYPE f. + +* test time conversions. Expected values generated using Excel 2010 + + lv_time = '000000'. + lv_value = zcl_excel_common=>time_to_excel_string( lv_time ). + cl_aunit_assert=>assert_equals_f( + act = lv_value + exp = 0 + msg = 'Wrong time conversion' ). + + lv_cell = lv_value. + lv_time = zcl_excel_common=>excel_string_to_time( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_time + exp = '000000' + msg = 'Wrong time conversion' ). + + lv_time = '090909'. + lv_value = zcl_excel_common=>time_to_excel_string( lv_time ). + cl_aunit_assert=>assert_equals_f( + act = lv_value + exp = '0.38135416666666666' + msg = 'Wrong time conversion' ). + + lv_cell = lv_value. + lv_time = zcl_excel_common=>excel_string_to_time( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_time + exp = '090909' + msg = 'Wrong time conversion' ). + + lv_time = '120000'. + lv_value = zcl_excel_common=>time_to_excel_string( lv_time ). + cl_aunit_assert=>assert_equals_f( + act = lv_value + exp = '0.5' + msg = 'Wrong time conversion' ). + + lv_cell = lv_value. + lv_time = zcl_excel_common=>excel_string_to_time( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_time + exp = '120000' + msg = 'Wrong time conversion' ). + + lv_time = '235959'. + lv_value = zcl_excel_common=>time_to_excel_string( lv_time ). + cl_aunit_assert=>assert_equals_f( + act = lv_value + exp = '0.99998842592592585' + msg = 'Wrong time conversion' ). + + lv_cell = lv_value. + lv_time = zcl_excel_common=>excel_string_to_time( lv_cell ). + cl_aunit_assert=>assert_equals( + act = lv_time + exp = '235959' + msg = 'Wrong time conversion' ). + ENDMETHOD. "time_conversions ENDCLASS. "zcl_Excel_Common_Test - - - - - - - + + + + + + + + method CHAR2HEX. IF o_conv IS NOT BOUND. @@ -210,9 +328,9 @@ ENDCLASS. "zcl_Excel_Common_Test endmethod. - - - + + + method CONVERT_COLUMN2ALPHA. DATA: lo_conv TYPE REF TO cl_abap_conv_in_ce, @@ -235,9 +353,9 @@ endmethod. endmethod. - - - + + + method CONVERT_COLUMN2INT. DATA: lv_uccpi TYPE i, @@ -266,11 +384,11 @@ endmethod. endmethod. - - - - - METHOD convert_columnrow2column_a_row. + + + + + method CONVERT_COLUMNROW2COLUMN_A_ROW. DATA: width TYPE i, col_width TYPE i, row_str TYPE string. @@ -284,25 +402,24 @@ endmethod. width = width - col_width. row_str = i_columnrow+col_width(width). e_row = row_str. -ENDMETHOD. - - - - - - method DATE_TO_EXCEL_STRING. - DATA: lv_date_diff TYPE i, - lc_date_baseline TYPE d VALUE '19000101'. - - CHECK ip_value IS NOT INITIAL. - lv_date_diff = ip_value - lc_date_baseline + 2. - ep_value = zcl_excel_common=>number_to_excel_string( ip_value = lv_date_diff ). endmethod. - - - + + + + METHOD date_to_excel_string. + DATA: lv_date_diff TYPE i. + + CHECK ip_value IS NOT INITIAL. + lv_date_diff = ip_value - c_excel_baseline_date + 2. + ep_value = zcl_excel_common=>number_to_excel_string( ip_value = lv_date_diff ). +ENDMETHOD. + + + + + method ENCRYPT_PASSWORD. DATA lv_curr_offset TYPE i. @@ -343,9 +460,45 @@ endmethod. endmethod. - - - + + + + METHOD excel_string_to_date. + DATA: lv_date_int TYPE i. + + TRY . + lv_date_int = ip_value. + ep_value = lv_date_int + c_excel_baseline_date - 2. + CATCH cx_sy_conversion_error. + CLEAR ep_value. + ENDTRY. +ENDMETHOD. + + + + + + METHOD excel_string_to_time. + DATA: lv_seconds_in_day TYPE i, +lv_day_fraction TYPE f, +lc_seconds_in_day TYPE i VALUE 86400. + + TRY. + + lv_day_fraction = ip_value. + lv_seconds_in_day = lv_day_fraction * lc_seconds_in_day. + + ep_value = lv_seconds_in_day. + + CATCH cx_sy_conversion_error. + CLEAR ep_value. + ENDTRY. +ENDMETHOD. + + + + + method GET_FIELDCATALOG. DATA: lr_data TYPE REF TO data, @@ -371,9 +524,9 @@ endmethod. endmethod. - - - + + + method NUMBER_TO_EXCEL_STRING. DATA: lv_value_c TYPE c LENGTH 100. @@ -389,9 +542,9 @@ endmethod. endmethod. - - - + + + method SHL01. DATA: @@ -410,9 +563,9 @@ endmethod. endmethod. - - - + + + method SHR14. DATA: @@ -438,9 +591,9 @@ endmethod. endmethod. - - - + + + method TIME_TO_EXCEL_STRING. DATA: lv_seconds_in_day TYPE i, lv_day_fraction TYPE f,