-
Notifications
You must be signed in to change notification settings - Fork 10
/
excel_json_formula_mid.py
66 lines (54 loc) · 1.73 KB
/
excel_json_formula_mid.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
#!/usr/bin/env python
__description__ = 'Excel json formula mid'
__author__ = 'Didier Stevens'
__version__ = '0.0.1'
__date__ = '2020/05/20'
"""
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2020/05/20: sample 9e5f99a95a30b35d53799d88be16c68c
Todo:
"""
import sys
import json
import re
def StartsWithAndEndsWith(string, start, end):
if not string.startswith(start):
return None
string = string[len(start):]
if not string.endswith(end):
return None
return string[:-len(end)]
def UnQuote(string, quotecharacter='"'):
if string[0] == quotecharacter and string[-1] == quotecharacter:
return string[1:-1]
else:
return string
def Main():
dCells = {}
cells = json.loads(sys.stdin.read())
for cell in cells:
found = StartsWithAndEndsWith(cell[2], u'SET.VALUE(', u')')
if found != None:
cellref, value = found.split(',', 1)
value = UnQuote(value)
dCells[cellref] = value
for cell in cells:
regexFormula = r'^FORMULA\((.+),([a-zA-Z0-9]+)\)$'
oMatch = re.match(regexFormula, cell[2])
if oMatch != None:
midstrings, cellref = oMatch.groups()
formula = ''
for mid in midstrings.split('&'):
found = StartsWithAndEndsWith(mid, u'MID(', u')')
if found != None:
cellref, offset, length = found.split(',')
formula += dCells[cellref][int(offset)-1]
print(formula)
# found = StartsWithAndEndsWith(cell[2], u'FORMULA(', u')')
# if found != None:
# print(found.split('&'))
if __name__ == '__main__':
Main()