Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_address_cell_addr_inc():
cell_addr = AddressCell('sh!C2')
assert MAX_COL - 1 == cell_addr.inc_col(-4)
assert MAX_COL == cell_addr.inc_col(-3)
assert 1 == cell_addr.inc_col(-2)
assert 5 == cell_addr.inc_col(2)
assert 6 == cell_addr.inc_col(3)
assert MAX_ROW - 1 == cell_addr.inc_row(-3)
assert MAX_ROW == cell_addr.inc_row(-2)
assert 1 == cell_addr.inc_row(-1)
assert 5 == cell_addr.inc_row(3)
assert 6 == cell_addr.inc_row(4)
def test_address_pickle(tmpdir):
addrs = [
AddressRange('B1'),
AddressRange('B1:C1'),
AddressRange('B1:B2'),
AddressRange('B1:C2'),
AddressRange('sh!B1'),
AddressRange('sh!B1:C1'),
AddressRange('sh!B1:B2'),
AddressRange('sh!B1:C2'),
AddressRange('B:C'),
AddressRange('2:4'),
AddressCell('sh!XFC1048575'),
AddressCell('sh!XFD1048576'),
AddressCell('sh!A1'),
AddressCell('sh!E5'),
AddressCell('sh!F6'),
]
filename = os.path.join(str(tmpdir), 'test_addrs.pkl')
with open(filename, 'wb') as f:
pickle.dump(addrs, f)
with open(filename, 'rb') as f:
new_addrs = pickle.load(f)
assert addrs == new_addrs
def test_offset(crwh, refer, rows, cols, height, width):
expected = crwh
if isinstance(crwh, tuple):
start = AddressCell((crwh[0], crwh[1], crwh[0], crwh[1]))
end = AddressCell((crwh[0] + crwh[2] - 1, crwh[1] + crwh[3] - 1,
crwh[0] + crwh[2] - 1, crwh[1] + crwh[3] - 1))
expected = AddressRange.create(
'{0}:{1}'.format(start.coordinate, end.coordinate))
result = offset(refer, rows, cols, height, width)
assert result == expected
refer_addr = AddressRange.create(refer)
if height == refer_addr.size.height:
height = None
if width == refer_addr.size.width:
width = None
assert offset(refer_addr, rows, cols, height, width) == expected
def test_quoted_address(sheet_name):
addr = AddressCell('A2', sheet=sheet_name)
assert addr.quoted_address == '{}!A2'.format(addr.quote_sheet(sheet_name))
("R1C1", AddressCell("A1")),
("ab", REF_ERROR),
)
)
def test_indirect(address, expected):
assert indirect(address) == expected
if is_address(expected):
with_sheet = expected.create(expected, sheet='S')
assert indirect(address, None, 'S') == with_sheet
address = 'S!{}'.format(address)
assert indirect(address) == with_sheet
assert indirect(address, None, 'S') == with_sheet
def test_needed_addresses():
formula = '=(3600/1000)*E40*(E8/E39)*(E15/E19)*LN(E54/(E54-E48))'
needed = sorted(('E40', 'E8', 'E39', 'E15', 'E19', 'E54', 'E48'))
excel_formula = ExcelFormula(formula)
assert needed == sorted(x.address for x in excel_formula.needed_addresses)
assert needed == sorted(x.address for x in excel_formula.needed_addresses)
assert () == ExcelFormula('').needed_addresses
excel_formula = ExcelFormula('_REF_(_R_("S!A1"))',
formula_is_python_code=True)
assert excel_formula.needed_addresses == (AddressCell('S!A1'), )
def test_address_range():
a = AddressRange('a1:b2')
b = AddressRange('A1:B2')
c = AddressRange(a)
assert a == b
assert b == c
assert b == AddressRange(b)
assert b == AddressRange.create(b)
assert AddressRange('sh!a1:b2') == AddressRange(a, sheet='sh')
assert AddressCell('C13') == AddressCell('R13C3')
with pytest.raises(ValueError):
AddressRange(AddressRange('sh!a1:b2'), sheet='sheet')
a = AddressRange('A:A')
assert 'A' == a.start.column
assert 'A' == a.end.column
assert 0 == a.start.row
assert 0 == a.end.row
b = AddressRange('1:1')
assert '' == b.start.column
assert '' == b.end.column
assert 1 == b.start.row
assert 1 == b.end.row
def test_address_cell_addr_offset():
cell_addr = AddressCell('sh!C2')
assert AddressCell('sh!XFC1048575') == cell_addr.address_at_offset(-3, -4)
assert AddressCell('sh!XFD1048576') == cell_addr.address_at_offset(-2, -3)
assert AddressCell('sh!A1') == cell_addr.address_at_offset(-1, -2)
assert AddressCell('sh!E5') == cell_addr.address_at_offset(3, 2)
assert AddressCell('sh!F6') == cell_addr.address_at_offset(4, 3)
width = base_addr.size.width
new_row = base_addr.row + row_inc
end_row = new_row + height - 1
new_col = base_addr.col_idx + col_inc
end_col = new_col + width - 1
if new_row <= 0 or end_row > MAX_ROW or new_col <= 0 or end_col > MAX_COL:
return REF_ERROR
top_left = AddressCell((new_col, new_row, new_col, new_row),
sheet=base_addr.sheet)
if height == width == 1:
return top_left
else:
bottom_right = AddressCell((end_col, end_row, end_col, end_row),
sheet=base_addr.sheet)
return AddressRange('{}:{}'.format(
top_left.coordinate, bottom_right.coordinate),
sheet=top_left.sheet)
def get_formula_from_range(self, address):
if not isinstance(address, (AddressRange, AddressCell)):
address = AddressRange(address)
result = self.get_range(address)
if isinstance(address, AddressCell):
return result.formula if result.formula.startswith("=") else None
else:
return tuple(tuple(
self.get_formula_from_range(a) for a in row
) for row in result.resolve_range)