Skip to content

Commit

Permalink
Enable some line spilitting
Browse files Browse the repository at this point in the history
* But no indentation unfortunately
  • Loading branch information
chainsawriot committed Aug 21, 2023
1 parent 99944c7 commit 6c590c7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
Binary file modified R/sysdata.rda
Binary file not shown.
5 changes: 5 additions & 0 deletions R/write_ods.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ write_ods <- function(x, path = tempfile(fileext = ".ods"), sheet = "Sheet1", ap
#' @export
write_fods <- function(x, path = tempfile(fileext = ".fods"), sheet = "Sheet1", row_names = FALSE, col_names = TRUE, na_as_string = FALSE) {
normalized_path <- normalizePath(path, mustWork = FALSE)
## ensure the file can be created
file.create(normalized_path)
if (!file.exists(normalized_path)) {
stop("File cannot be created at this path: ", normalized_path, call. = FALSE)
}
x <- .preprocess_x(x)
write_sheet_(filename = normalized_path, x = x, sheet = sheet, row_names = row_names, col_names = col_names, na_as_string = na_as_string, padding = FALSE, header = .FODS_HEADER, footer = .FODS_FOOTER)
invisible(path)
Expand Down
4 changes: 2 additions & 2 deletions benchmark/fods_footer.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
</office:spreadsheet>
</office:body>
</office:spreadsheet>
</office:body>
</office:document>
6 changes: 3 additions & 3 deletions benchmark/fods_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<office:document xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rpt="http://openoffice.org/2005/report" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:xforms="http://www.w3.org/2002/xforms" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.spreadsheet">

<office:meta><meta:generator>readODS</meta:generator></office:meta>
<office:body>
<office:spreadsheet>
<office:meta><meta:generator>readODS</meta:generator></office:meta>
<office:body>
<office:spreadsheet>
27 changes: 15 additions & 12 deletions src/write_sheet_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ void cell_out_ (const cpp11::r_string& value_type, const cpp11::r_string& value,
}
xml_file << "\" table:style-name=\"ce1\"><text:p>";
xml_file << value_c;
xml_file << "</text:p></table:table-cell>";
xml_file << "</text:p></table:table-cell>\n";
}

void pad_rows_ (const bool& padding, const int& cols, const int& cmax, std::ofstream& xml_file) {
if (cols < cmax && padding) {
xml_file << "<table:table-cell table:number-columns-repeated=\"";
xml_file << cmax - cols;
xml_file << "\"/>";
xml_file << "\"/>\n";
}
}

Expand Down Expand Up @@ -90,13 +90,13 @@ cpp11::r_string write_sheet_(const std::string& filename,
int cmax = column_types.size() > 1024 ? 16384 : 1024;
// gen_sheet_tag
xml_file << header;
xml_file << "<table:table table:name=\"";
xml_file << "\n<table:table table:name=\"";
xml_file << escaped_sheet;
xml_file << "\" table:style-name=\"ta1\">";
xml_file << "\" table:style-name=\"ta1\">\n";
// column
xml_file << "<table:table-column table:style-name=\"co1\" table:number-columns-repeated=\"";
padding ? xml_file << cmax : xml_file << cols;
xml_file << "\" table:default-cell-style-name=\"ce1\"/>";
xml_file << "\" table:default-cell-style-name=\"ce1\"/>\n";
// add_data
if (col_names) {
xml_file << "<table:table-row table:style-name=\"ro1\">";
Expand All @@ -107,10 +107,10 @@ cpp11::r_string write_sheet_(const std::string& filename,
cell_out_("string", colnames_x[j], xml_file);
}
pad_rows_(padding, cols, cmax, xml_file);
xml_file << "</table:table-row>";
xml_file << "</table:table-row>\n";
}
for (int i = 0; i < x_list[0].size(); i++) {
xml_file << "<table:table-row table:style-name=\"ro1\">";
xml_file << "<table:table-row table:style-name=\"ro1\">\n";
if (row_names) {
cell_out_("string", rownames_x[i], xml_file);
}
Expand All @@ -120,24 +120,27 @@ cpp11::r_string write_sheet_(const std::string& filename,
continue;
}
if (!na_as_string) {
xml_file << "<table:table-cell/>";
xml_file << "<table:table-cell/>\n";
continue;
}
cell_out_("string", "NA", xml_file);
}
pad_rows_(padding, cols, cmax, xml_file);
xml_file << "</table:table-row>";
xml_file << "</table:table-row>\n";
}
// pad_columns
if (rows < 1048576 && padding) {
xml_file << "<table:table-row table:style-name=\"ro1\" table:number-rows-repeated=\"";
xml_file << 1048576 - rows;
xml_file << "\"><table:table-cell table:number-columns-repeated=\"";
xml_file << "\">\n";
xml_file << "<table:table-cell table:number-columns-repeated=\"";
xml_file << cmax;
xml_file << "\"/></table:table-row>";
xml_file << "\"/>";
xml_file << "</table:table-row>\n";
}
xml_file << "</table:table>";
xml_file << "</table:table>\n";
xml_file << footer;
xml_file << "\n";
xml_file.close();
return filename;
}
4 changes: 2 additions & 2 deletions tests/testthat/test_write_ods.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ test_that("na_as_string, #79", {
write_ods(na_data, path = temp_odsfile, na_as_string = TRUE, col_names = FALSE)
zip::unzip(temp_odsfile, exdir = temp_odsdir)
contentfile <- file.path(temp_odsdir, "content.xml")
expect_true(grepl("office:value-type=\"string\"", suppressWarnings(readLines(contentfile))))
expect_true(any(grepl("office:value-type=\"string\"", suppressWarnings(readLines(contentfile)))))
temp_odsfile <- tempfile(fileext = ".ods")
temp_odsdir <- tempdir()
na_data <- data.frame(x = c(1.0, NA, 2.0))
write_ods(na_data, path = temp_odsfile, na_as_string = FALSE, col_names = FALSE)
zip::unzip(temp_odsfile, exdir = temp_odsdir)
contentfile <- file.path(temp_odsdir, "content.xml")
expect_false(grepl("office:value-type=\"string\"", suppressWarnings(readLines(contentfile))))
expect_false(any(grepl("office:value-type=\"string\"", suppressWarnings(readLines(contentfile)))))
})

test_that("na_as_string, round trip", {
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test_write_sheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ test_that("padding TRUE, ncol <= 1024", {
test_data <- data.frame(x = c(1.1, 2.2, 3.3), y = c("a", "b", "c"))
.write_sheet_(test_data, filename = throwaway_xml_file, padding = TRUE)
content <- readLines(throwaway_xml_file, warn = FALSE)
expect_true(grepl("table:number-columns-repeated=\"1024\"", content))
expect_true(any(grepl("table:number-columns-repeated=\"1024\"", content)))
## 1024 - ncol(test_data)
expect_true(grepl("table:number-columns-repeated=\"1022\"", content))
expect_true(any(grepl("table:number-columns-repeated=\"1022\"", content)))
## 2^20 - 3
expect_true(grepl("table:number-rows-repeated=\"1048573\"", content))
expect_true(any(grepl("table:number-rows-repeated=\"1048573\"", content)))
})

test_that("padding FALSE, ncol <= 1024", {
throwaway_xml_file <- tempfile(fileext = ".xml")
test_data <- data.frame(x = c(1.1, 2.2, 3.3), y = c("a", "b", "c"))
.write_sheet_(test_data, throwaway_xml_file, padding = FALSE)
content <- readLines(throwaway_xml_file, warn = FALSE)
expect_false(grepl("table:number-columns-repeated=\"1024\"", content))
expect_false(any(grepl("table:number-columns-repeated=\"1024\"", content)))
## 1024 - ncol(test_data)
expect_false(grepl("table:number-columns-repeated=\"1022\"", content))
expect_false(any(grepl("table:number-columns-repeated=\"1022\"", content)))
## 2^20 - 3
expect_false(grepl("table:number-rows-repeated=\"1048573\"", content))
expect_false(any(grepl("table:number-rows-repeated=\"1048573\"", content)))
})

## padding > 1024 cols
Expand All @@ -40,9 +40,9 @@ test_that("padding TRUE, ncol > 1024", {
test_data <- as.data.frame(matrix(rnorm(1025), nrow = 1))
.write_sheet_(test_data, throwaway_xml_file, padding = TRUE)
content <- readLines(throwaway_xml_file, warn = FALSE)
expect_true(grepl("table:number-columns-repeated=\"16384\"", content))
expect_true(any(grepl("table:number-columns-repeated=\"16384\"", content)))
## 16384 - ncol(test_data)
expect_true(grepl("table:number-columns-repeated=\"15359\"", content))
expect_true(any(grepl("table:number-columns-repeated=\"15359\"", content)))
## 2^20 - 1
expect_true(grepl("table:number-rows-repeated=\"1048575\"", content))
expect_true(any(grepl("table:number-rows-repeated=\"1048575\"", content)))
})

0 comments on commit 6c590c7

Please sign in to comment.