-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSpans.py
executable file
·125 lines (96 loc) · 3.57 KB
/
getSpans.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python
#
# from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
spans_key = 'span'
borders_key = 'brdr'
def extendEdgesOfSpan(key, dictCells, cellSpec):
dictCells[key][spans_key] = '"{}", "{}"'.format(cellSpec['r'], cellSpec['c'])
top_left_borders = dictCells[key][borders_key]
coords = key.split(":")
sheet = int(coords[0])
col = int(coords[2])
row = int(coords[1])
colSpan = int(cellSpec['c'])
rowSpan = int(cellSpec['r'])
if colSpan > 1 :
for ixRow in range(rowSpan) :
newRow = row + ixRow
newCol = col + colSpan - 1
newKey = '{}:{}:{}'.format(sheet, newRow, newCol)
'''
print 'Extend column {}{} ({}) to {}{} ({})'.format(
str(unichr(64 + col))
, row
, key
, str(unichr(64 + newCol))
, newRow
, newKey)
'''
if newKey not in dictCells :
dictCells[newKey] = {}
dictCells[newKey][borders_key] = top_left_borders
if rowSpan > 1 :
for ixCol in range(colSpan) :
newRow = row + rowSpan - 1
newCol = col + ixCol
newKey = '{}:{}:{}'.format(sheet, newRow, newCol)
'''
print 'Extend row {}{} ({}) to {}{} ({})'.format(
str(unichr(64 + col))
, row
, key
, str(unichr(64 + newCol))
, newRow
, newKey)
'''
if newKey not in dictCells :
dictCells[newKey] = {}
dictCells[newKey][borders_key] = top_left_borders
return dictCells
def getSheetSpannedCells(dictCells, page, sheet):
page.seek(0)
soup = BeautifulSoup(page)
table = soup.body.table
idxRow = 0
for row in table:
idxCell = 0
col = {}
foundInRow = False
for cell in row:
key = '{}:{}:{}'.format(sheet, idxRow, idxCell)
idxAttr = 1
cellSpec = {'c':'1', 'r':'1'}
found = False
for attr, value in cell.attrs:
if attr == "colspan":
cellSpec['c'] = value
found = True
if attr == "rowspan":
cellSpec['r'] = value
found = True
idxAttr += 1
if found:
if not key in dictCells :
dictCells[key] = {}
dictCells[key][spans_key] = '"{}", "{}"'.format(cellSpec['r'], cellSpec['c'])
else:
dictCells = extendEdgesOfSpan(key, dictCells, cellSpec)
idxCell += 1
idxRow += 1
return dictCells
def getSpreadsheetSpannedCells(dictCells, page, sheet):
return getSheetSpannedCells(dictCells, page, sheet)
result = ''
for row in spanned_cells:
line = spanned_cells[row]
for col in line:
cell = line[col]
result += '\n"{}", "{}", "{}", "span", "{}", "{}", "", "", "", ""'.format(sheet, row, col, cell['r'], cell['c'])
return result
if __name__ == "__main__":
result = getSpreadsheetSpannedCells("0Asxy-TdDgbjidG5ycTlRYTg0R0RyZzdJbE81MWtWcmc", "6")
print result
# Example : ./getSpans.py -k "0AhGdN..S.P.R.E.A.D.S.H.E.E.T...K.E.Y..c1TlE" -s 3 -u "[email protected]" -p "yourPassword" > spans.csv
# OR
# Example : ./getSpans.py -k "0AhGdN..S.P.R.E.A.D.S.H.E.E.T...K.E.Y..c1TlE" -s 3 -a "yourGoogleAuthKey" > spans.csv