/
cell.py
259 lines (226 loc) · 8.36 KB
/
cell.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-.
"""
pygsheets.cell
~~~~~~~~~~~~~~
This module contains cell model
"""
# import warnings
from .custom_types import *
from .exceptions import (IncorrectCellLabel, CellNotFound, InvalidArgumentValue)
from .utils import format_addr
class Cell(object):
"""An instance of this class represents a single cell
in a :class:`worksheet <Worksheet>`.
"""
def __init__(self, pos, val='', worksheet=None):
self.worksheet = worksheet
if type(pos) == str:
pos = format_addr(pos, 'tuple')
self._row, self._col = pos
self._label = format_addr(pos, 'label')
self._value = val # formated vlaue
self._unformated_value = val # unformated vlaue
self._formula = ''
self._note = ''
self._color = (1.0, 1.0, 1.0, 1.0)
self._simplecell = True
self.format = (FormatType.CUSTOM, '')
"""tuple specifying data format (format type, pattern) or just format"""
self.parse_value = True
"""if set false, value will be shown as it is"""
self.text_format = {"foregroundColor": {}, "fontFamily": '', "fontSize": 10, "bold": False, "italic": False,
"strikethrough": False, "underline": False}
"""the text format as json"""
self.borders = {}
"""border properties as json"""
@property
def row(self):
"""Row number of the cell."""
return self._row
@row.setter
def row(self, row):
if self.worksheet:
ncell = self.worksheet.cell((row, self.col))
self.__dict__.update(ncell.__dict__)
else:
self._row = row
@property
def col(self):
"""Column number of the cell."""
return self._col
@col.setter
def col(self, col):
if self.worksheet:
ncell = self.worksheet.cell((self._row, col))
self.__dict__.update(ncell.__dict__)
else:
self._col = col
@property
def label(self):
"""Cell Label - Eg A1"""
return self._label
@label.setter
def label(self, label):
if self.worksheet:
ncell = self.worksheet.cell(label)
self.__dict__.update(ncell.__dict__)
else:
self._label = label
@property
def value(self):
"""formatted value of the cell"""
return self._value
@value.setter
def value(self, value):
if self.worksheet:
self.worksheet.update_cell(self.label, value, self.parse_value)
if not self._simplecell:
self.fetch()
else:
self._value = value
@property
def value_unformatted(self):
""" return unformatted value of the cell """
return self._unformated_value
@property
def formula(self):
"""formula if any of the cell"""
# self.fetch()
return self._formula
@formula.setter
def formula(self, formula):
if not formula.startswith('='):
formula = "=" + formula
tmp = self.parse_value
self.parse_value = True
self.value = formula
self._formula = formula
self.parse_value = tmp
self.fetch()
@property
def note(self):
"""note on the cell"""
return self._note
@note.setter
def note(self, note):
self._note = note
@property
def color(self):
"""background color of the cell as (red, green, blue, alpha)"""
return self._color
@color.setter
def color(self, value):
if type(value) is tuple:
if len(value) < 4:
value = list(value) + [1.0]*(4-len(value))
else:
value = (value, 1.0, 1.0, 1.0)
self._color = tuple(value)
def unlink(self):
"""unlink the cell from worksheet"""
self.worksheet = None
def neighbour(self, position):
"""
get a neighbouring cell of this cell
:param position: a tuple of relative position of position as string as
right, left, top, bottom or combinatoin
:return: Cell object of neighbouring cell
"""
if not self.worksheet:
return False
addr = [self.row, self.col]
if type(position) == tuple:
addr = (addr[0] + position[0], addr[1] + position[1])
elif type(position) == str:
if "right" in position:
addr[1] += 1
if "left" in position:
addr[1] -= 1
if "top" in position:
addr[0] -= 1
if "bottom" in position:
addr[0] += 1
try:
ncell = self.worksheet.cell(tuple(addr))
except IncorrectCellLabel:
raise CellNotFound
return ncell
def fetch(self):
""" Update the value of the cell from sheet """
self._simplecell = False
if self.worksheet:
self._value = self.worksheet.cell(self._label).value
result = self.worksheet.client.sh_get_ssheet(self.worksheet.spreadsheet.id, fields='sheets/data/rowData',
include_data=True,
ranges=self.worksheet._get_range(self.label))
result = result['sheets'][0]['data'][0]['rowData'][0]['values'][0]
self._value = result.get('formattedValue','')
try:
self._unformated_value = list(result['effectiveValue'].values())[0]
except KeyError:
self._unformated_value = ''
self._formula = result.get('userEnteredValue', {}).get('formulaValue', '')
self._note = result.get('note', '')
nformat = result.get('userEnteredFormat', {}).get('numberFormat', {})
self.format = (nformat.get('type', FormatType.CUSTOM), nformat.get('pattern', ''))
self.color = tuple(result.get('userEnteredFormat', {})
.get('backgroundColor', {'r':1.0,'g':1.0,'b':1.0,'a':1.0}).values())
self.text_format = result.get('userEnteredFormat', {}).get('textFormat', {})
self.borders = result.get('userEnteredFormat', {}).get('borders', {})
return self
else:
return False
def update(self):
"""update the sheet cell value with the attributes set """
self._simplecell = False
request = {
"repeatCell": {
"range": {
"sheetId": self.worksheet.id,
"startRowIndex": self.row - 1,
"endRowIndex": self.row,
"startColumnIndex": self.col - 1,
"endColumnIndex": self.col
},
"cell": self.get_json(),
"fields": "userEnteredFormat, note"
}
}
self.worksheet.client.sh_batch_update(self.worksheet.spreadsheet.id, request, None, False)
self.value = self._value # @TODO combine to above?
def get_json(self):
"""get the json representation of the cell as per google api"""
try:
nformat, pattern = self.format
except ValueError:
nformat, pattern = self.format, ""
json_repr = {
"userEnteredFormat": {
"numberFormat": {
"type": nformat.value,
"pattern": pattern
},
"backgroundColor": {
"red": self._color[0],
"green": self._color[1],
"blue": self._color[2],
"alpha": self._color[3],
},
"textFormat": self.text_format,
"borders": self.borders
},
"note": self._note,
}
return json_repr
def __eq__(self, other):
if self.worksheet is not None and other.worksheet is not None:
if self.worksheet != other.worksheet:
return False
if self.label != other.label:
return False
return True
def __repr__(self):
return '<%s R%sC%s %s>' % (self.__class__.__name__,
self.row,
self.col,
repr(self.value))