/usr/share/pyshared/openpyxl/tests/test_named_range.py is in python-openpyxl 1.7.0+ds1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | # file openpyxl/tests/test_named_range.py
# Copyright (c) 2010-2011 openpyxl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# @license: http://www.opensource.org/licenses/mit-license.php
# @author: see AUTHORS file
# Python stdlib imports
import os.path
# 3rd-party imports
from nose.tools import eq_, assert_raises, ok_
# package imports
from openpyxl.tests.helper import DATADIR, TMPDIR, clean_tmpdir, make_tmpdir
from openpyxl.namedrange import split_named_range, NamedRange
from openpyxl.reader.workbook import read_named_ranges
from openpyxl.shared.exc import NamedRangeException
from openpyxl.reader.excel import load_workbook
from openpyxl.writer.workbook import write_workbook
from openpyxl.workbook import Workbook
def test_split():
eq_([('My Sheet', '$D$8'), ], split_named_range("'My Sheet'!$D$8"))
def test_split_no_quotes():
eq_([('HYPOTHESES', '$B$3:$L$3'), ], split_named_range('HYPOTHESES!$B$3:$L$3'))
def test_bad_range_name():
assert_raises(NamedRangeException, split_named_range, 'HYPOTHESES$B$3')
def test_range_name_worksheet_special_chars():
class DummyWs(object):
title = 'My Sheeet with a , and \''
def __str__(self):
return self.title
ws = DummyWs()
class DummyWB(object):
def get_sheet_by_name(self, name):
if name == ws.title:
return ws
handle = open(os.path.join(DATADIR, 'reader', 'workbook_namedrange.xml'))
try:
content = handle.read()
named_ranges = read_named_ranges(content, DummyWB())
eq_(1, len(named_ranges))
ok_(isinstance(named_ranges[0], NamedRange))
eq_([(ws, '$U$16:$U$24'), (ws, '$V$28:$V$36')], named_ranges[0].destinations)
finally:
handle.close()
def test_read_named_ranges():
class DummyWs(object):
title = 'My Sheeet'
def __str__(self):
return self.title
class DummyWB(object):
def get_sheet_by_name(self, name):
return DummyWs()
handle = open(os.path.join(DATADIR, 'reader', 'workbook.xml'))
try:
content = handle.read()
named_ranges = read_named_ranges(content, DummyWB())
eq_(["My Sheeet!$D$8"], [str(range) for range in named_ranges])
finally:
handle.close()
def test_oddly_shaped_named_ranges():
ranges_counts = ((4, 'TEST_RANGE'),
(3, 'TRAP_1'),
(13, 'TRAP_2'))
def check_ranges(ws, count, range_name):
eq_(count, len(ws.range(range_name)))
wb = load_workbook(os.path.join(DATADIR, 'genuine', 'merge_range.xlsx'),
use_iterators = False)
ws = wb.worksheets[0]
for count, range_name in ranges_counts:
yield check_ranges, ws, count, range_name
def test_merged_cells_named_range():
wb = load_workbook(os.path.join(DATADIR, 'genuine', 'merge_range.xlsx'),
use_iterators = False)
ws = wb.worksheets[0]
cell = ws.range('TRAP_3')
eq_('B15', cell.get_coordinate())
eq_(10, cell.value)
def test_print_titles():
wb = Workbook()
ws1 = wb.create_sheet()
ws2 = wb.create_sheet()
ws1.add_print_title(2)
ws2.add_print_title(3, rows_or_cols='cols')
def mystr(nr):
return ','.join(['%s!%s' % (sheet.title, name) for sheet, name in nr.destinations])
actual_named_ranges = set([(nr.name, nr.scope, mystr(nr)) for nr in wb.get_named_ranges()])
expected_named_ranges = set([('_xlnm.Print_Titles', ws1, 'Sheet1!$1:$2'),
('_xlnm.Print_Titles', ws2, 'Sheet2!$A:$C')])
assert(actual_named_ranges == expected_named_ranges)
class TestNameRefersToValue(object):
def setup(self):
self.wb = load_workbook(os.path.join(DATADIR, 'genuine', 'NameWithValueBug.xlsx'))
self.ws = self.wb.get_sheet_by_name("Sheet1")
make_tmpdir()
def tearDown(self):
clean_tmpdir()
def test_has_ranges(self):
ranges = self.wb.get_named_ranges()
eq_(['MyRef', 'MySheetRef', 'MySheetRef', 'MySheetValue', 'MySheetValue', 'MyValue'], [range.name for range in ranges])
def test_workbook_has_normal_range(self):
normal_range = self.wb.get_named_range("MyRef")
eq_("MyRef", normal_range.name)
def test_workbook_has_value_range(self):
value_range = self.wb.get_named_range("MyValue")
eq_("MyValue", value_range.name)
eq_("9.99", value_range.value)
def test_worksheet_range(self):
range = self.ws.range("MyRef")
def test_worksheet_range_error_on_value_range(self):
assert_raises(NamedRangeException, self.ws.range, "MyValue")
def range_as_string(self, range, include_value=False):
def scope_as_string(range):
if range.scope:
return range.scope.title
else:
return "Workbook"
retval = "%s: %s" % (range.name, scope_as_string(range))
if include_value:
if isinstance(range, NamedRange):
retval += "=[range]"
else:
retval += "=" + range.value
return retval
def test_handles_scope(self):
ranges = self.wb.get_named_ranges()
eq_(['MyRef: Workbook', 'MySheetRef: Sheet1', 'MySheetRef: Sheet2', 'MySheetValue: Sheet1', 'MySheetValue: Sheet2', 'MyValue: Workbook'],
[self.range_as_string(range) for range in ranges])
def test_can_be_saved(self):
FNAME = os.path.join(TMPDIR, "foo.xlsx")
self.wb.save(FNAME)
wbcopy = load_workbook(FNAME)
eq_(['MyRef: Workbook=[range]', 'MySheetRef: Sheet1=[range]', 'MySheetRef: Sheet2=[range]', 'MySheetValue: Sheet1=3.33', 'MySheetValue: Sheet2=14.4', 'MyValue: Workbook=9.99'],
[self.range_as_string(range, include_value=True) for range in wbcopy.get_named_ranges()])
|