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,
match_mode, search_mode):
value = None
# 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('Rows', lookup_array.Rows.Count)
@ -34,32 +38,25 @@ class ZAZFunctions(unohelper.Base, XZAZFunctions):
source = lookup_array.DataArray
target = return_array.DataArray
index = next((i for i, v in enumerate(source) if v[0] == lookup_value), None)
if not index is None:
value = (target[index],)
if search_mode == 1:
index = next((i for i, v in enumerate(source) if v[0] == lookup_value), None)
elif search_mode == -1:
index = next((i for i, v in enumerate(reversed(source)) if v[0] == lookup_value), None)
# ~ horizontal = False
# ~ if len(source[0]) == 1:
# ~ source = [r[0] for r in source]
# ~ else:
# ~ horizontal = True
# ~ source = source[0]
# ~ 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 index is None:
if match_mode in (-1, 1, 2):
source = map(lambda x: x[0], source))
if search_mode == -1:
source = reversed(source)
source = tuple(source)
if match_mode == -1:
index = bisect.bisect_right(source, lookup_value)
value = (target[index - 1],)
elif match_mode == 1:
index = bisect.bisect_left(source, lookup_value)
value = (target[index],)
else:
value = (target[index],)
if value is None and if_not_found:
value = ((if_not_found,),)