import bisect import uno import unohelper from net.elmau.zaz.functions import XZAZFunctions ID_EXTENSION = 'net.elmau.zaz.functions' SERVICE = ('com.sun.star.sheet.AddIn',) class ZAZFunctions(unohelper.Base, XZAZFunctions): def __init__(self, ctx): self.ctx = ctx def zreverse(self, value): result = value[::-1] return result def zeval(self, value): result = eval(value) return result def zxlookup(self, lookup_value, lookup_array, return_array, if_not_found, match_mode, search_mode): # 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) source = lookup_array.DataArray target = return_array.DataArray 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) 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,),) return value g_ImplementationHelper = unohelper.ImplementationHelper() g_ImplementationHelper.addImplementation(ZAZFunctions, ID_EXTENSION, SERVICE)