Skip to content

Commit

Permalink
[write_xlsx] restore first_active_{col,row} and first_{col,row}
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jan 20, 2024
1 parent 64f48bf commit 6a4e73e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
24 changes: 12 additions & 12 deletions R/write_xlsx.R
Expand Up @@ -389,35 +389,35 @@ 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)
}
}

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)
}
}

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)
}
}

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)
Expand All @@ -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]
)
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-save.R
Expand Up @@ -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(

Check warning on line 369 in tests/testthat/test-save.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-save.R,line=369,col=7,[infix_spaces_linter] Put spaces around all infix operators.
"<sheetPr><tabColor rgb=\"FF00FF00\"/></sheetPr>",
"<sheetPr><tabColor rgb=\"FF00FF00\"/></sheetPr>"
)
got <- c(
wb$worksheets[[1]]$sheetPr,
wb$worksheets[[2]]$sheetPr
)
expect_equal(exp, got)

# firstCol/firstRow
exp <- c(
"<pane ySplit=\"1\" xSplit=\"1\" topLeftCell=\"B2\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>",
"<pane ySplit=\"1\" xSplit=\"1\" topLeftCell=\"B2\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>"
)
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(
"<pane ySplit=\"3\" xSplit=\"2\" topLeftCell=\"C4\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>",
"<pane ySplit=\"3\" xSplit=\"2\" topLeftCell=\"C4\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>"
)
got <- c(
wb$worksheets[[1]]$freezePane,
wb$worksheets[[2]]$freezePane
)
expect_equal(exp, got)

})

0 comments on commit 6a4e73e

Please sign in to comment.