openxlsx icon indicating copy to clipboard operation
openxlsx copied to clipboard

Error loading and saving workbook with filters

Open ColinTB opened this issue 6 years ago • 1 comments

Expected Behavior

When loading a workbook with filters and then resaving the workbook the file should not become corrupt. I think it might be simplest to just have the filter removed, rather than attempt to apply to filter to possibly new data.

Actual Behavior

File becomes corrupt and any sheets with filters appear blank.

These are the excel corruption warning when trying to open the file.

image

image

Steps to Reproduce the Problem

  1. Set path wb_path <- "PATH TO FOLDER/test.xlsx"

  2. Create simple workbook wb <- createWorkbook() addWorksheet(wb, sheetName = "test") writeData(wb, sheet = "test", data.frame(a = 1:2)) saveWorkbook(wb,file = wb_path, overwrite = T)

  3. Open the workbook, add a filter to Column A and from the dropdown filter for for only 2

  4. Load the worbook and try to update the data on the tab with the filter wb <- loadWorkbook(wb_path) writeData(wb, sheet = "test", data.frame(a = 1:3)) saveWorkbook(wb,file = wb_path, overwrite = T)

  5. When I try to open the update workbook I get the warnings shared above and the "test" tab is now blank (Example corrupted workbook)

sessionInfo()

R version 3.4.2 (2017-09-28) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

other attached packages: [1] openxlsx_4.1.0

ColinTB avatar Mar 14 '19 17:03 ColinTB

I wish there were a proper solution, but I've found a workaround. Load the workbook and call openxlsx::removeFilter(wb, sheet = <sheets_with_filters>) before proceeding.

mschwartsman avatar Apr 12 '19 18:04 mschwartsman