def __init__(self, formula, origin): # Excel errors out when a workbook has formulae in R1C1 notation, # regardless of the calcPr:refMode setting, so I'm assuming the # formulae stored in the workbook must be in A1 notation. col, self.row = coordinate_from_string(origin) self.col = column_index_from_string(col) self.tokenizer = Tokenizer(formula)
def translate_formula(self, dest): """ Convert the formula into A1 notation. The formula is converted into A1 assuming it is assigned to the cell whose address is `dest` (no worksheet name). """ tokens = self.get_tokens() if not tokens: return "" elif tokens[0].type == Token.LITERAL: return tokens[0].value out = ['='] # per the spec: # A compliant producer or consumer considers a defined name in the # range A1-XFD1048576 to be an error. All other names outside this # range can be defined as names and overrides a cell reference if an # ambiguity exists. (I.18.2.5) dcol, drow = coordinate_from_string(dest) dcol = column_index_from_string(dcol) row_delta = drow - self.row col_delta = dcol - self.col for token in tokens: if token.type == Token.OPERAND and token.subtype == Token.RANGE: out.append(self.translate_range(token.value, row_delta, col_delta)) else: out.append(token.value) return "".join(out)
def add_comment_shape(self, root, idx, coord): col, row = coordinate_from_string(coord) row -= 1 column = column_index_from_string(col) - 1 shape = _shape_factory(row, column) shape.set('id', "_x0000_s%04d" % idx) root.append(shape)
def test_invalid_coordinate(value): from openpyxl25.utils.exceptions import CellCoordinatesException with pytest.raises(CellCoordinatesException): coordinate_from_string(value)
def test_coordinates(): assert coordinate_from_string('ZF46') == ("ZF", 46)