-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
94 lines (83 loc) · 2.59 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
''' examples to show some xlsxwriter_pandasformula magic '''
import pandas as pd
import xlsxwriter_pandasformula as x
x.Constant('pi',3.14)
x.Constant('e',2.72)
price = x.View({
"sheet" : "price example"
, "anchor" : [0, 0]
, "name" : "PRICE((size), (vendor, food)) [exampleinput.csv]"
, "value" : pd.read_csv("exampleinput.csv", index_col=[0], header=[0,1])
}) # (size), (vendor, food)
print(price.value.columns)
total = x.View({
"sheet" : "price example"
, "anchor" : [7, 0]
, "name" : "TOTAL(vendor,food)"
, "value" : x.Formula({
"total" : ["total"]
, "rows" : ["total"]
, "cols" : price.cols()
, "vals" : lambda itotal, ivendor, ifood: f'=SUM({price.ref((x.ALL),(ivendor,ifood))})'
})
})
minprice = x.View({
"sheet" : "price example"
, "anchor" : [1, 7]
, "name" : "MINPRICE(size,food)"
, "value" : x.Formula({
"size" : x.dom['size']
, "food" : x.dom['food']
, "rows" : ["size"]
, "cols" : ["food"]
, "vals" : lambda isize, ifood: f'=MIN({price.ref((isize),("Pizz",ifood))},{price.ref((isize),("Izza",ifood))})'
})
})
avgprice = x.View({
"sheet" : "price example"
, "anchor" : [1, 11]
, "name" : "AVGPRICE(avgprice,vendor))"
, "value" : x.Formula({
"avgprice" : ['average']
, "vendor" : x.dom['vendor']
, "rows" : ["avgprice"]
, "cols" : ["vendor"]
, "vals" : lambda iavgprice, ivendor: f'=MIN({price.ref((x.ALL),(ivendor,x.ALL))})'
})
})
#self-referencing example: first define the view without values so that it can be referenced, then simply use set(<value function>) to set values
sizes = x.View({
"sheet" : "price example"
, "anchor" : [12,0]
, "name" : "SIZES(diameter,size))"
, "value" : x.Formula({
"diameter" : ['cm','dm']
, "size" : x.dom['size']
, "rows" : ["diameter"]
, "cols" : ["size"]
})
})
def sizes_formula(idiameter, isize):
''' the function to define the values of the "sizes" view '''
if isize=="S" and idiameter=="cm":
retval="20"
elif idiameter=="cm":
retval="(pi/e)*"+sizes.ref((idiameter),(x.dom["size"][x.dom["size"].index(isize)-1]))
else:
retval=sizes.ref(("cm"),(isize))+"/10"
return "="+retval
sizes.set(sizes_formula)
pricenewPizz = x.View({
"sheet" : "new price"
, "anchor" : [0, 0]
, "name" : "PRICENEWPIZZ((size),(newprice,food)))"
, "value" : x.Formula({
"size" : x.dom['size']
, "newprice": ["new price Pizz"]
, "food" : x.dom["food"]
, "rows" : ["size"]
, "cols" : ["newprice", "food"]
, "vals" : lambda isize, inewprice, ifood: f'=(pi/e)*AVERAGE({minprice.ref(isize,ifood,sheetref=True)},{price.ref((isize),("Izza",ifood),sheetref=True,debug=True)})'
})
})
x.writexls("example.xlsx")