Skip to content

Commit

Permalink
Fmt txt. closes #107 (#607)
Browse files Browse the repository at this point in the history
* provide fmt_txt() to style character strings

* add more arguments and cleanup

* Update NEWS
  • Loading branch information
JanMarvin committed May 13, 2023
1 parent a63c807 commit bd66671
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(current_sheet)
export(dataframe_to_dims)
export(delete_data)
export(dims_to_dataframe)
export(fmt_txt)
export(get_cell_refs)
export(get_date_origin)
export(get_named_regions)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

* Import `tableStyles` with `wb_load()` and improve `dxf` style creation. [603](https://github.com/JanMarvin/openxlsx2/pull/603)

* Add `fmt_txt()` to style character strings. [607](https://github.com/JanMarvin/openxlsx2/pull/607)


***************************************************************************

Expand Down
124 changes: 124 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,127 @@ read_xml_files <- function(x) {
FUN.VALUE = NA_character_,
USE.NAMES = FALSE)
}

#' format strings independent of the cell style.
#' @details
#' The result is an xml string. It is possible to paste multiple `fmt_txt()`
#' strings together to create a string with differing styles.
#' @param x a string or part of a string
#' @param bold bold
#' @param italic italic
#' @param underline underline
#' @param strike strike
#' @param size the font size
#' @param color a wbColor color for the font
#' @param font the font name
#' @param charset integer value from the table below
#' @param outline TRUE or FALSE
#' @param vertAlign baseline, superscript, or subscript
#' @details
#' |"Value" | "Character Set" |
#' |--------|----------------------|
#' |"0" | "ANSI_CHARSET" |
#' |"1" | "DEFAULT_CHARSET" |
#' |"2" | "SYMBOL_CHARSET" |
#' |"77" | "MAC_CHARSET" |
#' |"128" | "SHIFTJIS_CHARSET" |
#' |"129" | "HANGUL_CHARSET" |
#' |"130" | "JOHAB_CHARSET" |
#' |"134" | "GB2312_CHARSET" |
#' |"136" | "CHINESEBIG5_CHARSET"|
#' |"161" | "GREEK_CHARSET" |
#' |"162" | "TURKISH_CHARSET" |
#' |"163" | "VIETNAMESE_CHARSET" |
#' |"177" | "HEBREW_CHARSET" |
#' |"178" | "ARABIC_CHARSET" |
#' |"186" | "BALTIC_CHARSET" |
#' |"204" | "RUSSIAN_CHARSET" |
#' |"222" | "THAI_CHARSET" |
#' |"238" | "EASTEUROPE_CHARSET" |
#' |"255" | "OEM_CHARSET" |
#' @examples
#' fmt_txt("bar", underline = TRUE)
#' @export
fmt_txt <- function(
x,
bold = FALSE,
italic = FALSE,
underline = FALSE,
strike = FALSE,
size = NULL,
color = NULL,
font = NULL,
charset = NULL,
outline = NULL,
vertAlign = NULL
) {

xml_b <- NULL
xml_i <- NULL
xml_u <- NULL
xml_strk <- NULL
xml_sz <- NULL
xml_color <- NULL
xml_font <- NULL
xml_chrst <- NULL
xml_otln <- NULL
xml_vrtln <- NULL

if (bold) {
xml_b <- xml_node_create("b")
}
if (italic) {
xml_i <- xml_node_create("i")
}
if (underline) {
xml_u <- xml_node_create("u")
}
if (strike) {
xml_strk <- xml_node_create("strike")
}
if (length(size)) {
xml_sz <- xml_node_create("sz", xml_attributes = c(val = as_xml_attr(size)))
}
if (inherits(color, "wbColour")) {
xml_color <- xml_node_create("color", xml_attributes = color)
}
if (length(font)) {
xml_font <- xml_node_create("rFont", xml_attributes = c(val = font))
}
if (length(charset)) {
xml_chrst <- xml_node_create("charset", xml_attributes = c("val" = charset))
}
if (length(outline)) {
xml_otln <- xml_node_create("outline", xml_attributes = c("val" = as_xml_attr(outline)))
}
if (length(vertAlign)) {
xml_vrtln <- xml_node_create("vertAlign", xml_attributes = c("val" = as_xml_attr(vertAlign)))
}

xml_t_attr <- if (grepl("(^\\s+)|(\\s+$)", x)) c("xml:space" = "preserve") else NULL
xml_t <- xml_node_create("t", xml_children = x, xml_attributes = xml_t_attr)

xml_rpr <- xml_node_create(
"rPr",
xml_children = c(
xml_b,
xml_i,
xml_u,
xml_strk,
xml_sz,
xml_color,
xml_font,
xml_chrst,
xml_otln,
xml_vrtln
)
)

xml_node_create(
"r",
xml_children = c(
xml_rpr,
xml_t
)
)
}
76 changes: 76 additions & 0 deletions man/fmt_txt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/strings_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,22 @@ std::string txt_to_xml(

pugi::xml_node is_node = doc.append_child(type.c_str());

// text to export
pugi::xml_node t_node = is_node.append_child("t");;
pugi::xml_document txt_node;
pugi::xml_parse_result result = txt_node.load_string(text.c_str(), pugi::parse_default | pugi::parse_ws_pcdata | pugi::parse_escapes);

if ((text.size() > 0) && (std::isspace(text.at(0)) || std::isspace(text.at(text.size()-1)))) {
t_node.append_attribute("xml:space").set_value("preserve");
}
if (result) {
for (auto is_n : txt_node.children())
is_node.append_copy(is_n);
} else {
// text to export
pugi::xml_node t_node = is_node.append_child("t");

t_node.append_child(pugi::node_pcdata).set_value(text.c_str());
if ((text.size() > 0) && (std::isspace(text.at(0)) || std::isspace(text.at(text.size()-1)))) {
t_node.append_attribute("xml:space").set_value("preserve");
}

t_node.append_child(pugi::node_pcdata).set_value(text.c_str());
}

std::ostringstream oss;
doc.print(oss, " ", pugi_format_flags);
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,25 @@ test_that("as_xml_attr works", {
expect_equal(exp, mm)

})

test_that("string formating", {

foo <- fmt_txt("foo: ", bold = TRUE, size = 16, color = wb_color("green"))
bar <- fmt_txt("bar", underline = TRUE)
txt <- paste0(foo, bar)

exp <- "<is><r><rPr><b/><sz val=\"16\"/><color rgb=\"FF00FF00\"/></rPr><t xml:space=\"preserve\">foo: </t></r></is>"
got <- txt_to_is(foo)
expect_equal(exp, got)

exp <- "<si><r><rPr><b/><sz val=\"16\"/><color rgb=\"FF00FF00\"/></rPr><t xml:space=\"preserve\">foo: </t></r></si>"
got <- txt_to_si(foo)
expect_equal(exp, got)

wb <- wb_workbook()$add_worksheet()$add_data(x = data.frame(txt))

exp <- "foo: bar"
got <- wb_to_df(wb)$txt
expect_equal(exp, got)

})

0 comments on commit bd66671

Please sign in to comment.