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_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_resolve_range():
a = AddressRange.create
assert ((a('B1'), ), ) == a('B1').resolve_range
assert ((a('B1'), a('C1')),) == a('B1:C1').resolve_range
assert ((a('B1'),), (a('B2'), )) == a('B1:B2').resolve_range
assert ((a('B1'), a('C1')), (a('B2'), a('C2'))) == a('B1:C2').resolve_range
assert ((a('sh!B1'),),) == a('sh!B1').resolve_range
assert ((a('sh!B1'), a('sh!C1')),) == a('sh!B1:C1').resolve_range
assert ((a('sh!B1'),), (a('sh!B2'),)) == a('sh!B1:B2').resolve_range
assert ((a('sh!B1'), a('sh!C1')),
(a('sh!B2'), a('sh!C2'))) == (a('sh!B1:C2')).resolve_range
assert ((a('sh!B1'),),) == a('sh!B1', sheet='sh').resolve_range
assert ((a('sh!B1'), a('sh!C1')),) == (
a('sh!B1:C1', sheet='sh')).resolve_range
assert ((a('sh!B1'),), (a('sh!B2'),)) == (
def test_address_absolute(address, expected):
assert AddressRange.create(address).abs_address == expected
def test_row(address, expected):
try:
address = AddressRange.create(address)
except ValueError:
pass
result = row(address)
if expected is None:
assert 1 == next(iter(result))
else:
assert expected == result
def _emit(self, value=None):
# resolve the range into cells
sheet = self.cell and self.cell.sheet or ''
value = value is not None and value or self.value
if '!' in value:
sheet = ''
try:
addr_str = value.replace('$', '')
address = AddressRange.create(addr_str, sheet=sheet, cell=self.cell)
except ValueError:
# check for table relative address
table_name = None
if self.cell:
excel = self.cell.excel
if excel and '[' in addr_str:
table_name = excel.table_name_containing(self.cell.address)
if not table_name:
logging.getLogger('pycel').warning(
'Table Name not found: {}'.format(addr_str))
return '"{}"'.format(NAME_ERROR)
addr_str = '{}{}'.format(table_name, addr_str)
address = AddressRange.create(
addr_str, sheet=self.cell.address.sheet, cell=self.cell)
address = AddressRange.create(addr_str, sheet=sheet, cell=self.cell)
except ValueError:
# check for table relative address
table_name = None
if self.cell:
excel = self.cell.excel
if excel and '[' in addr_str:
table_name = excel.table_name_containing(self.cell.address)
if not table_name:
logging.getLogger('pycel').warning(
'Table Name not found: {}'.format(addr_str))
return '"{}"'.format(NAME_ERROR)
addr_str = '{}{}'.format(table_name, addr_str)
address = AddressRange.create(
addr_str, sheet=self.cell.address.sheet, cell=self.cell)
if isinstance(address, AddressMultiAreaRange):
return ', '.join(self._emit(value=str(addr)) for addr in address)
else:
template = '_R_("{}")' if address.is_range else '_C_("{}")'
return template.format(address)
""" evaluate a cell or cells in the spreadsheet
:param address: str, AddressRange, AddressCell or a tuple or list
or iterable of these three
:return: evaluated value/values
"""
if str(address) not in self.cell_map:
if list_like(address):
if not isinstance(address, (tuple, list)):
address = tuple(address)
# process a tuple or list of addresses
return type(address)(
self._evaluate_non_iterative(c) for c in address)
address = AddressRange.create(address)
# get the sheet if not specified
if not address.has_sheet:
address = AddressRange(
address, sheet=self.excel.get_active_sheet_name())
if address.address not in self.cell_map:
self._gen_graph(address)
result = self._evaluate(str(address))
if isinstance(result, tuple):
# trim excess dimensions
if len(result[0]) == 1:
result = tuple(row[0] for row in result)
if len(result) == 1:
result = result[0]
NOTE: conditional_formats are not saved in the persistent formats.
If needed they can be hand serialized into "extra_data"
:param address: str, AddressRange, AddressCell or a tuple or list
or iterable of these three
:return: evaluated objects ids
"""
if list_like(address):
if not isinstance(address, (tuple, list)):
address = tuple(address)
# process a tuple or list of addresses
return type(address)(
self.eval_conditional_formats(c) for c in address)
address = AddressRange.create(address)
# get the sheet if not specified
if not address.has_sheet:
address = AddressRange(
address, sheet=self.excel.get_active_sheet_name())
if address.is_range:
return tuple(tuple(self.eval_conditional_formats(addr)
for addr in row) for row in address.rows)
cf_addr = str(address).replace('!', '.cf!')
if cf_addr not in self.cell_map:
phony_cell = _Cell(address)
formats = self.excel.conditional_format(address)
format_strs = []
def indirect(ref_text, a1=True, sheet=''):
# Excel reference: https://support.office.com/en-us/article/
# indirect-function-474b3a3a-8a26-4f44-b491-92b6306fa261
try:
address = AddressRange.create(ref_text)
except ValueError:
return REF_ERROR
if address.row > MAX_ROW or address.col_idx > MAX_COL:
return REF_ERROR
if not address.has_sheet:
address = AddressRange.create(address, sheet=sheet)
return address
def indirect(ref_text, a1=True, sheet=''):
# Excel reference: https://support.office.com/en-us/article/
# indirect-function-474b3a3a-8a26-4f44-b491-92b6306fa261
try:
address = AddressRange.create(ref_text)
except ValueError:
return REF_ERROR
if address.row > MAX_ROW or address.col_idx > MAX_COL:
return REF_ERROR
if not address.has_sheet:
address = AddressRange.create(address, sheet=sheet)
return address