31 #ifndef DATA_IMPORTEXPORT_OPENDOCUMENT_HPP_ 32 #define DATA_IMPORTEXPORT_OPENDOCUMENT_HPP_ 34 #include "../Compression/Zip.hpp" 36 #include "../../Helper/Strings.hpp" 71 using Table = std::vector<TableRow>;
80 const std::vector<NamedTable>& tables,
88 std::string
cell(std::uint8_t spacing,
const std::string& raw,
const std::string& style);
117 inline std::string
exportSpreadsheet(
const std::vector<NamedTable>& tables,
bool firstRowBold) {
118 std::vector<StringString> fileContents;
121 fileContents.emplace_back(
"mimetype",
"application/vnd.oasis.opendocument.spreadsheet");
124 fileContents.emplace_back(
125 "META-INF/manifest.xml",
126 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 127 "<manifest:manifest manifest-version=\"1.2\"" 128 " xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n" 129 " <manifest:file-entry manifest:full-path=\"/\" manifest:version=\"1.2\"" 130 " manifest:media-type=\"application/vnd.oasis.opendocument.spreadsheet\"/>" 131 " <manifest:file-entry manifest:full-path=\"content.xml\" manifest:media-type=\"text/xml\"/>\n" 132 "</manifest:manifest>" 137 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 138 "<office:document-content" 139 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"" 140 " xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"" 141 " xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"" 142 " xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"" 143 " xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\">" 144 " <office:automatic-styles>\n" 145 " <style:style style:name=\"headings\" style:family=\"table-cell\">\n" 146 " <style:text-properties fo:font-weight=\"bold\" style:font-weight-asian=\"bold\"" 147 " style:font-weight-complex=\"bold\" />\n" 149 " </office:automatic-styles>\n" 151 " <office:spreadsheet>\n" 154 for(
const auto& table : tables) {
155 content +=
" <table:table table:name=\"" + table.first +
"\">\n";
157 bool boldRow{firstRowBold};
159 for(
const auto& row : table.second) {
163 style =
"table:style-name=\"headings\"";
168 content +=
" <table:table-row>\n";
170 for(
const auto&
cell : row) {
174 content +=
" </table:table-row>\n";
177 content +=
" </table:table>\n";
180 content +=
" </office:spreadsheet>\n" 182 "</office:document-content>\n";
184 fileContents.emplace_back(
"content.xml", content);
214 inline std::string
cell(std::uint8_t spacing,
const std::string& raw,
const std::string& style) {
215 const std::string spaces(spacing,
' ');
218 return spaces +
"<table:table-cell />\n";
221 std::string content{raw};
222 std::string attributes{};
223 double numericValue{};
227 attributes.reserve(style.size() + 1);
229 attributes.push_back(
' ');
236 const auto * oldLocale{
237 std::setlocale(LC_NUMERIC,
"C")
241 numericValue = std::stod(raw);
245 attributes += R
"( office:value-type="float" office:value=")"; 246 attributes += std::to_string(numericValue); 249 catch(
const std::logic_error& ) {}
252 std::setlocale(LC_NUMERIC, oldLocale);
264 std::string result{};
270 result +=
"<table:table-cell";
271 result += attributes;
274 result +=
"<text:p>";
276 result +=
"</text:p>\n";
278 result +=
"</table:table-cell>\n";
constexpr auto cellConstChars
The number of additional characters for a OpenDocument XML cell element and its content.
Definition: OpenDocument.hpp:58
std::vector< TableRow > Table
A vector of vectors of strings used as spreadsheet tables.
Definition: OpenDocument.hpp:71
std::vector< std::string > TableRow
A vector of strings used as rows in a spreadsheet table.
Definition: OpenDocument.hpp:68
std::pair< std::string, Table > NamedTable
A pair containing the name and the content of a spreadsheet table.
Definition: OpenDocument.hpp:74
std::string compress(const std::vector< StringString > &fileContents)
Compresses files using zip.
Definition: Zip.hpp:88
constexpr auto cellLines
The number of lines used for a OpenDocument XML cell element and its content.
Definition: OpenDocument.hpp:55
std::pair< std::string, std::string > StringString
A pair of strings.
Definition: OpenDocument.hpp:65
void replaceAll(std::string &strInOut, std::string_view needle, std::string_view replacement)
Replaces all occurences within a string with another string.
Definition: Strings.hpp:246
constexpr auto cellSpacing
The number of spaces before a OpenDocument XML cell element.
Definition: OpenDocument.hpp:52
bool isDec(std::string_view inputString)
Checks whether a string contains only decimal digits and max. one dot (.).
Definition: Strings.hpp:303
Namespace for importing and exporting OpenDocument spreadsheets.
Definition: OpenDocument.hpp:45
std::string exportSpreadsheet(const std::vector< NamedTable > &tables, bool firstRowBold)
Exports tables as a OpenDocument spreadsheet.
Definition: OpenDocument.hpp:117
std::string cell(std::uint8_t spacing, const std::string &raw, const std::string &style)
Creates the XML code for a simple cell containing a value.
Definition: OpenDocument.hpp:214