search in reverse

This commit is contained in:
El Mau 2022-02-08 23:23:31 -06:00
parent c59b1258da
commit cf41711716
2 changed files with 18 additions and 21 deletions

Binary file not shown.

View File

@ -24,9 +24,13 @@ class ZAZFunctions(unohelper.Base, XZAZFunctions):
def zxlookup(self, lookup_value, lookup_array, return_array, if_not_found, def zxlookup(self, lookup_value, lookup_array, return_array, if_not_found,
match_mode, search_mode): match_mode, search_mode):
value = None
# The python default value not set in custom functions # The python default value not set in custom functions
if match_mode is None:
match_mode = 0
if search_mode is None:
search_mode = 1
value = None
# ~ print('Columns', lookup_array.Columns.Count) # ~ print('Columns', lookup_array.Columns.Count)
# ~ print('Rows', lookup_array.Rows.Count) # ~ print('Rows', lookup_array.Rows.Count)
@ -34,32 +38,25 @@ class ZAZFunctions(unohelper.Base, XZAZFunctions):
source = lookup_array.DataArray source = lookup_array.DataArray
target = return_array.DataArray target = return_array.DataArray
index = next((i for i, v in enumerate(source) if v[0] == lookup_value), None) if search_mode == 1:
if not index is None: index = next((i for i, v in enumerate(source) if v[0] == lookup_value), None)
value = (target[index],) elif search_mode == -1:
index = next((i for i, v in enumerate(reversed(source)) if v[0] == lookup_value), None)
# ~ horizontal = False if index is None:
# ~ if len(source[0]) == 1: if match_mode in (-1, 1, 2):
# ~ source = [r[0] for r in source] source = map(lambda x: x[0], source))
# ~ else: if search_mode == -1:
# ~ horizontal = True source = reversed(source)
# ~ source = source[0] source = tuple(source)
# ~ if lookup_value in source:
# ~ index = source.index(lookup_value)
# ~ if horizontal:
# ~ value = ((return_array[0][index],),)
# ~ else:
# ~ value = (return_array[index],)
if value is None:
source = tuple(map(lambda x: x[0], source))
if match_mode == -1: if match_mode == -1:
index = bisect.bisect_right(source, lookup_value) index = bisect.bisect_right(source, lookup_value)
value = (target[index - 1],) value = (target[index - 1],)
elif match_mode == 1: elif match_mode == 1:
index = bisect.bisect_left(source, lookup_value) index = bisect.bisect_left(source, lookup_value)
value = (target[index],) value = (target[index],)
else:
value = (target[index],)
if value is None and if_not_found: if value is None and if_not_found:
value = ((if_not_found,),) value = ((if_not_found,),)