Skip to content

Commit b7fe7f1

Browse files
authored
Fix #494 add min and max functions (#496)
- Fix #495 defer evaluation of py_value - pkdebug=xlsx provides significant trace output - added blank_table to sheet
1 parent c150211 commit b7fe7f1

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

pykern/xlsx.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ def _literal():
287287
_is_decimal=isinstance(content, decimal.Decimal),
288288
)
289289

290+
pkdc("{}!{}={}", cell.sheet.title, cell.xl_id, content)
290291
self.is_formula = isinstance(content, (list, tuple))
291292
if self.is_formula:
292293
_formula()
@@ -300,6 +301,8 @@ def is_decimal(self):
300301
def py_value(self):
301302
if (rv := self.get("_py_value")) is None:
302303
self._error("expected an expression with a single value expr={}", self)
304+
if callable(rv):
305+
self._py_value = rv = rv()
303306
if not isinstance(rv, (bool, str, decimal.Decimal)):
304307
self._error("unexpected expression value={} expr={}", rv, self)
305308
return rv
@@ -393,7 +396,7 @@ def _xl_id(cell):
393396
self.pkupdate(
394397
_link_ref=content,
395398
_is_decimal=l.expr.is_decimal(),
396-
_py_value=l.expr.py_value() if c.resolved_count == 1 else None,
399+
_py_value=(lambda: l.expr.py_value()) if c.resolved_count == 1 else None,
397400
_xl_formula=_xl_formula(c.pairs),
398401
cells=c.cells,
399402
resolved_count=c.resolved_count,
@@ -525,21 +528,23 @@ def init(cls):
525528
def _bool(token, func):
526529
cls(token, func, _is_decimal=False, _infix=True)
527530

528-
def _multi(token, func, init, xl):
531+
def _multi(token, func, init=None, xl=None):
529532
cls(
530533
token,
531534
"multi",
532535
_py_func_multi_func=func,
533-
_py_func_multi_init=decimal.Decimal(init),
534-
_xl_func=xl,
536+
_py_func_multi_init=None if init is None else decimal.Decimal(init),
537+
_xl_func=xl or token,
535538
operand_count=(1, 65535),
536539
_is_multi=True,
537540
_is_decimal=True,
538541
)
539542

540543
cls("%", lambda x, y: x % y, _xl_func="MOD", _is_decimal=True)
541-
_multi("*", lambda x, y: x * y, 1, "PRODUCT")
542-
_multi("+", lambda x, y: x + y, 0, "SUM")
544+
_multi("*", lambda rv, y: rv * y, 1, "PRODUCT")
545+
_multi("+", lambda rv, y: rv + y, 0, "SUM")
546+
_multi("MAX", max)
547+
_multi("MIN", min)
543548
cls(
544549
"-",
545550
"minus",
@@ -570,7 +575,7 @@ def evaluate(self, operands, cell):
570575
),
571576
_op_spec=self,
572577
_operands=o,
573-
_py_value=self._py_func(o),
578+
_py_value=lambda: self._py_func(o),
574579
_xl_formula=self._xl_func(o),
575580
resolved_count=1,
576581
)
@@ -646,7 +651,7 @@ def _iter():
646651
self._error(
647652
"not decimal operand type={} value={} operand={}", type(v), v, e
648653
)
649-
rv = self._py_func_multi_func(rv, v)
654+
rv = v if rv is None else self._py_func_multi_func(rv, v)
650655
return rv
651656

652657
def _xl_func_default(self, operands):
@@ -766,6 +771,10 @@ def __init__(self, cfg):
766771
super().__init__(cfg)
767772
self.tables = []
768773

774+
def blank_table(self):
775+
"""Create a table with one row, which is blank"""
776+
self.table(title=f"blank_table-{len(self.tables)}").row()
777+
769778
def table(self, **kwargs):
770779
"""Appends table to sheets
771780

tests/xlsx_data/1.in/case.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
("IF", ["<=", ["n"], 2], "red", "green"),
4545
),
4646
).pkupdate(defaults=PKDict(num_fmt="currency"))
47+
t.row(
48+
Left=["MAX", 999],
49+
Middle=["MAX", 111, 222],
50+
three=["MIN", 333, 444],
51+
four=["IF", 0, ["/", 1, 0], 99],
52+
).pkupdate(defaults=PKDict(num_fmt="currency"))
4753
t.footer(
4854
Left="L",
4955
Middle=None,

tests/xlsx_data/1.out/case1.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
375,a fairly long string,7.77,TRUE,,3.14
22
Left,Middle,Right,Very End,,
33
135.34,35.34,0.34,green,,
4+
999,222,333,99,,
45
L,,R,,,
56
No Totals,,,,,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:F5"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="15"/><cols><col min="1" max="1" width="9.7109375" customWidth="1"/><col min="2" max="2" width="20.7109375" customWidth="1"/><col min="3" max="3" width="5.7109375" customWidth="1"/><col min="4" max="4" width="8.7109375" customWidth="1"/><col min="5" max="5" width="0" customWidth="1"/><col min="6" max="6" width="4.7109375" customWidth="1"/></cols><sheetData><row r="1" spans="1:6"><c r="A1" s="1"><v>375</v></c><c r="B1" s="2" t="s"><v>0</v></c><c r="C1" s="1"><v>7.77</v></c><c r="D1" s="2" t="s"><v>1</v></c><c r="E1" s="2"/><c r="F1" s="1"><v>3.14</v></c></row><row r="2" spans="1:6"><c r="A2" s="3" t="s"><v>2</v></c><c r="B2" s="3" t="s"><v>3</v></c><c r="C2" s="3" t="s"><v>4</v></c><c r="D2" s="3" t="s"><v>5</v></c></row><row r="3" spans="1:6"><c r="A3" s="4"><f>ROUND(PRODUCT(100--B3,1),2)</f><v>135.34</v></c><c r="B3" s="5"><v>35.34</v></c><c r="C3" s="1"><f>ROUND(MOD(B3,1),2)</f><v>0.34</v></c><c r="D3" s="5" t="str"><f>IF(B3&lt;=2,"red","green")</f><v>green</v></c></row><row r="4" spans="1:6"><c r="A4" s="6" t="s"><v>6</v></c><c r="B4" s="6"/><c r="C4" s="7" t="s"><v>7</v></c><c r="D4" s="6"/></row><row r="5" spans="1:6"><c r="A5" s="2" t="s"><v>8</v></c><c r="B5" s="2"/><c r="C5" s="2"/><c r="D5" s="2"/></row></sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><ignoredErrors><ignoredError sqref="A1 C1 F1 A3 B3 C3" numberStoredAsText="1"/></ignoredErrors></worksheet>
2+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:F6"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="15"/><cols><col min="1" max="1" width="9.7109375" customWidth="1"/><col min="2" max="2" width="20.7109375" customWidth="1"/><col min="3" max="4" width="8.7109375" customWidth="1"/><col min="5" max="5" width="0" customWidth="1"/><col min="6" max="6" width="4.7109375" customWidth="1"/></cols><sheetData><row r="1" spans="1:6"><c r="A1" s="1"><v>375</v></c><c r="B1" s="2" t="s"><v>0</v></c><c r="C1" s="1"><v>7.77</v></c><c r="D1" s="2" t="s"><v>1</v></c><c r="E1" s="2"/><c r="F1" s="1"><v>3.14</v></c></row><row r="2" spans="1:6"><c r="A2" s="3" t="s"><v>2</v></c><c r="B2" s="3" t="s"><v>3</v></c><c r="C2" s="3" t="s"><v>4</v></c><c r="D2" s="3" t="s"><v>5</v></c></row><row r="3" spans="1:6"><c r="A3" s="4"><f>ROUND(PRODUCT(100--B3,1),2)</f><v>135.34</v></c><c r="B3" s="5"><v>35.34</v></c><c r="C3" s="1"><f>ROUND(MOD(B3,1),2)</f><v>0.34</v></c><c r="D3" s="5" t="str"><f>IF(B3&lt;=2,"red","green")</f><v>green</v></c></row><row r="4" spans="1:6"><c r="A4" s="6"><f>ROUND(MAX(999),2)</f><v>999.00</v></c><c r="B4" s="6"><f>ROUND(MAX(111,222),2)</f><v>222.00</v></c><c r="C4" s="6"><f>ROUND(MIN(333,444),2)</f><v>333.00</v></c><c r="D4" s="6"><f>ROUND(IF(0,1/0,99),2)</f><v>99.00</v></c></row><row r="5" spans="1:6"><c r="A5" s="7" t="s"><v>6</v></c><c r="B5" s="7"/><c r="C5" s="8" t="s"><v>7</v></c><c r="D5" s="7"/></row><row r="6" spans="1:6"><c r="A6" s="2" t="s"><v>8</v></c><c r="B6" s="2"/><c r="C6" s="2"/><c r="D6" s="2"/></row></sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><ignoredErrors><ignoredError sqref="A1 C1 F1 A3 B3 C3 A4 B4 C4 D4" numberStoredAsText="1"/></ignoredErrors></worksheet>

0 commit comments

Comments
 (0)