Skip to content

Commit

Permalink
[xlsb] add custom sheet view (#1241)
Browse files Browse the repository at this point in the history
* [xlsb] add custom sheet view

* [xlsb] add xml_order_children() function and add test

* [xlsb] cleanup

* [tests] increase coverage for xml_order_children()

* [load] switch to read_xml2df() and write_df2xml()

* [xlsb] parse header footer attributes

Just for completeness. This wont be used, because when writing headerFooter, we will assign the attributes as required and will not rely on their initial values.

* [xlsb] guid should not be swapped and add customSheet parsed result for chartsheet

* [xlsb] fix and unify
  • Loading branch information
JanMarvin authored Jan 11, 2025
1 parent e63a038 commit 828b6cd
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 20 deletions.
30 changes: 30 additions & 0 deletions R/wb_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,36 @@ wb_load <- function(
# correct sheet references and replace our replacement with it.
if (!data_only && length(workbookBIN)) {

# we need to update the order of customSheetView children. Incorrect orders
# causes spreadsheet software to be unable to load and recover the file.
for (sheet in seq_along(wb$worksheets)) {
if (length(wb$worksheets[[sheet]]$customSheetViews) == 0) next

cvs <- xml_node(wb$worksheets[[sheet]]$customSheetViews, "customSheetViews", "customSheetView")

# chart sheets have a reduced custom view
exp_attr <- c(
"guid", "scale", "colorId", "showPageBreaks", "showFormulas",
"showGridLines", "showRowCol", "outlineSymbols", "zeroValues",
"fitToPage", "printArea", "filter", "showAutoFilter", "hiddenRows",
"hiddenColumns", "state", "filterUnique", "view", "showRuler",
"topLeftCell", "zoomToFit"
)
exp_nams <- c(
"pane", "selection", "rowBreaks", "colBreaks", "pageMargins",
"printOptions", "pageSetup", "headerFooter", "autoFilter", "extLst"
)
cv <- read_xml2df(read_xml(cvs), "customSheetView", vec_attrs = exp_attr, vec_chlds = exp_nams)

# headerFooter cause issues. they are (a) not added to the correct node
# and (b) brick the entire XML structure
cv$headerFooter <- ""

cvs <- write_df2xml(cv[c(exp_attr, exp_nams)], "customSheetView", vec_attrs = exp_attr, vec_chlds = exp_nams)

wb$worksheets[[sheet]]$customSheetViews <- xml_node_create("customSheetViews", xml_children = cvs)
}

if (length(wb$workbook$xti)) {
# create data frame containing sheet names for Xti entries
xti <- rbindlist(xml_attr(wb$workbook$xti, "xti"))
Expand Down
Loading

0 comments on commit 828b6cd

Please sign in to comment.