Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fill merged cells #441

Merged
merged 2 commits into from
Nov 26, 2022
Merged

Fix fill merged cells #441

merged 2 commits into from
Nov 26, 2022

Conversation

sjewo
Copy link
Contributor

@sjewo sjewo commented Nov 26, 2022

Hi @JanMarvin,

i found a minor problem while reading merged cells. If the merged area only contains NA values the import fails with an error. I saw this error in combination with startRow (after selecting a different startRow the merged cells became all NA)

library(openxlsx2)
library(testthat)

wb <- wb_workbook()
wb$add_worksheet("sheet1")
wb$add_data(1, t(matrix(c(1:3, NA_real_), 4, 4)), startRow = 3, startCol = 1, colNames = FALSE)

wb$merge_cells(1, rows = 1:4, cols = 4)

tmp_file <- temp_xlsx()
wb_save(wb, tmp_file)

# runs fine
read_xlsx(tmp_file, fillMergedCells = FALSE)

# Error in x[[jj]][iseq] <- vjj : replacement has length zero
read_xlsx(tmp_file, fillMergedCells = TRUE)

A simple solution is checking if the merged cells are all NA and skip this cell range. I created a pull request with a possible solution and a new test.

Reading a file fails with "Error in x[[jj]][iseq] <- vjj : replacement has length zero" if a merged cell range only contains NA values.
@JanMarvin JanMarvin linked an issue Nov 26, 2022 that may be closed by this pull request
@JanMarvin
Copy link
Owner

Thanks, this might be related to #324. I never got to recreate the issue myself. Assumed that it was caused by merged cells overlapping into the requested region. Like every cell in row 1 is filled and a merged cell with column 2. Column 2 is empty, but the user requests to skip column 1 and fillMergedCells = TRUE.

Anyhow, nice catch and thanks for the PR :)

@JanMarvin JanMarvin merged commit f0b89ea into JanMarvin:main Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants