From 7c89796948e5bae73f746e86dc78dd710adc694b Mon Sep 17 00:00:00 2001 From: stuartjchapman Date: Thu, 8 Feb 2018 13:52:17 +0000 Subject: [PATCH 1/3] Special Case permitting single row to referenced in Range() A special case where only the header row needs to be referenced appears difficult to implement. Referencing the table header row is useful in e.g. Index(Match()) type formulae. Suggest a special case where bottom_row=False (rather than None or a index item key being provided) in which the bottom_row_offset is set to the top_row_offset so as to reference the single header row. --- xltable/expression.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xltable/expression.py b/xltable/expression.py index f71f4d8..85b410d 100644 --- a/xltable/expression.py +++ b/xltable/expression.py @@ -207,7 +207,7 @@ class Range(Expression): :param left_col: Left most column label this refers to. :param right_col: Right most column label this refers to. :param top_row: Top most row label, or None to select from the top of the table. - :param bottom_row: Bottom most row label, or None to select to the bottom of the table. + :param bottom_row: Bottom most row label, or None to select to the bottom of the table, or False to select single row :param include_header: Include table header in the range. :param table: Name of table the column is in, if not in the same table this expression is in. Use "%s!%s" % (worksheet.name, table.name) if refering to a table in another worksheet @@ -247,6 +247,8 @@ def resolve(self, workbook, row, col): if self.__bottom is None: bottom_row_offset = table.height - 1 + elif self.__bottom is False: + bottom_row_offset = top_row_offset else: bottom_row_offset = table.get_row_offset(self.__bottom) From 3e938903bdfba86a6b94b0b06b88459bcf0298d3 Mon Sep 17 00:00:00 2001 From: stuartjchapman Date: Mon, 12 Feb 2018 21:35:47 +0000 Subject: [PATCH 2/3] add class called ArrayConstant --- xltable/expression.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/xltable/expression.py b/xltable/expression.py index 85b410d..027e2b1 100644 --- a/xltable/expression.py +++ b/xltable/expression.py @@ -357,6 +357,21 @@ def resolve(self, workbook, row, col): return str(self.__value) +class ArrayConstant(Expression): + """ + An evaluated array, an "Array Constant". Can increase performance and/or + reduce dependencies + """ + def __init__(self, array, **kwargs): + super(ArrayConstant, self).__init__(**kwargs) + self.value = array + self.__value = array + + def resolve(self, workbook, row, col): + arr_str = ";".join(",".join(map(str,row)) for row in self.__value) + return "{%s}" % arr_str + + def _to_addr(worksheet, row, col, row_fixed=False, col_fixed=False): """converts a (0,0) based coordinate to an excel address""" addr = "" From b4eccc08aa3d6c3ac8c6bc34ce1e10ecc9eb6ba5 Mon Sep 17 00:00:00 2001 From: stuartjchapman Date: Mon, 12 Feb 2018 21:37:24 +0000 Subject: [PATCH 3/3] expose ArrayConstant class externally --- xltable/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xltable/__init__.py b/xltable/__init__.py index b83a489..ba3dd95 100644 --- a/xltable/__init__.py +++ b/xltable/__init__.py @@ -101,6 +101,8 @@ .. autoclass:: ArrayExpression +.. autoclass:: ArrayConstant + .. autoclass:: Cell .. autoclass:: Column @@ -118,7 +120,7 @@ .. autoclass:: Value """ -from .expression import Column, Index, Cell, Range, Formula, ConstExpr, Expression, ArrayExpression +from .expression import Column, Index, Cell, Range, Formula, ConstExpr, Expression, ArrayExpression, ArrayConstant from .style import CellStyle, TableStyle from .table import Table, Value, ArrayFormula from .chart import Chart @@ -143,4 +145,5 @@ "Formula", "ArrayExpression", "ConstExpr", + "ArrayConstant" ]