Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (c) 2010-2020 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(object): 

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 @property 

59 def number_format(self): 

60 _id = self.style_array.numFmtId 

61 if _id < BUILTIN_FORMATS_MAX_SIZE: 

62 return BUILTIN_FORMATS.get(_id, "General") 

63 else: 

64 return self.parent.parent._number_formats[ 

65 _id - BUILTIN_FORMATS_MAX_SIZE] 

66 

67 @property 

68 def font(self): 

69 _id = self.style_array.fontId 

70 return self.parent.parent._fonts[_id] 

71 

72 @property 

73 def fill(self): 

74 _id = self.style_array.fillId 

75 return self.parent.parent._fills[_id] 

76 

77 @property 

78 def border(self): 

79 _id = self.style_array.borderId 

80 return self.parent.parent._borders[_id] 

81 

82 @property 

83 def alignment(self): 

84 _id = self.style_array.alignmentId 

85 return self.parent.parent._alignments[_id] 

86 

87 @property 

88 def protection(self): 

89 _id = self.style_array.protectionId 

90 return self.parent.parent._protections[_id] 

91 

92 

93 @property 

94 def is_date(self): 

95 return Cell.is_date.__get__(self) 

96 

97 

98 @property 

99 def internal_value(self): 

100 return self._value 

101 

102 @property 

103 def value(self): 

104 return self._value 

105 

106 @value.setter 

107 def value(self, value): 

108 if self._value is not None: 

109 raise AttributeError("Cell is read only") 

110 self._value = value 

111 

112 

113class EmptyCell(object): 

114 

115 __slots__ = () 

116 

117 value = None 

118 is_date = False 

119 font = None 

120 border = None 

121 fill = None 

122 number_format = None 

123 alignment = None 

124 data_type = 'n' 

125 

126 

127 def __repr__(self): 

128 return "<EmptyCell>" 

129 

130EMPTY_CELL = EmptyCell()