diff --git a/NAMESPACE b/NAMESPACE index f490121aa..dea2f2ad0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/NEWS.md b/NEWS.md index 6a248f7f8..5d459ca22 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/class-workbook.R b/R/class-workbook.R index 450da5b65..574aed8c4 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -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, ]) @@ -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) } } @@ -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( @@ -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) }) @@ -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 = "", fl = file.path(xlDir, "sharedStrings.xml") ) @@ -3436,26 +3436,26 @@ wbWorkbook <- R6::R6Class( styleXML <- self$styles_mgr$styles if (length(styleXML$numFmts)) { styleXML$numFmts <- - stri_join( + stringi::stri_join( sprintf('', length(styleXML$numFmts)), pxml(styleXML$numFmts), "" ) } styleXML$fonts <- - stri_join( + stringi::stri_join( sprintf('', length(styleXML$fonts)), pxml(styleXML$fonts), "" ) styleXML$fills <- - stri_join( + stringi::stri_join( sprintf('', length(styleXML$fills)), pxml(styleXML$fills), "" ) styleXML$borders <- - stri_join( + stringi::stri_join( sprintf('', length(styleXML$borders)), pxml(styleXML$borders), "" @@ -3467,13 +3467,13 @@ wbWorkbook <- R6::R6Class( "" ) styleXML$cellXfs <- - stri_join( + stringi::stri_join( sprintf('', length(styleXML$cellXfs)), paste0(styleXML$cellXfs, collapse = ""), "" ) styleXML$cellStyles <- - stri_join( + stringi::stri_join( sprintf('', length(styleXML$cellStyles)), pxml(styleXML$cellStyles), "" @@ -3481,9 +3481,9 @@ wbWorkbook <- R6::R6Class( styleXML$dxfs <- if (length(styleXML$dxfs)) { - stri_join( + stringi::stri_join( sprintf('', length(styleXML$dxfs)), - stri_join(unlist(styleXML$dxfs), sep = " ", collapse = ""), + stringi::stri_join(unlist(styleXML$dxfs), sep = " ", collapse = ""), "" ) } else { @@ -3544,10 +3544,10 @@ wbWorkbook <- R6::R6Class( ## write workbook.xml workbookXML <- self$workbook - workbookXML$sheets <- stri_join("", pxml(workbookXML$sheets), "") + workbookXML$sheets <- stringi::stri_join("", pxml(workbookXML$sheets), "") if (length(workbookXML$definedNames)) { - workbookXML$definedNames <- stri_join("", pxml(workbookXML$definedNames), "") + workbookXML$definedNames <- stringi::stri_join("", pxml(workbookXML$definedNames), "") } # openxml 2.8.1 expects the following order of xml nodes. While we create this per default, it is not @@ -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 = "|" @@ -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 ) @@ -5319,7 +5319,7 @@ wbWorkbook <- R6::R6Class( sprintf( '', - stri_join(attrs, collapse = " ", sep = " "), + stringi::stri_join(attrs, collapse = " ", sep = " "), get_cell_refs(data.frame(first_active_row, first_active_col)), activePane, activePane @@ -6067,7 +6067,7 @@ wbWorkbook <- R6::R6Class( pos <- '' - drawingsXML <- stri_join( + drawingsXML <- stringi::stri_join( "", pos, sprintf('', width, height), @@ -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 = ", ") )) } @@ -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) @@ -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")) ) } @@ -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")) ) } @@ -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")) ) } @@ -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")) ) } } @@ -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 = '', body = pxml(self$drawings_rels[[i]]), tail = '', - fl = file.path(xldrawingsRelsDir, stri_join("drawing", i, ".xml.rels")) + fl = file.path(xldrawingsRelsDir, stringi::stri_join("drawing", i, ".xml.rels")) ) } @@ -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]])) { @@ -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 @@ -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 = ":" ) @@ -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( "", - stri_join(sprintf('', newInds), collapse = ""), + stringi::stri_join(sprintf('', newInds), collapse = ""), "" ) } diff --git a/R/class-worksheet.R b/R/class-worksheet.R index 3e5cddf29..d78660684 100644 --- a/R/class-worksheet.R +++ b/R/class-worksheet.R @@ -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 @@ -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) } @@ -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 diff --git a/R/conditional_formatting.R b/R/conditional_formatting.R index f50990730..2a311d54c 100644 --- a/R/conditional_formatting.R +++ b/R/conditional_formatting.R @@ -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, diff --git a/R/helper-functions.R b/R/helper-functions.R index 230823b0d..464d8e6de 100644 --- a/R/helper-functions.R +++ b/R/helper-functions.R @@ -209,20 +209,20 @@ amp_split <- function(x) { # Extract each component if present and remove &[LCR] if (grepl("&L", x)) { has[1] <- 1 - res[1] <- stri_extract_first_regex(x, "&L.*?(?=&C|&R|$)") + res[1] <- stringi::stri_extract_first_regex(x, "&L.*?(?=&C|&R|$)") } if (grepl("&C", x)) { has[2] <- 1 - res[2] <- stri_extract_first_regex(x, "&C.*?(?=&R|$)") + res[2] <- stringi::stri_extract_first_regex(x, "&C.*?(?=&R|$)") } if (grepl("&R", x)) { has[3] <- 1 - res[3] <- stri_extract_first_regex(x, "&R.*?$") + res[3] <- stringi::stri_extract_first_regex(x, "&R.*?$") } if (sum(has) == 0) return(character(0)) - res <- stri_replace_all_regex(res, "&[LCR]", "") + res <- stringi::stri_replace_all_regex(res, "&[LCR]", "") # return the string vector unname(res) diff --git a/R/openxlsx2-package.R b/R/openxlsx2-package.R index e1651b9b4..d304a0dc3 100644 --- a/R/openxlsx2-package.R +++ b/R/openxlsx2-package.R @@ -12,11 +12,10 @@ #' @import R6 #' @importFrom grDevices bmp col2rgb colors dev.copy dev.list dev.off jpeg palette png rgb tiff #' @importFrom magrittr %>% -#' @importFrom stringi stri_c stri_encode stri_extract_all_regex stri_extract_first_regex -#' stri_isempty stri_join stri_match stri_match_all_regex stri_match_first_regex -#' stri_order stri_opts_collator stri_pad_left stri_rand_strings stri_read_lines -#' stri_replace_all_fixed stri_replace_all_regex stri_split_fixed -#' stri_split_regex stri_sub stri_unescape_unicode stri_unique +#' @importFrom stringi stri_encode stri_extract_all_regex stri_extract_first_regex +#' stri_join stri_match_first_regex stri_order stri_opts_collator stri_pad_left +#' stri_rand_strings stri_read_lines stri_replace_all_fixed stri_replace_all_regex +#' stri_split_fixed stri_sub stri_unescape_unicode stri_unique #' @importFrom utils download.file head menu read.csv unzip #' @importFrom zip zip #' diff --git a/R/utils.R b/R/utils.R index 3d237d98d..6b38689e3 100644 --- a/R/utils.R +++ b/R/utils.R @@ -20,7 +20,7 @@ naToNULLList <- function(x) { paste_c <- function(..., sep = "", collapse = " ", unlist = FALSE) { x <- c(...) if (unlist) x <- unlist(x, use.names = FALSE) - stri_join(x[nzchar(x)], sep = sep, collapse = collapse) + stringi::stri_join(x[nzchar(x)], sep = sep, collapse = collapse) } `%||%` <- function(x, y) if (is.null(x)) y else x diff --git a/R/wb_load.R b/R/wb_load.R index b93be26db..46481b68b 100644 --- a/R/wb_load.R +++ b/R/wb_load.R @@ -451,7 +451,7 @@ wb_load <- function( ## sheet rId links to the workbook.xml.resl which links worksheets/sheet(i).xml file ## order they appear here gives order of worksheets in xlsx file sheets$typ <- basename(sheets$Type) - sheets$target <- stri_join(xmlDir, xl_path, sheets$Target) + sheets$target <- stringi::stri_join(xmlDir, xl_path, sheets$Target) sheets$id <- as.numeric(sheets$sheetId) if (is.null(sheets$state)) sheets$state <- "visible" diff --git a/R/write.R b/R/write.R index 08ca4ac6a..0f8352634 100644 --- a/R/write.R +++ b/R/write.R @@ -264,7 +264,7 @@ write_data2 <- function( } hconvert_date1904 <- grepl('date1904="1"|date1904="true"', - stri_join(unlist(wb$workbook), collapse = ""), + stringi::stri_join(unlist(wb$workbook), collapse = ""), ignore.case = TRUE) # TODO need to tell excel that we have a date, apply some kind of numFmt @@ -959,12 +959,12 @@ write_data_table <- function( ## write autoFilter, can only have a single filter per worksheet if (withFilter) { # TODO: replace ref calculation with wb_dims() coords <- data.frame("x" = c(startRow, startRow + nRow + colNames - 1L), "y" = c(startCol, startCol + nCol - 1L)) - ref <- stri_join(get_cell_refs(coords), collapse = ":") + ref <- stringi::stri_join(get_cell_refs(coords), collapse = ":") wb$worksheets[[sheet]]$autoFilter <- sprintf('', ref) l <- int2col(unlist(coords[, 2])) - dfn <- sprintf("'%s'!%s", wb$get_sheet_names(escape = TRUE)[sheet], stri_join("$", l, "$", coords[, 1], collapse = ":")) + dfn <- sprintf("'%s'!%s", wb$get_sheet_names(escape = TRUE)[sheet], stringi::stri_join("$", l, "$", coords[, 1], collapse = ":")) dn <- sprintf('', sheet - 1L, dfn)