@@ -721,7 +721,8 @@ public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not
721
721
722
722
$ rowNumber = $ rowValue = False ;
723
723
foreach ($ lookup_array as $ rowKey => $ rowData ) {
724
- if (strtolower ($ rowData [$ firstColumn ]) > strtolower ($ lookup_value )) {
724
+ if ((is_numeric ($ lookup_value ) && is_numeric ($ rowData [$ firstColumn ]) && ($ rowData [$ firstColumn ] > $ lookup_value )) ||
725
+ (!is_numeric ($ lookup_value ) && !is_numeric ($ rowData [$ firstColumn ]) && (strtolower ($ rowData [$ firstColumn ]) > strtolower ($ lookup_value )))) {
725
726
break ;
726
727
}
727
728
$ rowNumber = $ rowKey ;
@@ -742,6 +743,69 @@ public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not
742
743
} // function VLOOKUP()
743
744
744
745
746
+ /**
747
+ * HLOOKUP
748
+ * The HLOOKUP function searches for value in the top-most row of lookup_array and returns the value in the same column based on the index_number.
749
+ * @param lookup_value The value that you want to match in lookup_array
750
+ * @param lookup_array The range of cells being searched
751
+ * @param index_number The row number in table_array from which the matching value must be returned. The first row is 1.
752
+ * @param not_exact_match Determines if you are looking for an exact match based on lookup_value.
753
+ * @return mixed The value of the found cell
754
+ */
755
+ public static function HLOOKUP ($ lookup_value , $ lookup_array , $ index_number , $ not_exact_match =true ) {
756
+ $ lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue ($ lookup_value );
757
+ $ index_number = PHPExcel_Calculation_Functions::flattenSingleValue ($ index_number );
758
+ $ not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue ($ not_exact_match );
759
+
760
+ // index_number must be greater than or equal to 1
761
+ if ($ index_number < 1 ) {
762
+ return PHPExcel_Calculation_Functions::VALUE ();
763
+ }
764
+
765
+ // index_number must be less than or equal to the number of columns in lookup_array
766
+ if ((!is_array ($ lookup_array )) || (empty ($ lookup_array ))) {
767
+ return PHPExcel_Calculation_Functions::REF ();
768
+ } else {
769
+ $ f = array_keys ($ lookup_array );
770
+ $ firstRow = array_pop ($ f );
771
+ if ((!is_array ($ lookup_array [$ firstRow ])) || ($ index_number > count ($ lookup_array [$ firstRow ]))) {
772
+ return PHPExcel_Calculation_Functions::REF ();
773
+ } else {
774
+ $ columnKeys = array_keys ($ lookup_array [$ firstRow ]);
775
+ $ firstkey = $ f [0 ] - 1 ;
776
+ $ returnColumn = $ firstkey + $ index_number ;
777
+ $ firstColumn = array_shift ($ f );
778
+ }
779
+ }
780
+
781
+ if (!$ not_exact_match ) {
782
+ $ firstRowH = asort ($ lookup_array [$ firstColumn ]);
783
+ }
784
+
785
+ $ rowNumber = $ rowValue = False ;
786
+ foreach ($ lookup_array [$ firstColumn ] as $ rowKey => $ rowData ) {
787
+ if ((is_numeric ($ lookup_value ) && is_numeric ($ rowData ) && ($ rowData > $ lookup_value )) ||
788
+ (!is_numeric ($ lookup_value ) && !is_numeric ($ rowData ) && (strtolower ($ rowData ) > strtolower ($ lookup_value )))) {
789
+ break ;
790
+ }
791
+ $ rowNumber = $ rowKey ;
792
+ $ rowValue = $ rowData ;
793
+ }
794
+
795
+ if ($ rowNumber !== false ) {
796
+ if ((!$ not_exact_match ) && ($ rowValue != $ lookup_value )) {
797
+ // if an exact match is required, we have what we need to return an appropriate response
798
+ return PHPExcel_Calculation_Functions::NA ();
799
+ } else {
800
+ // otherwise return the appropriate value
801
+ return $ lookup_array [$ returnColumn ][$ rowNumber ];
802
+ }
803
+ }
804
+
805
+ return PHPExcel_Calculation_Functions::NA ();
806
+ } // function HLOOKUP()
807
+
808
+
745
809
/**
746
810
* LOOKUP
747
811
* The LOOKUP function searches for value either from a one-row or one-column range or from an array.
0 commit comments