2018-01-22 22:38:48 +08:00
|
|
|
// Copyright (c) 2014-2018 Thomas Fussell
|
2017-04-14 07:01:30 +08:00
|
|
|
//
|
|
|
|
// 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, WRISING 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
|
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2017-04-14 07:24:20 +08:00
|
|
|
#include <helpers/test_suite.hpp>
|
2015-10-14 12:03:48 +08:00
|
|
|
#include <xlnt/workbook/named_range.hpp>
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2017-04-14 07:01:30 +08:00
|
|
|
class named_range_test_suite : public test_suite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void test_split()
|
|
|
|
{
|
2016-12-04 20:29:10 +08:00
|
|
|
/*
|
2015-11-11 07:58:54 +08:00
|
|
|
using string_pair = std::pair<std::string, std::string>;
|
2015-10-14 12:03:48 +08:00
|
|
|
using string_pair_vector = std::vector<string_pair>;
|
2015-11-11 07:58:54 +08:00
|
|
|
using expected_pair = std::pair<std::string, string_pair_vector>;
|
2015-10-14 12:03:48 +08:00
|
|
|
|
|
|
|
std::vector<expected_pair> expected_pairs =
|
|
|
|
{
|
|
|
|
{ "'My Sheet'!$D$8", {{ "My Sheet", "$D$8" }} },
|
|
|
|
{ "Sheet1!$A$1", {{ "Sheet1", "$A$1" }} },
|
|
|
|
{ "[1]Sheet1!$A$1", {{ "[1]Sheet1", "$A$1" }} },
|
|
|
|
{ "[1]!B2range", {{ "[1]", "" }} },
|
|
|
|
{ "Sheet1!$C$5:$C$7,Sheet1!$C$9:$C$11,Sheet1!$E$5:$E$7,Sheet1!$E$9:$E$11,Sheet1!$D$8",
|
|
|
|
{
|
|
|
|
{ "Sheet1", "$C$5:$C$7" },
|
|
|
|
{ "Sheet1", "$C$9:$C$11" },
|
|
|
|
{ "Sheet1", "$E$5:$E$7" },
|
|
|
|
{ "Sheet1", "$E$9:$E$11" },
|
|
|
|
{ "Sheet1", "$D$8" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto pair : expected_pairs)
|
|
|
|
{
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(xlnt::split_named_range(pair.first), pair.second);
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
2016-12-04 20:29:10 +08:00
|
|
|
*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_split_no_quotes()
|
|
|
|
{
|
2017-04-19 06:30:54 +08:00
|
|
|
/*xlnt_assert_equals([("HYPOTHESES", "$B$3:$L$3"), ], split_named_range("HYPOTHESES!$B$3:$L$3"))*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_bad_range_name()
|
|
|
|
{
|
2017-04-19 06:30:54 +08:00
|
|
|
/*xlnt_assert_raises(NamedRangeException, split_named_range, "HYPOTHESES$B$3")*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_range_name_worksheet_special_chars()
|
|
|
|
{
|
|
|
|
/*class DummyWs
|
|
|
|
{
|
|
|
|
title = "My Sheeet with a , and \""
|
|
|
|
|
|
|
|
void __str__()
|
|
|
|
{
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
|
|
|
|
ws = DummyWs()
|
|
|
|
};
|
|
|
|
|
|
|
|
class DummyWB
|
|
|
|
{
|
|
|
|
void 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())
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(1, len(named_ranges))
|
2014-05-09 03:32:12 +08:00
|
|
|
ok_(isinstance(named_ranges[0], NamedRange))
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals([(ws, "$U$16:$U$24"), (ws, "$V$28:$V$36")], named_ranges[0].destinations)
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
handle.close()
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test_read_named_ranges()
|
|
|
|
{
|
|
|
|
/*class DummyWs
|
|
|
|
{
|
|
|
|
title = "My Sheeet"
|
|
|
|
|
|
|
|
void __str__()
|
|
|
|
{
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DummyWB
|
|
|
|
{
|
|
|
|
void 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())
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(["My Sheeet!$D$8"], [str(range) for range in named_ranges])
|
2014-05-09 03:32:12 +08:00
|
|
|
finally :
|
|
|
|
handle.close()*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_oddly_shaped_named_ranges()
|
|
|
|
{
|
|
|
|
/* ranges_counts = ((4, "TEST_RANGE"),
|
|
|
|
(3, "TRAP_1"),
|
|
|
|
(13, "TRAP_2"))*/
|
|
|
|
}
|
|
|
|
|
|
|
|
//void check_ranges(ws, count, range_name)
|
|
|
|
//{
|
2017-04-19 06:30:54 +08:00
|
|
|
// /*xlnt_assert_equals(count, len(ws.range(range_name)))
|
2014-05-09 03:32:12 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// }*/
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
void 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")
|
|
|
|
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals("B15", cell.get_coordinate())
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(10, cell.value)*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
/*wb = load_workbook(os.path.join(DATADIR, "genuine", "NameWithValueBug.xlsx"))
|
|
|
|
ws = wb.get_sheet_by_name("Sheet1")
|
|
|
|
make_tmpdir()*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
/* clean_tmpdir()*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_has_ranges()
|
|
|
|
{
|
|
|
|
/*ranges = wb.get_named_ranges()
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(["MyRef", "MySheetRef", "MySheetRef", "MySheetValue", "MySheetValue", "MyValue"], [range.name for range in ranges])*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_workbook_has_normal_range()
|
|
|
|
{
|
|
|
|
/*normal_range = wb.get_named_range("MyRef")
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals("MyRef", normal_range.name)*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_workbook_has_value_range()
|
|
|
|
{
|
|
|
|
/*value_range = wb.get_named_range("MyValue")
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals("MyValue", value_range.name)
|
|
|
|
xlnt_assert_equals("9.99", value_range.value)*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_worksheet_range()
|
|
|
|
{
|
|
|
|
/*range = ws.range("MyRef")*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_worksheet_range_error_on_value_range()
|
|
|
|
{
|
2017-04-19 06:30:54 +08:00
|
|
|
/*xlnt_assert_raises(NamedRangeException, ws.range, "MyValue")*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//void range_as_string(self, range, include_value = False)
|
|
|
|
//{
|
|
|
|
// /*void 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*/
|
|
|
|
//}
|
|
|
|
|
|
|
|
void test_handles_scope()
|
|
|
|
{
|
|
|
|
/*ranges = wb.get_named_ranges()
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(["MyRef: Workbook", "MySheetRef: Sheet1", "MySheetRef: Sheet2", "MySheetValue: Sheet1", "MySheetValue: Sheet2", "MyValue: Workbook"],
|
2014-05-09 03:32:12 +08:00
|
|
|
[range_as_string(range) for range in ranges])*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_can_be_saved()
|
|
|
|
{
|
|
|
|
/*FNAME = os.path.join(TMPDIR, "foo.xlsx")
|
|
|
|
wb.save(FNAME)
|
|
|
|
|
|
|
|
wbcopy = load_workbook(FNAME)
|
2017-04-19 06:30:54 +08:00
|
|
|
xlnt_assert_equals(["MyRef: Workbook=[range]", "MySheetRef: Sheet1=[range]", "MySheetRef: Sheet2=[range]", "MySheetValue: Sheet1=3.33", "MySheetValue: Sheet2=14.4", "MyValue: Workbook=9.99"],
|
2014-05-09 03:32:12 +08:00
|
|
|
[range_as_string(range, include_value = True) for range in wbcopy.get_named_ranges()])*/
|
|
|
|
}
|
|
|
|
};
|
2018-07-05 16:47:24 +08:00
|
|
|
static named_range_test_suite x;
|