From 0efb3d2f9712cc52e84e79e665c9d49663d4b3ae Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Mon, 26 Oct 2015 23:06:00 -0400 Subject: [PATCH] fix worksheet reordering on save --- build/cmake/build-osx.sh | 2 +- build/cmake/xlnt/CMakeLists.txt | 2 +- include/xlnt/styles/fill.hpp | 59 ++++++++++++++++++++++++++++++++ samples/sample1.xlsx | Bin 491964 -> 495074 bytes source/reader/style_reader.cpp | 16 +++++++++ source/workbook/workbook.cpp | 10 +++++- source/writer/excel_writer.cpp | 17 ++++++--- source/writer/style_writer.cpp | 2 ++ 8 files changed, 100 insertions(+), 8 deletions(-) diff --git a/build/cmake/build-osx.sh b/build/cmake/build-osx.sh index 57fabcae..c1c565c4 100755 --- a/build/cmake/build-osx.sh +++ b/build/cmake/build-osx.sh @@ -7,5 +7,5 @@ fi mkdir build cd build -cmake .. +cmake -G "Unix Makefiles" .. make diff --git a/build/cmake/xlnt/CMakeLists.txt b/build/cmake/xlnt/CMakeLists.txt index 5284a473..2a72bd28 100644 --- a/build/cmake/xlnt/CMakeLists.txt +++ b/build/cmake/xlnt/CMakeLists.txt @@ -5,7 +5,7 @@ include_directories(../../../source) include_directories(../../../third-party/miniz) include_directories(../../../third-party/pugixml/src) -FILE(GLOB SOURCES ../../../source/*/*.cpp) +FILE(GLOB SOURCES ../../../source/*/* ../../../include/xlnt/*/*.hpp ../../../include/xlnt/*.hpp) FILE(GLOB DETAIL_SOURCES ../../../source/detail/*.cpp) add_library(xlnt STATIC ${SOURCES} ${DETAIL_SOURCES} ../../../third-party/pugixml/src/pugixml.cpp ../../../third-party/miniz/miniz.c) diff --git a/include/xlnt/styles/fill.hpp b/include/xlnt/styles/fill.hpp index cfde640a..831c6f38 100644 --- a/include/xlnt/styles/fill.hpp +++ b/include/xlnt/styles/fill.hpp @@ -201,6 +201,65 @@ public: if(type_ == type::pattern) { hash_combine(seed, static_cast(pattern_type_)); + hash_combine(seed, foreground_color_assigned_); + + if(foreground_color_assigned_) + { + hash_combine(seed, static_cast(foreground_color_.get_type())); + + switch(foreground_color_.get_type()) + { + case color::type::auto_: + hash_combine(seed, static_cast(foreground_color_.get_auto())); + break; + case color::type::indexed: + hash_combine(seed, static_cast(foreground_color_.get_index())); + break; + case color::type::theme: + hash_combine(seed, static_cast(foreground_color_.get_theme())); + break; + } + } + + hash_combine(seed, background_color_assigned_); + + if(background_color_assigned_) + { + hash_combine(seed, static_cast(background_color_.get_type())); + + switch(foreground_color_.get_type()) + { + case color::type::auto_: + hash_combine(seed, static_cast(background_color_.get_auto())); + break; + case color::type::indexed: + hash_combine(seed, static_cast(background_color_.get_index())); + break; + case color::type::theme: + hash_combine(seed, static_cast(background_color_.get_theme())); + break; + } + } + } + else if(type_ == type::gradient) + { + hash_combine(seed, static_cast(gradient_type_)); + + if(gradient_type_ == gradient_type::path) + { + hash_combine(seed, gradient_path_left_); + hash_combine(seed, gradient_path_right_); + hash_combine(seed, gradient_path_top_); + hash_combine(seed, gradient_path_bottom_); + } + else if(gradient_type_ == gradient_type::linear) + { + hash_combine(seed, rotation_); + } + } + else if(type_ == type::solid) + { +// hash_combine(seed, static_cast()); } return seed; diff --git a/samples/sample1.xlsx b/samples/sample1.xlsx index 2b37bd1450f1b389f719fa247973e72b26279dfd..12da556a79bf71e246d21ea3db5763f160055740 100644 GIT binary patch delta 5503 zcmZu#2RNKfyIzD1ixu65=tS?5sL^|m9wAzY=!p_#3DK9ZTC~->=p;%+38DlmVi6^X z-j>xv&hr1~P&v;ICbE~?XH>+8>UATf@Ggxa16S#~ zea%i~TDpC~mxm{ZVkf zL5&iST~=W(y>w-S@%=S0uZ;J@O^r8^P4#3sU#;v?t$_S9Vk;H-sFT(Yg+gL2RsnT$ zP}PB&C(KfH3G?G}>HIroQ_vRqWx3L9oZbdw9v;|2qwRJUmzto3oo_~TbSdo@0la{> zati(0k_(gv>~Xb-9*HG`oU16&$aKkI2(ALDk$DM*?q1ypO)rV#c@IrRQPX&}7&Z9uGgGu3P z_M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4Q<@N&V3^@L*mLuSzK0m zLo`z;C3s;vmv@QMLmeqy8oeJ;a69%J3AvQ0iV3tuaW{;0DB+2xGRl?28fBou$Kl{q>m}I z>c*x#SdEYGj|{kc&BnkbC3XCF zD{3SvPxYW4d*NV{8Z#ug{ky?v3S_zQcy|2?^90|DV6nyxRrBWVhMol9$o&oRtM3}m z89P>Yd$G^do7uS6M!6;!#}5qFw?qA$ocZeT?c2|3>`wR_r+ZOxwWtAY z555_FKfcFp%3o)6l=*X0^W&=mH%GsIYdo0qSpCju3IBf!i3<@;3=w6Z2O~Wo^nlU> zMh~X?5K%DI<7fiB9ORQOWB`CyOhm3EP!?O2*=s(dz}d07%ac}9*&z$YUounon_@O3 zaa^4?a{Gc^eX5q| zLE25OFn!mOz8>9n>aC@)r_E8?>Y#ka`gWZaiOThXSCJCMf_GAx1vJzlq%#95f20U=Y#AC zgJKjb9V5wti%A?UmM-SqHUUBld!Xd*Y8^Gu5^;XRmRZ+!EziY zNW+8ZBh=544*+aGA@pp4n|Lk-6gZ;5fu1m;d5K{upy<)HpF;n4M^7-s`V{a4I~;%m NY!Cp*{sW{J`!8Lx9u5Ei diff --git a/source/reader/style_reader.cpp b/source/reader/style_reader.cpp index b70ee959..640da5cc 100644 --- a/source/reader/style_reader.cpp +++ b/source/reader/style_reader.cpp @@ -280,6 +280,14 @@ void style_reader::read_fills(pugi::xml_node fills_node) { new_fill.set_background_color(color(color::type::indexed, bg_color_node.attribute("indexed").as_ullong())); } + else if(bg_color_node.attribute("auto") != nullptr) + { + new_fill.set_background_color(color(color::type::auto_, bg_color_node.attribute("auto").as_ullong())); + } + else if(bg_color_node.attribute("theme") != nullptr) + { + new_fill.set_background_color(color(color::type::theme, bg_color_node.attribute("theme").as_ullong())); + } } auto fg_color_node = pattern_fill_node.child("fgColor"); @@ -290,6 +298,14 @@ void style_reader::read_fills(pugi::xml_node fills_node) { new_fill.set_foreground_color(color(color::type::indexed, fg_color_node.attribute("indexed").as_ullong())); } + else if(fg_color_node.attribute("auto") != nullptr) + { + new_fill.set_foreground_color(color(color::type::auto_, fg_color_node.attribute("auto").as_ullong())); + } + else if(fg_color_node.attribute("theme") != nullptr) + { + new_fill.set_foreground_color(color(color::type::theme, fg_color_node.attribute("theme").as_ullong())); + } } } diff --git a/source/workbook/workbook.cpp b/source/workbook/workbook.cpp index e3997df4..7d378895 100644 --- a/source/workbook/workbook.cpp +++ b/source/workbook/workbook.cpp @@ -298,8 +298,16 @@ void workbook::remove_sheet(worksheet ws) { throw std::runtime_error("worksheet not owned by this workbook"); } - + auto sheet_filename = "xl/worksheets/sheet" + std::to_string(d_->worksheets_.size()) + ".xml"; + auto rel_iter = std::find_if(d_->relationships_.begin(), d_->relationships_.end(), [=](relationship &r) { return r.get_target_uri() == sheet_filename; }); + + if(rel_iter == d_->relationships_.end()) + { + throw std::runtime_error("no matching rel found"); + } + + d_->relationships_.erase(rel_iter); d_->worksheets_.erase(match_iter); } diff --git a/source/writer/excel_writer.cpp b/source/writer/excel_writer.cpp index a38e5a69..80d5b66c 100644 --- a/source/writer/excel_writer.cpp +++ b/source/writer/excel_writer.cpp @@ -108,14 +108,21 @@ void excel_writer::write_chartsheets(zip_file &/*archive*/) void excel_writer::write_worksheets(zip_file &archive, const std::vector &shared_strings) { - for(auto relationship : wb_.get_relationships()) + std::size_t index = 0; + + for(auto ws : wb_) { - if(relationship.get_type() == relationship::type::worksheet) + for(auto relationship : wb_.get_relationships()) { - auto sheet_index = workbook::index_from_ws_filename(relationship.get_target_uri()); - auto ws = wb_.get_sheet_by_index(sheet_index); - archive.writestr(relationship.get_target_uri(), write_worksheet(ws, shared_strings)); + if(relationship.get_type() == relationship::type::worksheet && + workbook::index_from_ws_filename(relationship.get_target_uri()) == index) + { + archive.writestr(relationship.get_target_uri(), write_worksheet(ws, shared_strings)); + break; + } } + + index++; } } diff --git a/source/writer/style_writer.cpp b/source/writer/style_writer.cpp index e313ef68..fe58f3cb 100644 --- a/source/writer/style_writer.cpp +++ b/source/writer/style_writer.cpp @@ -356,6 +356,8 @@ std::string style_writer::write_table() const "ff000000", "ffaaaaaa", "ffbdc0bf", + "ffdbdbdb", + "ffbdc0bf", "ffdbdbdb" };