Source code

Download source archive ↓

src/openpyxl/cell/read_only.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.cell import Cell
4from openpyxl.utils import get_column_letter
5from openpyxl.utils.datetime import from_excel
6from openpyxl.styles import is_date_format
7from openpyxl.styles.numbers import BUILTIN_FORMATS, BUILTIN_FORMATS_MAX_SIZE
8
9
10class ReadOnlyCell:
11
12    __slots__ =  ('parent', 'row', 'column', '_value', 'data_type', '_style_id')
13
14    def __init__(self, sheet, row, column, value, data_type='n', style_id=0):
15        self.parent = sheet
16        self._value = None
17        self.row = row
18        self.column = column
19        self.data_type = data_type
20        self.value = value
21        self._style_id = style_id
22
23
24    def __eq__(self, other):
25        for a in self.__slots__:
26            if getattr(self, a) != getattr(other, a):
27                return
28        return True
29
30    def __ne__(self, other):
31        return not self.__eq__(other)
32
33
34    def __repr__(self):
35        return "<ReadOnlyCell {0!r}.{1}>".format(self.parent.title, self.coordinate)
36
37
38    @property
39    def coordinate(self):
40        column = get_column_letter(self.column)
41        return "{1}{0}".format(self.row, column)
42
43
44    @property
45    def coordinate(self):
46        return Cell.coordinate.__get__(self)
47
48
49    @property
50    def column_letter(self):
51        return Cell.column_letter.__get__(self)
52
53
54    @property
55    def style_array(self):
56        return self.parent.parent._cell_styles[self._style_id]
57
58
59    @property
60    def has_style(self):
61        return self._style_id != 0
62
63
64    @property
65    def number_format(self):
66        _id = self.style_array.numFmtId
67        if _id < BUILTIN_FORMATS_MAX_SIZE:
68            return BUILTIN_FORMATS.get(_id, "General")
69        else:
70            return self.parent.parent._number_formats[
71                _id - BUILTIN_FORMATS_MAX_SIZE]
72
73    @property
74    def font(self):
75        _id = self.style_array.fontId
76        return self.parent.parent._fonts[_id]
77
78    @property
79    def fill(self):
80        _id = self.style_array.fillId
81        return self.parent.parent._fills[_id]
82
83    @property
84    def border(self):
85        _id = self.style_array.borderId
86        return self.parent.parent._borders[_id]
87
88    @property
89    def alignment(self):
90        _id = self.style_array.alignmentId
91        return self.parent.parent._alignments[_id]
92
93    @property
94    def protection(self):
95        _id = self.style_array.protectionId
96        return self.parent.parent._protections[_id]
97
98
99    @property
100    def is_date(self):
101        return Cell.is_date.__get__(self)
102
103
104    @property
105    def internal_value(self):
106        return self._value
107
108    @property
109    def value(self):
110        return self._value
111
112    @value.setter
113    def value(self, value):
114        if self._value is not None:
115            raise AttributeError("Cell is read only")
116        self._value = value
117
118
119class EmptyCell:
120
121    __slots__ = ()
122
123    value = None
124    is_date = False
125    font = None
126    border = None
127    fill = None
128    number_format = None
129    alignment = None
130    data_type = 'n'
131
132
133    def __repr__(self):
134        return "<EmptyCell>"
135
136EMPTY_CELL = EmptyCell()