Skip to content

Commit

Permalink
[misc] cleanup of imported stringi functions (#1187)
Browse files Browse the repository at this point in the history
* [misc] cleanup stringi imports

* [misc] update NEWS
  • Loading branch information
JanMarvin authored Nov 20, 2024
1 parent cb0d24f commit 9d04ab1
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 66 deletions.
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,10 @@ importFrom(grDevices,png)
importFrom(grDevices,rgb)
importFrom(grDevices,tiff)
importFrom(magrittr,"%>%")
importFrom(stringi,stri_c)
importFrom(stringi,stri_encode)
importFrom(stringi,stri_extract_all_regex)
importFrom(stringi,stri_extract_first_regex)
importFrom(stringi,stri_isempty)
importFrom(stringi,stri_join)
importFrom(stringi,stri_match)
importFrom(stringi,stri_match_all_regex)
importFrom(stringi,stri_match_first_regex)
importFrom(stringi,stri_opts_collator)
importFrom(stringi,stri_order)
Expand All @@ -199,7 +195,6 @@ importFrom(stringi,stri_read_lines)
importFrom(stringi,stri_replace_all_fixed)
importFrom(stringi,stri_replace_all_regex)
importFrom(stringi,stri_split_fixed)
importFrom(stringi,stri_split_regex)
importFrom(stringi,stri_sub)
importFrom(stringi,stri_unescape_unicode)
importFrom(stringi,stri_unique)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Fixes

* Create date is not reset to the present time in each call to `wb_set_properties()`. [1176](https://github.com/JanMarvin/openxlsx2/pull/1176)
* Improve handling of file headers and footers for a case where `wb_load()` would previously fail. [1186](https://github.com/JanMarvin/openxlsx2/pull/1186)

## Breaking changes

Expand Down
86 changes: 43 additions & 43 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ wbWorkbook <- R6::R6Class(

for (cf in chartfiles) {
chartid <- NROW(self$charts) + 1L
newname <- stri_join("chart", chartid, ".xml")
newname <- stringi::stri_join("chart", chartid, ".xml")
old_chart <- as.integer(gsub("\\D+", "", cf))
self$charts <- rbind(self$charts, from$charts[old_chart, ])

Expand Down Expand Up @@ -1027,13 +1027,13 @@ wbWorkbook <- R6::R6Class(
# two charts can not point to the same rels
if (self$charts$rels[chartid] != "") {
self$charts$rels[chartid] <- gsub(
stri_join(old_chart, ".xml"),
stri_join(chartid, ".xml"),
stringi::stri_join(old_chart, ".xml"),
stringi::stri_join(chartid, ".xml"),
self$charts$rels[chartid]
)
}

drawings_rels[dl] <- gsub(stri_join("(?<=charts/)", cf), newname, drawings_rels[dl], perl = TRUE)
drawings_rels[dl] <- gsub(stringi::stri_join("(?<=charts/)", cf), newname, drawings_rels[dl], perl = TRUE)
}
}

Expand Down Expand Up @@ -1207,12 +1207,12 @@ wbWorkbook <- R6::R6Class(
else
newid <- 1L

if (any(stri_join(tbls$tab_name, suffix) %in% self$tables$tab_name)) {
tbls$tab_name <- stri_join(tbls$tab_name, "1")
if (any(stringi::stri_join(tbls$tab_name, suffix) %in% self$tables$tab_name)) {
tbls$tab_name <- stringi::stri_join(tbls$tab_name, "1")
}

# add _n to all table names found
tbls$tab_name <- stri_join(tbls$tab_name, suffix)
tbls$tab_name <- stringi::stri_join(tbls$tab_name, suffix)
tbls$tab_sheet <- newSheetIndex
# modify tab_xml with updated name, displayName and id
tbls$tab_xml <- vapply(
Expand Down Expand Up @@ -2958,7 +2958,7 @@ wbWorkbook <- R6::R6Class(
} else {
# TODO replace with seq_len() or seq_along()
lapply(seq_len(nThemes), function(i) {
con <- file(file.path(xlthemeDir, stri_join("theme", i, ".xml")), open = "wb")
con <- file(file.path(xlthemeDir, stringi::stri_join("theme", i, ".xml")), open = "wb")
writeBin(charToRaw(pxml(self$theme[[i]])), con)
close(con)
})
Expand Down Expand Up @@ -3400,8 +3400,8 @@ wbWorkbook <- R6::R6Class(
length(self$sharedStrings),
attr(self$sharedStrings, "uniqueCount")
),
#body = stri_join(set_sst(attr(self$sharedStrings, "text")), collapse = "", sep = " "),
body = stri_join(self$sharedStrings, collapse = "", sep = ""),
#body = stringi::stri_join(set_sst(attr(self$sharedStrings, "text")), collapse = "", sep = " "),
body = stringi::stri_join(self$sharedStrings, collapse = "", sep = ""),
tail = "</sst>",
fl = file.path(xlDir, "sharedStrings.xml")
)
Expand Down Expand Up @@ -3436,26 +3436,26 @@ wbWorkbook <- R6::R6Class(
styleXML <- self$styles_mgr$styles
if (length(styleXML$numFmts)) {
styleXML$numFmts <-
stri_join(
stringi::stri_join(
sprintf('<numFmts count="%s">', length(styleXML$numFmts)),
pxml(styleXML$numFmts),
"</numFmts>"
)
}
styleXML$fonts <-
stri_join(
stringi::stri_join(
sprintf('<fonts count="%s">', length(styleXML$fonts)),
pxml(styleXML$fonts),
"</fonts>"
)
styleXML$fills <-
stri_join(
stringi::stri_join(
sprintf('<fills count="%s">', length(styleXML$fills)),
pxml(styleXML$fills),
"</fills>"
)
styleXML$borders <-
stri_join(
stringi::stri_join(
sprintf('<borders count="%s">', length(styleXML$borders)),
pxml(styleXML$borders),
"</borders>"
Expand All @@ -3467,23 +3467,23 @@ wbWorkbook <- R6::R6Class(
"</cellStyleXfs>"
)
styleXML$cellXfs <-
stri_join(
stringi::stri_join(
sprintf('<cellXfs count="%s">', length(styleXML$cellXfs)),
paste0(styleXML$cellXfs, collapse = ""),
"</cellXfs>"
)
styleXML$cellStyles <-
stri_join(
stringi::stri_join(
sprintf('<cellStyles count="%s">', length(styleXML$cellStyles)),
pxml(styleXML$cellStyles),
"</cellStyles>"
)

styleXML$dxfs <-
if (length(styleXML$dxfs)) {
stri_join(
stringi::stri_join(
sprintf('<dxfs count="%s">', length(styleXML$dxfs)),
stri_join(unlist(styleXML$dxfs), sep = " ", collapse = ""),
stringi::stri_join(unlist(styleXML$dxfs), sep = " ", collapse = ""),
"</dxfs>"
)
} else {
Expand Down Expand Up @@ -3544,10 +3544,10 @@ wbWorkbook <- R6::R6Class(

## write workbook.xml
workbookXML <- self$workbook
workbookXML$sheets <- stri_join("<sheets>", pxml(workbookXML$sheets), "</sheets>")
workbookXML$sheets <- stringi::stri_join("<sheets>", pxml(workbookXML$sheets), "</sheets>")

if (length(workbookXML$definedNames)) {
workbookXML$definedNames <- stri_join("<definedNames>", pxml(workbookXML$definedNames), "</definedNames>")
workbookXML$definedNames <- stringi::stri_join("<definedNames>", pxml(workbookXML$definedNames), "</definedNames>")
}

# openxml 2.8.1 expects the following order of xml nodes. While we create this per default, it is not
Expand Down Expand Up @@ -4956,13 +4956,13 @@ wbWorkbook <- R6::R6Class(
fileNo <- reg_match0(removeRels, "(?<=pivotTable)[0-9]+(?=\\.xml)")
fileNo <- as.integer(unlist(fileNo))

toRemove <- stri_join(
toRemove <- stringi::stri_join(
sprintf("(pivotCacheDefinition%i\\.xml)", fileNo),
sep = " ",
collapse = "|"
)

toRemove <- stri_join(
toRemove <- stringi::stri_join(
sprintf("(pivotCacheDefinition%i\\.xml)", grep(toRemove, self$pivotTables.xml.rels)),
sep = " ",
collapse = "|"
Expand Down Expand Up @@ -5013,15 +5013,15 @@ wbWorkbook <- R6::R6Class(
if (nSheets > 1) {
for (i in (sheet + 1L):nSheets) {
self$workbook$sheets <- gsub(
stri_join("rId", i),
stri_join("rId", i - 1L),
stringi::stri_join("rId", i),
stringi::stri_join("rId", i - 1L),
self$workbook$sheets,
fixed = TRUE
)
# these are zero indexed
self$workbook$bookViews <- gsub(
stri_join("activeTab=\"", i - 1L, "\""),
stri_join("activeTab=\"", i - 2L, "\""),
stringi::stri_join("activeTab=\"", i - 1L, "\""),
stringi::stri_join("activeTab=\"", i - 2L, "\""),
self$workbook$bookViews,
fixed = TRUE
)
Expand Down Expand Up @@ -5319,7 +5319,7 @@ wbWorkbook <- R6::R6Class(

sprintf(
'<pane %s topLeftCell="%s" activePane="%s" state="frozen"/><selection pane="%s"/>',
stri_join(attrs, collapse = " ", sep = " "),
stringi::stri_join(attrs, collapse = " ", sep = " "),
get_cell_refs(data.frame(first_active_row, first_active_col)),
activePane,
activePane
Expand Down Expand Up @@ -6067,7 +6067,7 @@ wbWorkbook <- R6::R6Class(

pos <- '<xdr:pos x="0" y="0" />'

drawingsXML <- stri_join(
drawingsXML <- stringi::stri_join(
"<xdr:absoluteAnchor>",
pos,
sprintf('<xdr:ext cx="%s" cy="%s"/>', width, height),
Expand Down Expand Up @@ -6739,7 +6739,7 @@ wbWorkbook <- R6::R6Class(
showText <-
c(showText, sprintf(
"Write order: %s",
stri_join(self$sheetOrder, sep = " ", collapse = ", ")
stringi::stri_join(self$sheetOrder, sep = " ", collapse = ", ")
))
}

Expand Down Expand Up @@ -9441,7 +9441,7 @@ wbWorkbook <- R6::R6Class(

## write file path to media slot to copy across on save
tmp <- file
names(tmp) <- stri_join("image", mediaNo, ".", imageType)
names(tmp) <- stringi::stri_join("image", mediaNo, ".", imageType)
self$append("media", tmp)

invisible(self)
Expand Down Expand Up @@ -9529,7 +9529,7 @@ wbWorkbook <- R6::R6Class(

write_file(
body = self$charts$chart[crt],
fl = file.path(xlchartsDir, stri_join("chart", crt, ".xml"))
fl = file.path(xlchartsDir, stringi::stri_join("chart", crt, ".xml"))
)
}

Expand All @@ -9538,7 +9538,7 @@ wbWorkbook <- R6::R6Class(

write_file(
body = self$charts$chartEx[crt],
fl = file.path(xlchartsDir, stri_join("chartEx", crt, ".xml"))
fl = file.path(xlchartsDir, stringi::stri_join("chartEx", crt, ".xml"))
)
}

Expand All @@ -9547,7 +9547,7 @@ wbWorkbook <- R6::R6Class(

write_file(
body = self$charts$colors[crt],
fl = file.path(xlchartsDir, stri_join("colors", crt, ".xml"))
fl = file.path(xlchartsDir, stringi::stri_join("colors", crt, ".xml"))
)
}

Expand All @@ -9556,21 +9556,21 @@ wbWorkbook <- R6::R6Class(

write_file(
body = self$charts$style[crt],
fl = file.path(xlchartsDir, stri_join("style", crt, ".xml"))
fl = file.path(xlchartsDir, stringi::stri_join("style", crt, ".xml"))
)
}

if (self$charts$rels[crt] != "") {
write_file(
body = self$charts$rels[crt],
fl = file.path(xlchartsRelsDir, stri_join("chart", crt, ".xml.rels"))
fl = file.path(xlchartsRelsDir, stringi::stri_join("chart", crt, ".xml.rels"))
)
}

if (self$charts$relsEx[crt] != "") {
write_file(
body = self$charts$relsEx[crt],
fl = file.path(xlchartsRelsDir, stri_join("chartEx", crt, ".xml.rels"))
fl = file.path(xlchartsRelsDir, stringi::stri_join("chartEx", crt, ".xml.rels"))
)
}
}
Expand All @@ -9589,14 +9589,14 @@ wbWorkbook <- R6::R6Class(
head = "",
body = pxml(self$drawings[[i]]),
tail = "",
fl = file.path(xldrawingsDir, stri_join("drawing", i, ".xml"))
fl = file.path(xldrawingsDir, stringi::stri_join("drawing", i, ".xml"))
)
if (!all(self$drawings_rels[[i]] == "")) {
write_file(
head = '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">',
body = pxml(self$drawings_rels[[i]]),
tail = '</Relationships>',
fl = file.path(xldrawingsRelsDir, stri_join("drawing", i, ".xml.rels"))
fl = file.path(xldrawingsRelsDir, stringi::stri_join("drawing", i, ".xml.rels"))
)
}

Expand Down Expand Up @@ -9632,7 +9632,7 @@ wbWorkbook <- R6::R6Class(

write_file(
body = self$worksheets[[i]]$get_prior_sheet_data(),
fl = file.path(chartSheetDir, stri_join("sheet", i, ".xml"))
fl = file.path(chartSheetDir, stringi::stri_join("sheet", i, ".xml"))
)

if (length(self$worksheets_rels[[i]])) {
Expand All @@ -9655,7 +9655,7 @@ wbWorkbook <- R6::R6Class(


if (!is.null(cc)) {
cc$r <- stri_join(cc$c_r, cc$row_r)
cc$r <- stringi::stri_join(cc$c_r, cc$row_r)
# prepare data for output

# there can be files, where row_attr is incomplete because a row
Expand Down Expand Up @@ -9820,7 +9820,7 @@ wbWorkbook <- R6::R6Class(
# TODO rename: setConditionFormatting? Or addConditionalFormatting
# TODO can this be moved to the sheet data?
sheet <- private$get_sheet_index(sheet)
sqref <- stri_join(
sqref <- stringi::stri_join(
get_cell_refs(data.frame(x = c(startRow, endRow), y = c(startCol, endCol))),
collapse = ":"
)
Expand Down Expand Up @@ -10063,9 +10063,9 @@ wbWorkbook <- R6::R6Class(
## update workbook r:id to match reordered workbook.xml.rels externalLink element
if (length(extRefInds)) {
newInds <- seq_along(extRefInds) + length(sheetInds)
self$workbook$externalReferences <- stri_join(
self$workbook$externalReferences <- stringi::stri_join(
"<externalReferences>",
stri_join(sprintf('<externalReference r:id=\"rId%s\"/>', newInds), collapse = ""),
stringi::stri_join(sprintf('<externalReference r:id=\"rId%s\"/>', newInds), collapse = ""),
"</externalReferences>"
)
}
Expand Down
6 changes: 3 additions & 3 deletions R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ wbWorksheet <- R6::R6Class(
cols <- range(col2int(cols))

sqref <- paste0(int2col(cols), rows)
sqref <- stri_join(sqref, collapse = ":", sep = " ")
sqref <- stringi::stri_join(sqref, collapse = ":", sep = " ")

current <- rbindlist(xml_attr(xml = self$mergeCells, "mergeCell"))$ref

Expand Down Expand Up @@ -549,7 +549,7 @@ wbWorksheet <- R6::R6Class(
} else {
msg <- sprintf(
"Merge intersects with existing merged cells: \n\t\t%s.\nRemove existing merge first.",
stri_join(current[intersects], collapse = "\n\t\t")
stringi::stri_join(current[intersects], collapse = "\n\t\t")
)
stop(msg, call. = FALSE)
}
Expand All @@ -572,7 +572,7 @@ wbWorksheet <- R6::R6Class(
cols <- range(col2int(cols))

sqref <- paste0(int2col(cols), rows)
sqref <- stri_join(sqref, collapse = ":", sep = " ")
sqref <- stringi::stri_join(sqref, collapse = ":", sep = " ")

current <- rbindlist(xml_attr(xml = self$mergeCells, "mergeCell"))$ref

Expand Down
2 changes: 1 addition & 1 deletion R/conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ cf_create_databar <- function(extLst, formula, params, sqref, values) {
negColor <- "FFFF0000"
}

guid <- stri_join(
guid <- stringi::stri_join(
"F7189283-14F7-4DE0-9601-54DE9DB",
40000L + length(xml_node(
extLst,
Expand Down
Loading

0 comments on commit 9d04ab1

Please sign in to comment.