2014-05-09 03:32:12 +08:00
# pragma once
# include <iostream>
# include <cxxtest/TestSuite.h>
2014-05-14 02:40:28 +08:00
# include "pugixml.hpp"
2014-06-06 04:19:31 +08:00
# include <xlnt/xlnt.hpp>
2014-05-09 03:32:12 +08:00
2014-06-06 04:19:31 +08:00
class test_worksheet : public CxxTest : : TestSuite
2014-05-09 03:32:12 +08:00
{
public :
void test_new_worksheet ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws = wb_ . create_sheet ( ) ;
2014-07-27 04:19:15 +08:00
TS_ASSERT ( wb_ = = ws . get_parent ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_new_sheet_name ( )
{
2014-07-27 04:19:15 +08:00
xlnt : : worksheet ws = wb_ . create_sheet ( " TestName " ) ;
2014-06-16 00:16:34 +08:00
TS_ASSERT_EQUALS ( ws . to_string ( ) , " <Worksheet \" TestName \" > " ) ;
2014-05-09 03:32:12 +08:00
}
void test_get_cell ( )
{
2014-07-27 04:19:15 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-21 22:20:30 +08:00
auto cell = ws . get_cell ( " A1 " ) ;
TS_ASSERT_EQUALS ( cell . get_reference ( ) . to_string ( ) , " A1 " ) ;
2014-05-09 03:32:12 +08:00
}
void test_set_bad_title ( )
{
2014-05-13 07:59:33 +08:00
std : : string title ( 50 , ' X ' ) ;
2014-07-27 04:19:15 +08:00
TS_ASSERT_THROWS ( wb_ . create_sheet ( title ) , xlnt : : sheet_title_exception ) ;
2014-07-19 04:20:41 +08:00
}
void test_increment_title ( )
{
auto ws1 = wb_ . create_sheet ( " Test " ) ;
TS_ASSERT_EQUALS ( ws1 . get_title ( ) , " Test " ) ;
auto ws2 = wb_ . create_sheet ( " Test " ) ;
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS ( ws2 . get_title ( ) , " Test1 " ) ;
2014-05-09 03:32:12 +08:00
}
void test_set_bad_title_character ( )
{
2014-06-16 00:16:34 +08:00
TS_ASSERT_THROWS ( wb_ . create_sheet ( " [ " ) , xlnt : : sheet_title_exception ) ;
TS_ASSERT_THROWS ( wb_ . create_sheet ( " ] " ) , xlnt : : sheet_title_exception ) ;
TS_ASSERT_THROWS ( wb_ . create_sheet ( " * " ) , xlnt : : sheet_title_exception ) ;
TS_ASSERT_THROWS ( wb_ . create_sheet ( " : " ) , xlnt : : sheet_title_exception ) ;
TS_ASSERT_THROWS ( wb_ . create_sheet ( " ? " ) , xlnt : : sheet_title_exception ) ;
TS_ASSERT_THROWS ( wb_ . create_sheet ( " / " ) , xlnt : : sheet_title_exception ) ;
TS_ASSERT_THROWS ( wb_ . create_sheet ( " \\ " ) , xlnt : : sheet_title_exception ) ;
2014-05-09 03:32:12 +08:00
}
2014-07-27 04:19:15 +08:00
void test_unique_sheet_title ( )
{
2014-08-02 04:46:54 +08:00
auto ws = wb_ . create_sheet ( " AGE " ) ;
TS_ASSERT_EQUALS ( ws . unique_sheet_name ( " GE " ) , " GE " ) ;
2014-07-27 04:19:15 +08:00
}
2014-05-09 03:32:12 +08:00
void test_worksheet_dimension ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-07-26 04:39:25 +08:00
ws . get_cell ( " A1 " ) . set_value ( " AAA " ) ;
2014-06-11 05:12:15 +08:00
TS_ASSERT_EQUALS ( " A1:A1 " , ws . calculate_dimension ( ) . to_string ( ) ) ;
2014-07-26 04:39:25 +08:00
ws . get_cell ( " B12 " ) . set_value ( " AAA " ) ;
2014-05-21 22:20:30 +08:00
TS_ASSERT_EQUALS ( " A1:B12 " , ws . calculate_dimension ( ) . to_string ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_worksheet_range ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-21 22:20:30 +08:00
auto xlrange = ws . get_range ( " A1:C4 " ) ;
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS ( 4 , xlrange . length ( ) ) ;
2014-05-22 05:48:51 +08:00
TS_ASSERT_EQUALS ( 3 , xlrange [ 0 ] . num_cells ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_worksheet_named_range ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
wb_ . create_named_range ( " test_range " , ws , " C5 " ) ;
2014-05-21 22:20:30 +08:00
auto xlrange = ws . get_named_range ( " test_range " ) ;
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS ( 1 , xlrange . length ( ) ) ;
2014-05-22 05:48:51 +08:00
TS_ASSERT_EQUALS ( 1 , xlrange [ 0 ] . num_cells ( ) ) ;
2014-05-14 02:40:28 +08:00
TS_ASSERT_EQUALS ( 5 , xlrange [ 0 ] [ 0 ] . get_row ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_bad_named_range ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-07-24 04:00:09 +08:00
TS_ASSERT_THROWS ( ws . get_named_range ( " bad_range " ) , xlnt : : named_range_exception ) ;
2014-05-09 03:32:12 +08:00
}
void test_named_range_wrong_sheet ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws1 ( wb_ ) ;
xlnt : : worksheet ws2 ( wb_ ) ;
wb_ . create_named_range ( " wrong_sheet_range " , ws1 , " C5 " ) ;
2014-07-19 04:20:41 +08:00
TS_ASSERT_THROWS ( ws2 . get_named_range ( " wrong_sheet_range " ) , xlnt : : named_range_exception ) ;
2014-05-09 03:32:12 +08:00
}
void test_cell_offset ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-30 08:52:14 +08:00
TS_ASSERT_EQUALS ( " C17 " , ws . get_cell ( xlnt : : cell_reference ( " B15 " ) . make_offset ( 1 , 2 ) ) . get_reference ( ) . to_string ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_range_offset ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-21 22:20:30 +08:00
auto xlrange = ws . get_range ( xlnt : : range_reference ( " A1:C4 " ) . make_offset ( 3 , 1 ) ) ;
2014-07-20 04:59:05 +08:00
TS_ASSERT_EQUALS ( 4 , xlrange . length ( ) ) ;
2014-05-22 05:48:51 +08:00
TS_ASSERT_EQUALS ( 3 , xlrange [ 0 ] . num_cells ( ) ) ;
2014-05-21 22:20:30 +08:00
TS_ASSERT_EQUALS ( " D2 " , xlrange [ 0 ] [ 0 ] . get_reference ( ) . to_string ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_cell_alternate_coordinates ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-21 22:20:30 +08:00
auto cell = ws . get_cell ( xlnt : : cell_reference ( 4 , 8 ) ) ;
TS_ASSERT_EQUALS ( " E9 " , cell . get_reference ( ) . to_string ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_cell_range_name ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
wb_ . create_named_range ( " test_range_single " , ws , " B12 " ) ;
2014-05-31 06:42:25 +08:00
TS_ASSERT_THROWS ( ws . get_cell ( " test_range_single " ) , xlnt : : cell_coordinates_exception ) ;
2014-05-21 22:20:30 +08:00
auto c_range_name = ws . get_named_range ( " test_range_single " ) ;
auto c_range_coord = ws . get_range ( " B12 " ) ;
auto c_cell = ws . get_cell ( " B12 " ) ;
2014-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS ( c_range_coord , c_range_name ) ;
2014-05-14 04:32:33 +08:00
TS_ASSERT ( c_range_coord [ 0 ] [ 0 ] = = c_cell ) ;
2014-05-09 03:32:12 +08:00
}
void test_garbage_collect ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-14 02:40:28 +08:00
2014-07-26 04:39:25 +08:00
ws . get_cell ( " A1 " ) . set_value ( xlnt : : value : : null ( ) ) ;
ws . get_cell ( " B2 " ) . set_value ( " 0 " ) ;
ws . get_cell ( " C4 " ) . set_value ( 0 ) ;
2014-07-25 05:31:46 +08:00
ws . get_cell ( " D1 " ) . set_comment ( xlnt : : comment ( " Comment " , " Comment " ) ) ;
2014-05-14 02:40:28 +08:00
2014-05-13 01:42:28 +08:00
ws . garbage_collect ( ) ;
2014-05-14 02:40:28 +08:00
2014-07-25 05:31:46 +08:00
auto cell_collection = ws . get_cell_collection ( ) ;
std : : set < xlnt : : cell > cells ( cell_collection . begin ( ) , cell_collection . end ( ) ) ;
std : : set < xlnt : : cell > expected = { ws . get_cell ( " B2 " ) , ws . get_cell ( " C4 " ) , ws . get_cell ( " D1 " ) } ;
2014-05-14 02:40:28 +08:00
2014-07-25 05:31:46 +08:00
// Set difference
std : : set < xlnt : : cell > difference ;
for ( auto a : expected )
{
if ( cells . find ( a ) = = cells . end ( ) )
{
difference . insert ( a ) ;
}
}
for ( auto a : cells )
2014-05-13 01:42:28 +08:00
{
2014-07-25 05:31:46 +08:00
if ( expected . find ( a ) = = expected . end ( ) )
{
difference . insert ( a ) ;
}
2014-05-13 01:42:28 +08:00
}
2014-05-15 06:31:48 +08:00
2014-07-25 05:31:46 +08:00
TS_ASSERT ( difference . empty ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_hyperlink_relationships ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-14 02:40:28 +08:00
TS_ASSERT_EQUALS ( ws . get_relationships ( ) . size ( ) , 0 ) ;
2014-06-11 05:12:15 +08:00
ws . get_cell ( " A1 " ) . set_hyperlink ( " http://test.com " ) ;
2014-05-14 02:40:28 +08:00
TS_ASSERT_EQUALS ( ws . get_relationships ( ) . size ( ) , 1 ) ;
2014-06-11 05:12:15 +08:00
TS_ASSERT_EQUALS ( " rId1 " , ws . get_cell ( " A1 " ) . get_hyperlink ( ) . get_id ( ) ) ;
2014-05-14 02:40:28 +08:00
TS_ASSERT_EQUALS ( " rId1 " , ws . get_relationships ( ) [ 0 ] . get_id ( ) ) ;
2014-06-11 05:12:15 +08:00
TS_ASSERT_EQUALS ( " http://test.com " , ws . get_relationships ( ) [ 0 ] . get_target_uri ( ) ) ;
2014-05-15 06:31:48 +08:00
TS_ASSERT_EQUALS ( xlnt : : target_mode : : external , ws . get_relationships ( ) [ 0 ] . get_target_mode ( ) ) ;
2014-05-14 02:40:28 +08:00
2014-06-11 05:12:15 +08:00
ws . get_cell ( " A2 " ) . set_hyperlink ( " http://test2.com " ) ;
2014-05-14 02:40:28 +08:00
TS_ASSERT_EQUALS ( ws . get_relationships ( ) . size ( ) , 2 ) ;
2014-06-11 05:12:15 +08:00
TS_ASSERT_EQUALS ( " rId2 " , ws . get_cell ( " A2 " ) . get_hyperlink ( ) . get_id ( ) ) ;
2014-05-14 02:40:28 +08:00
TS_ASSERT_EQUALS ( " rId2 " , ws . get_relationships ( ) [ 1 ] . get_id ( ) ) ;
2014-06-11 05:12:15 +08:00
TS_ASSERT_EQUALS ( " http://test2.com " , ws . get_relationships ( ) [ 1 ] . get_target_uri ( ) ) ;
2014-05-15 06:31:48 +08:00
TS_ASSERT_EQUALS ( xlnt : : target_mode : : external , ws . get_relationships ( ) [ 1 ] . get_target_mode ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_bad_relationship_type ( )
{
2014-07-20 02:43:48 +08:00
xlnt : : relationship rel ( " bad " ) ;
2014-05-09 03:32:12 +08:00
}
void test_append_list ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-09 03:32:12 +08:00
2014-05-14 02:40:28 +08:00
ws . append ( std : : vector < std : : string > { " This is A1 " , " This is B1 " } ) ;
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( " This is A1 " , ws . get_cell ( " A1 " ) . get_value ( ) ) ;
TS_ASSERT_EQUALS ( " This is B1 " , ws . get_cell ( " B1 " ) . get_value ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_append_dict_letter ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-09 03:32:12 +08:00
2014-05-14 02:40:28 +08:00
ws . append ( std : : unordered_map < std : : string , std : : string > { { " A " , " This is A1 " } , { " C " , " This is C1 " } } ) ;
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( " This is A1 " , ws . get_cell ( " A1 " ) . get_value ( ) ) ;
TS_ASSERT_EQUALS ( " This is C1 " , ws . get_cell ( " C1 " ) . get_value ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_append_dict_index ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-09 03:32:12 +08:00
2014-05-14 02:40:28 +08:00
ws . append ( std : : unordered_map < int , std : : string > { { 0 , " This is A1 " } , { 2 , " This is C1 " } } ) ;
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( " This is A1 " , ws . get_cell ( " A1 " ) . get_value ( ) ) ;
TS_ASSERT_EQUALS ( " This is C1 " , ws . get_cell ( " C1 " ) . get_value ( ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_append_2d_list ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-09 03:32:12 +08:00
2014-05-14 02:40:28 +08:00
ws . append ( std : : vector < std : : string > { " This is A1 " , " This is B1 " } ) ;
ws . append ( std : : vector < std : : string > { " This is A2 " , " This is B2 " } ) ;
2014-05-09 03:32:12 +08:00
2014-05-21 22:20:30 +08:00
auto vals = ws . get_range ( " A1:B2 " ) ;
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( vals [ 0 ] [ 0 ] . get_value ( ) , " This is A1 " ) ;
TS_ASSERT_EQUALS ( vals [ 0 ] [ 1 ] . get_value ( ) , " This is B1 " ) ;
TS_ASSERT_EQUALS ( vals [ 1 ] [ 0 ] . get_value ( ) , " This is A2 " ) ;
TS_ASSERT_EQUALS ( vals [ 1 ] [ 1 ] . get_value ( ) , " This is B2 " ) ;
2014-05-09 03:32:12 +08:00
}
void test_rows ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
ws . get_cell ( " A1 " ) . set_value ( " first " ) ;
ws . get_cell ( " C9 " ) . set_value ( " last " ) ;
2014-05-09 03:32:12 +08:00
2014-05-14 02:40:28 +08:00
auto rows = ws . rows ( ) ;
2014-05-09 03:32:12 +08:00
2014-07-19 04:20:41 +08:00
TS_ASSERT_EQUALS ( rows . length ( ) , 9 ) ;
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( rows [ 0 ] [ 0 ] . get_value ( ) , " first " ) ;
TS_ASSERT_EQUALS ( rows [ 8 ] [ 2 ] . get_value ( ) , " last " ) ;
2014-05-09 03:32:12 +08:00
}
2014-07-19 04:20:41 +08:00
void test_cols ( )
{
xlnt : : worksheet ws ( wb_ ) ;
2014-07-26 04:39:25 +08:00
ws . get_cell ( " A1 " ) . set_value ( " first " ) ;
ws . get_cell ( " C9 " ) . set_value ( " last " ) ;
2014-07-19 04:20:41 +08:00
auto cols = ws . columns ( ) ;
TS_ASSERT_EQUALS ( cols . length ( ) , 3 ) ;
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( cols [ 0 ] [ 0 ] . get_value ( ) , " first " ) ;
TS_ASSERT_EQUALS ( cols [ 2 ] [ 8 ] . get_value ( ) , " last " ) ;
2014-07-19 04:20:41 +08:00
}
2014-05-09 03:32:12 +08:00
void test_auto_filter ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-14 02:40:28 +08:00
2014-06-05 06:42:17 +08:00
ws . auto_filter ( ws . get_range ( " a1:f1 " ) ) ;
2014-05-20 08:47:15 +08:00
TS_ASSERT_EQUALS ( ws . get_auto_filter ( ) , " A1:F1 " ) ;
2014-05-09 03:32:12 +08:00
2014-05-19 09:29:19 +08:00
ws . unset_auto_filter ( ) ;
TS_ASSERT_EQUALS ( ws . has_auto_filter ( ) , false ) ;
2014-05-09 03:32:12 +08:00
2014-06-05 06:42:17 +08:00
ws . auto_filter ( " c1:g9 " ) ;
2014-05-19 09:29:19 +08:00
TS_ASSERT_EQUALS ( ws . get_auto_filter ( ) , " C1:G9 " ) ;
2014-05-09 03:32:12 +08:00
}
2014-07-27 04:19:15 +08:00
void test_getitem ( )
{
xlnt : : worksheet ws ( wb_ ) ;
xlnt : : cell cell = ws [ xlnt : : cell_reference ( " A1 " ) ] ;
TS_ASSERT ( cell . get_reference ( ) . to_string ( ) = = " A1 " ) ;
}
void test_setitem ( )
{
xlnt : : worksheet ws ( wb_ ) ;
ws [ xlnt : : cell_reference ( " A1 " ) ] . get_value ( ) = 5 ;
TS_ASSERT ( ws [ xlnt : : cell_reference ( " A1 " ) ] . get_value ( ) = = 5 ) ;
}
void test_getslice ( )
{
xlnt : : worksheet ws ( wb_ ) ;
auto cell_range = ws ( " A1 " , " B2 " ) ;
TS_ASSERT_EQUALS ( cell_range [ 0 ] [ 0 ] , ws . get_cell ( " A1 " ) ) ;
TS_ASSERT_EQUALS ( cell_range [ 1 ] [ 0 ] , ws . get_cell ( " A2 " ) ) ;
TS_ASSERT_EQUALS ( cell_range [ 0 ] [ 1 ] , ws . get_cell ( " B1 " ) ) ;
TS_ASSERT_EQUALS ( cell_range [ 1 ] [ 1 ] , ws . get_cell ( " B2 " ) ) ;
}
2014-05-09 03:32:12 +08:00
2014-07-19 04:20:41 +08:00
void test_freeze ( )
2014-05-09 03:32:12 +08:00
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-14 02:40:28 +08:00
2014-07-19 04:20:41 +08:00
ws . freeze_panes ( ws . get_cell ( " b2 " ) ) ;
TS_ASSERT_EQUALS ( ws . get_frozen_panes ( ) . to_string ( ) , " B2 " ) ;
2014-05-19 09:29:19 +08:00
2014-07-19 04:20:41 +08:00
ws . unfreeze_panes ( ) ;
TS_ASSERT ( ! ws . has_frozen_panes ( ) ) ;
2014-05-14 02:40:28 +08:00
2014-07-19 04:20:41 +08:00
ws . freeze_panes ( " c5 " ) ;
TS_ASSERT_EQUALS ( ws . get_frozen_panes ( ) . to_string ( ) , " C5 " ) ;
ws . freeze_panes ( ws . get_cell ( " A1 " ) ) ;
TS_ASSERT ( ! ws . has_frozen_panes ( ) ) ;
2014-05-09 03:32:12 +08:00
}
2014-07-19 04:20:41 +08:00
void test_write_empty ( )
2014-05-09 03:32:12 +08:00
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-07-19 04:20:41 +08:00
auto xml_string = xlnt : : writer : : write_worksheet ( ws ) ;
2014-05-14 02:40:28 +08:00
2014-05-19 09:29:19 +08:00
pugi : : xml_document doc ;
doc . load ( xml_string . c_str ( ) ) ;
2014-07-26 04:39:25 +08:00
auto expected_string =
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:A1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData/> "
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
" </worksheet> " ;
pugi : : xml_document expected_doc ;
expected_doc . load ( expected_string ) ;
TS_ASSERT ( Helper : : compare_xml ( expected_doc , doc ) ) ;
2014-05-09 03:32:12 +08:00
}
2014-07-19 04:20:41 +08:00
void test_page_margins ( )
2014-05-09 03:32:12 +08:00
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-07-26 04:39:25 +08:00
ws . get_page_margins ( ) . set_left ( 2 ) ;
ws . get_page_margins ( ) . set_right ( 2 ) ;
ws . get_page_margins ( ) . set_top ( 2 ) ;
ws . get_page_margins ( ) . set_bottom ( 2 ) ;
ws . get_page_margins ( ) . set_header ( 1.5 ) ;
ws . get_page_margins ( ) . set_footer ( 1.5 ) ;
2014-07-19 04:20:41 +08:00
auto xml_string = xlnt : : writer : : write_worksheet ( ws ) ;
2014-05-09 03:32:12 +08:00
2014-07-19 04:20:41 +08:00
pugi : : xml_document doc ;
doc . load ( xml_string . c_str ( ) ) ;
2014-07-26 04:39:25 +08:00
auto expected_string =
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:A1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData/> "
" <pageMargins left= \" 2 \" right= \" 2 \" top= \" 2 \" bottom= \" 2 \" header= \" 1.5 \" footer= \" 1.5 \" /> "
" </worksheet> " ;
pugi : : xml_document expected_doc ;
expected_doc . load ( expected_string ) ;
TS_ASSERT ( Helper : : compare_xml ( expected_doc , doc ) ) ;
2014-07-19 04:20:41 +08:00
}
void test_merge ( )
{
xlnt : : worksheet ws ( wb_ ) ;
2014-07-26 04:39:25 +08:00
std : : vector < std : : string > string_table = { " Cell A1 " , " Cell B1 " } ;
2014-07-19 04:20:41 +08:00
2014-07-26 04:39:25 +08:00
auto expected_string1 =
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:B1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData> "
" <row r= \" 1 \" spans= \" 1:2 \" > "
" <c r= \" A1 \" t= \" s \" > "
" <v>0</v> "
" </c> "
" <c r= \" B1 \" t= \" s \" > "
" <v>1</v> "
" </c> "
" </row> "
" </sheetData> "
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
" </worksheet> " ;
ws . get_cell ( " A1 " ) . set_value ( " Cell A1 " ) ;
ws . get_cell ( " B1 " ) . set_value ( " Cell B1 " ) ;
auto xml_string = xlnt : : writer : : write_worksheet ( ws , string_table ) ;
2014-05-14 02:40:28 +08:00
2014-07-19 04:20:41 +08:00
pugi : : xml_document doc ;
doc . load ( xml_string . c_str ( ) ) ;
2014-07-26 04:39:25 +08:00
pugi : : xml_document expected_doc ;
expected_doc . load ( expected_string1 ) ;
TS_ASSERT ( Helper : : compare_xml ( expected_doc , doc ) ) ;
ws . merge_cells ( " A1:B1 " ) ;
xml_string = xlnt : : writer : : write_worksheet ( ws , string_table ) ;
auto expected_string2 =
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:B1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData> "
" <row r= \" 1 \" spans= \" 1:2 \" > "
" <c r= \" A1 \" t= \" s \" > "
" <v>0</v> "
" </c> "
" <c r= \" B1 \" t= \" s \" /> "
" </row> "
" </sheetData> "
" <mergeCells count= \" 1 \" > "
" <mergeCell ref= \" A1:B1 \" /> "
" </mergeCells> "
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
" </worksheet> " ;
doc . load ( xml_string . c_str ( ) ) ;
expected_doc . load ( expected_string2 ) ;
TS_ASSERT ( Helper : : compare_xml ( expected_doc , doc ) ) ;
ws . unmerge_cells ( " A1:B1 " ) ;
xml_string = xlnt : : writer : : write_worksheet ( ws , string_table ) ;
auto expected_string3 =
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:B1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData> "
" <row r= \" 1 \" spans= \" 1:2 \" > "
" <c r= \" A1 \" t= \" s \" > "
" <v>0</v> "
" </c> "
" <c r= \" B1 \" t= \" s \" /> "
" </row> "
" </sheetData> "
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
" </worksheet> " ;
doc . load ( xml_string . c_str ( ) ) ;
expected_doc . load ( expected_string3 ) ;
TS_ASSERT ( Helper : : compare_xml ( expected_doc , doc ) ) ;
2014-05-09 03:32:12 +08:00
}
void test_printer_settings ( )
{
2014-06-16 00:16:34 +08:00
xlnt : : worksheet ws ( wb_ ) ;
2014-05-14 02:40:28 +08:00
2014-05-20 08:47:15 +08:00
ws . get_page_setup ( ) . set_orientation ( xlnt : : page_setup : : orientation : : landscape ) ;
2014-05-21 22:20:30 +08:00
ws . get_page_setup ( ) . set_paper_size ( xlnt : : page_setup : : paper_size : : tabloid ) ;
2014-05-20 08:47:15 +08:00
ws . get_page_setup ( ) . set_fit_to_page ( true ) ;
ws . get_page_setup ( ) . set_fit_to_height ( false ) ;
ws . get_page_setup ( ) . set_fit_to_width ( true ) ;
2014-08-02 04:46:54 +08:00
ws . get_page_setup ( ) . set_horizontal_centered ( true ) ;
ws . get_page_setup ( ) . set_vertical_centered ( true ) ;
2014-05-20 08:47:15 +08:00
2014-05-19 09:29:19 +08:00
auto xml_string = xlnt : : writer : : write_worksheet ( ws ) ;
2014-05-20 08:47:15 +08:00
2014-05-19 09:29:19 +08:00
pugi : : xml_document doc ;
doc . load ( xml_string . c_str ( ) ) ;
2014-07-27 04:19:15 +08:00
auto expected_string =
" <?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> "
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" <pageSetUpPr fitToPage= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:A1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData /> "
" <printOptions horizontalCentered= \" 1 \" verticalCentered= \" 1 \" /> "
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
" <pageSetup orientation= \" landscape \" paperSize= \" 3 \" fitToHeight= \" 0 \" fitToWidth= \" 1 \" /> "
" </worksheet> " ;
pugi : : xml_document expected_doc ;
expected_doc . load ( expected_string ) ;
2014-05-20 08:47:15 +08:00
2014-07-27 04:19:15 +08:00
TS_ASSERT ( Helper : : compare_xml ( expected_doc , doc ) ) ;
2014-07-19 04:20:41 +08:00
}
void test_header_footer ( )
{
auto ws = wb_ . create_sheet ( ) ;
ws . get_header_footer ( ) . get_left_header ( ) . set_text ( " Left Header Text " ) ;
ws . get_header_footer ( ) . get_center_header ( ) . set_text ( " Center Header Text " ) ;
ws . get_header_footer ( ) . get_center_header ( ) . set_font_name ( " Arial,Regular " ) ;
ws . get_header_footer ( ) . get_center_header ( ) . set_font_size ( 6 ) ;
ws . get_header_footer ( ) . get_center_header ( ) . set_font_color ( " 445566 " ) ;
ws . get_header_footer ( ) . get_right_header ( ) . set_text ( " Right Header Text " ) ;
ws . get_header_footer ( ) . get_right_header ( ) . set_font_name ( " Arial,Bold " ) ;
ws . get_header_footer ( ) . get_right_header ( ) . set_font_size ( 8 ) ;
ws . get_header_footer ( ) . get_right_header ( ) . set_font_color ( " 112233 " ) ;
ws . get_header_footer ( ) . get_left_footer ( ) . set_text ( " Left Footer Text \n And &[Date] and &[Time] " ) ;
ws . get_header_footer ( ) . get_left_footer ( ) . set_font_name ( " Times New Roman,Regular " ) ;
ws . get_header_footer ( ) . get_left_footer ( ) . set_font_size ( 10 ) ;
ws . get_header_footer ( ) . get_left_footer ( ) . set_font_color ( " 445566 " ) ;
ws . get_header_footer ( ) . get_center_footer ( ) . set_text ( " Center Footer Text &[Path]&[File] on &[Tab] " ) ;
ws . get_header_footer ( ) . get_center_footer ( ) . set_font_name ( " Times New Roman,Bold " ) ;
ws . get_header_footer ( ) . get_center_footer ( ) . set_font_size ( 12 ) ;
ws . get_header_footer ( ) . get_center_footer ( ) . set_font_color ( " 778899 " ) ;
ws . get_header_footer ( ) . get_right_footer ( ) . set_text ( " Right Footer Text &[Page] of &[Pages] " ) ;
ws . get_header_footer ( ) . get_right_footer ( ) . set_font_name ( " Times New Roman,Italic " ) ;
ws . get_header_footer ( ) . get_right_footer ( ) . set_font_size ( 14 ) ;
ws . get_header_footer ( ) . get_right_footer ( ) . set_font_color ( " AABBCC " ) ;
2014-07-20 04:59:05 +08:00
std : : string expected_xml_string =
2014-07-19 04:20:41 +08:00
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
" </sheetPr> "
" <dimension ref= \" A1:A1 \" /> "
" <sheetViews> "
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
" </sheetView> "
" </sheetViews> "
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
" <sheetData/> "
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
" <headerFooter> "
" <oddHeader>&L& \" Calibri,Regular \" &K000000Left Header Text&C& \" Arial,Regular \" &6&K445566Center Header Text&R& \" Arial,Bold \" &8&K112233Right Header Text</oddHeader> "
" <oddFooter>&L& \" Times New Roman,Regular \" &10&K445566Left Footer Text_x000D_And &D and &T&C& \" Times New Roman,Bold \" &12&K778899Center Footer Text &Z&F on &A&R& \" Times New Roman,Italic \" &14&KAABBCCRight Footer Text &P of &N</oddFooter> "
" </headerFooter> "
" </worksheet> " ;
2014-07-20 04:59:05 +08:00
pugi : : xml_document expected_doc ;
pugi : : xml_document observed_doc ;
2014-07-19 04:20:41 +08:00
2014-07-20 04:59:05 +08:00
expected_doc . load ( expected_xml_string . c_str ( ) ) ;
observed_doc . load ( xlnt : : writer : : write_worksheet ( ws , { } , { } ) . c_str ( ) ) ;
2014-07-19 04:20:41 +08:00
2014-07-20 04:59:05 +08:00
TS_ASSERT ( Helper : : compare_xml ( expected_doc , observed_doc ) ) ;
2014-07-19 04:20:41 +08:00
2014-07-20 04:59:05 +08:00
ws = wb_ . create_sheet ( ) ;
2014-07-19 04:20:41 +08:00
expected_xml_string =
" <worksheet xmlns= \" http://schemas.openxmlformats.org/spreadsheetml/2006/main \" xmlns:r= \" http://schemas.openxmlformats.org/officeDocument/2006/relationships \" > "
" <sheetPr> "
2014-07-20 04:59:05 +08:00
" <outlinePr summaryRight= \" 1 \" summaryBelow= \" 1 \" /> "
2014-07-19 04:20:41 +08:00
" </sheetPr> "
2014-07-20 04:59:05 +08:00
" <dimension ref= \" A1:A1 \" /> "
2014-07-19 04:20:41 +08:00
" <sheetViews> "
2014-07-20 04:59:05 +08:00
" <sheetView workbookViewId= \" 0 \" > "
" <selection sqref= \" A1 \" activeCell= \" A1 \" /> "
2014-07-19 04:20:41 +08:00
" </sheetView> "
" </sheetViews> "
2014-07-20 04:59:05 +08:00
" <sheetFormatPr baseColWidth= \" 10 \" defaultRowHeight= \" 15 \" /> "
2014-07-19 04:20:41 +08:00
" <sheetData/> "
2014-07-20 04:59:05 +08:00
" <pageMargins left= \" 0.75 \" right= \" 0.75 \" top= \" 1 \" bottom= \" 1 \" header= \" 0.5 \" footer= \" 0.5 \" /> "
2014-07-19 04:20:41 +08:00
" </worksheet> " ;
2014-07-20 04:59:05 +08:00
expected_doc . load ( expected_xml_string . c_str ( ) ) ;
observed_doc . load ( xlnt : : writer : : write_worksheet ( ws , { } , { } ) . c_str ( ) ) ;
2014-07-19 04:20:41 +08:00
2014-07-20 04:59:05 +08:00
TS_ASSERT ( Helper : : compare_xml ( expected_doc , observed_doc ) ) ;
2014-07-19 04:20:41 +08:00
}
void test_positioning_point ( )
{
2014-07-20 04:59:05 +08:00
auto ws = wb_ . create_sheet ( ) ;
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( ws . get_point_pos ( 150 , 40 ) , xlnt : : cell_reference ( " C3 " ) ) ;
2014-07-19 04:20:41 +08:00
}
void test_positioning_roundtrip ( )
{
auto ws = wb_ . create_sheet ( ) ;
TS_ASSERT_EQUALS ( ws . get_point_pos ( ws . get_cell ( " A1 " ) . get_anchor ( ) ) , xlnt : : cell_reference ( " A1 " ) ) ;
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( ws . get_point_pos ( ws . get_cell ( " D52 " ) . get_anchor ( ) ) , xlnt : : cell_reference ( " D52 " ) ) ;
TS_ASSERT_EQUALS ( ws . get_point_pos ( ws . get_cell ( " X11 " ) . get_anchor ( ) ) , xlnt : : cell_reference ( " X11 " ) ) ;
2014-07-19 04:20:41 +08:00
}
void test_page_setup ( )
{
xlnt : : page_setup p ;
2014-07-26 04:39:25 +08:00
TS_ASSERT_EQUALS ( p . get_scale ( ) , 1 ) ;
p . set_scale ( 2 ) ;
TS_ASSERT_EQUALS ( p . get_scale ( ) , 2 ) ;
2014-07-19 04:20:41 +08:00
}
void test_page_options ( )
{
xlnt : : page_setup p ;
2014-07-26 04:39:25 +08:00
TS_ASSERT ( ! p . get_horizontal_centered ( ) ) ;
TS_ASSERT ( ! p . get_vertical_centered ( ) ) ;
2014-07-19 04:20:41 +08:00
p . set_horizontal_centered ( true ) ;
p . set_vertical_centered ( true ) ;
2014-07-26 04:39:25 +08:00
TS_ASSERT ( p . get_horizontal_centered ( ) ) ;
TS_ASSERT ( p . get_vertical_centered ( ) ) ;
2014-05-09 03:32:12 +08:00
}
2014-05-14 02:40:28 +08:00
private :
2014-06-16 00:16:34 +08:00
xlnt : : workbook wb_ ;
2014-05-09 03:32:12 +08:00
} ;