diff --git a/NEWS.md b/NEWS.md index 41c5112f5..b69f14888 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ * Previously formulas written as data frames were not xml escaped. [834](https://github.com/JanMarvin/openxlsx2/pull/834) +* Improve drawing relationship id selection that could cause issues with unordered relationship ids in loaded workbooks. [838](https://github.com/JanMarvin/openxlsx2/pull/838) + *************************************************************************** diff --git a/R/baseXML.R b/R/baseXML.R index 7691529de..a3ca859d3 100644 --- a/R/baseXML.R +++ b/R/baseXML.R @@ -243,7 +243,7 @@ genBaseStyleSheet <- function(dxfs = NULL, tableStyles = NULL, extLst = NULL) { ) } -genBasePic <- function(imageNo) { +genBasePic <- function(imageNo, next_id) { sprintf(' @@ -252,7 +252,7 @@ genBasePic <- function(imageNo) { - + @@ -263,7 +263,7 @@ genBasePic <- function(imageNo) { - ', imageNo, imageNo, imageNo) + ', imageNo, imageNo, next_id) } genBaseTheme <- function() { diff --git a/R/class-workbook.R b/R/class-workbook.R index ed6a15442..af1e8e2b5 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -4677,6 +4677,12 @@ wbWorkbook <- R6::R6Class( imageNo <- 1L } + if (length(self$drawings_rels) >= sheet_drawing) { + next_id <- get_next_id(self$drawings_rels[[sheet_drawing]]) + } else { + next_id <- "rId1" + } + ## write file path to media slot to copy across on save tmp <- file names(tmp) <- stri_join("image", mediaNo, ".", imageType) @@ -4688,7 +4694,7 @@ wbWorkbook <- R6::R6Class( '', pos, sprintf('', width, height), - genBasePic(imageNo), + genBasePic(imageNo, next_id), "", "" ) @@ -4722,8 +4728,8 @@ wbWorkbook <- R6::R6Class( self$drawings_rels[[sheet_drawing]] <- c( old_drawings_rels, sprintf( - '', - imageNo, + '', + next_id, mediaNo, imageType ) diff --git a/R/class-worksheet.R b/R/class-worksheet.R index 975514268..79230cc42 100644 --- a/R/class-worksheet.R +++ b/R/class-worksheet.R @@ -450,10 +450,11 @@ wbWorksheet <- R6::R6Class( out <- NULL for (i in seq_len(nrow(col_df))) { z <- col_df[i, ] - for (j in seq(z$min, z$max)) { - z$key <- j - out <- rbind(out, z) - } + keys <- seq.int(z$min, z$max) + + out_tmp <- z[rep(1L, length(keys)), ] + out_tmp$key <- keys + out <- rbind(out, out_tmp) } # merge and convert to character, remove key diff --git a/inst/WORDLIST b/inst/WORDLIST index 7e24d3ed2..34b86571f 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -165,6 +165,7 @@ pageBreakPreview pageLayout pageSetup paperSize +params patternFill pcdata ph diff --git a/tests/testthat/test-class-workbook.R b/tests/testthat/test-class-workbook.R index a7f8ea141..1bf4978ec 100644 --- a/tests/testthat/test-class-workbook.R +++ b/tests/testthat/test-class-workbook.R @@ -63,6 +63,14 @@ test_that("wb_set_col_widths", { got <- wb$worksheets[[1]]$cols_attr expect_equal(exp, got) + wb <- wb_workbook()$add_worksheet() + wb$worksheets[[1]]$cols_attr <- c( + "", + "" + ) + + expect_silent(wb$set_col_widths(cols = 19, width = 9)) + })