From 463b4af9d5f022dab599277ec8f9b399b175794a Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Sat, 20 Jan 2024 13:43:59 +0100 Subject: [PATCH] [write_xlsx] restore first_active_{col,row} and first_{col,row} --- R/write_xlsx.R | 24 ++++++++++----------- tests/testthat/test-save.R | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/R/write_xlsx.R b/R/write_xlsx.R index a9b1e3fa6..601c208c1 100644 --- a/R/write_xlsx.R +++ b/R/write_xlsx.R @@ -389,8 +389,8 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) { freeze_pane <- FALSE firstActiveRow <- rep_len(1L, length.out = nSheets) - if ("firstActiveRow" %in% names(params)) { - firstActiveRow <- params$firstActiveRow + if ("first_active_row" %in% names(params)) { + firstActiveRow <- params$first_active_row freeze_pane <- TRUE if (length(firstActiveRow) != nSheets) { firstActiveRow <- rep_len(firstActiveRow, length.out = nSheets) @@ -398,8 +398,8 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) { } firstActiveCol <- rep_len(1L, length.out = nSheets) - if ("firstActiveCol" %in% names(params)) { - firstActiveCol <- params$firstActiveCol + if ("first_active_col" %in% names(params)) { + firstActiveCol <- params$first_active_col freeze_pane <- TRUE if (length(firstActiveCol) != nSheets) { firstActiveCol <- rep_len(firstActiveCol, length.out = nSheets) @@ -407,8 +407,8 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) { } firstRow <- rep_len(FALSE, length.out = nSheets) - if ("firstRow" %in% names(params)) { - firstRow <- params$firstRow + if ("first_row" %in% names(params)) { + firstRow <- params$first_row freeze_pane <- TRUE if (inherits(x, "list") && (length(firstRow) != nSheets)) { firstRow <- rep_len(firstRow, length.out = nSheets) @@ -416,8 +416,8 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) { } firstCol <- rep_len(FALSE, length.out = nSheets) - if ("firstCol" %in% names(params)) { - firstCol <- params$firstCol + if ("first_col" %in% names(params)) { + firstCol <- params$first_col freeze_pane <- TRUE if (inherits(x, "list") && (length(firstCol) != nSheets)) { firstCol <- rep_len(firstCol, length.out = nSheets) @@ -430,10 +430,10 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) { wb <- wb_freeze_pane( wb = wb, sheet = i, - firstActiveRow = firstActiveRow[i], - firstActiveCol = firstActiveCol[i], - firstRow = firstRow[i], - firstCol = firstCol[i] + first_active_row = firstActiveRow[i], + first_active_col = firstActiveCol[i], + first_row = firstRow[i], + first_col = firstCol[i] ) } } diff --git a/tests/testthat/test-save.R b/tests/testthat/test-save.R index 0894f0df3..a91e8d0ff 100644 --- a/tests/testthat/test-save.R +++ b/tests/testthat/test-save.R @@ -358,3 +358,46 @@ test_that("write_xlsx() works", { expect_equal(exp, got) }) + +test_that("write_xlsx() freezing rows works", { + + tmp <- temp_xlsx() + + wb <- write_xlsx(list(mtcars, mtcars), tmp, firstRow = TRUE, firstCol = TRUE, tab_color = wb_color("green")) + + # tabColor + exp <- c( + "", + "" + ) + got <- c( + wb$worksheets[[1]]$sheetPr, + wb$worksheets[[2]]$sheetPr + ) + expect_equal(exp, got) + + # firstCol/firstRow + exp <- c( + "", + "" + ) + got <- c( + wb$worksheets[[1]]$freezePane, + wb$worksheets[[2]]$freezePane + ) + expect_equal(exp, got) + + wb <- write_xlsx(list(mtcars, mtcars), tmp, firstActiveRow = 4, firstActiveCol = 3) + + # firstActiveCol/firstActiveRow + exp <- c( + "", + "" + ) + got <- c( + wb$worksheets[[1]]$freezePane, + wb$worksheets[[2]]$freezePane + ) + expect_equal(exp, got) + +})