Skip to content

Commit 7df72d0

Browse files
authored
[pt] disable subtotals (#913)
* [pt] disable subtotals * [pt] fix extLst logic
1 parent a5282f1 commit 7df72d0

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

R/class-workbook.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,10 @@ wbWorkbook <- R6::R6Class(
14301430

14311431
varfun <- data[sel]
14321432

1433+
if (is.null(names(varfun))) {
1434+
stop("Unknown variable in data argument: ", data)
1435+
}
1436+
14331437
for (var in varfun) {
14341438
if (all(grepl("^=", names(varfun)))) {
14351439
x[[var]] <- names(varfun[varfun == var])
@@ -1603,7 +1607,7 @@ wbWorkbook <- R6::R6Class(
16031607
showMissing = showMissing,
16041608
crossFilter = crossFilter
16051609
),
1606-
xml_children = get_items(x, which(names(x) == slicer), NULL, slicer = TRUE, choose = choo)
1610+
xml_children = get_items(x, which(names(x) == slicer), NULL, slicer = TRUE, choose = choo, has_default = TRUE)
16071611
)
16081612

16091613
slicer_cache <- read_xml(sprintf(

R/helper-functions.R

+55-8
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,14 @@ pivot_def_rel <- function(n) sprintf("<Relationships xmlns=\"http://schemas.open
667667

668668
pivot_xml_rels <- function(n) sprintf("<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition\" Target=\"../pivotCache/pivotCacheDefinition%s.xml\"/></Relationships>", n)
669669

670-
get_items <- function(data, x, item_order, slicer = FALSE, choose = NULL) {
670+
get_items <- function(data, x, item_order, slicer = FALSE, choose = NULL, has_default = TRUE) {
671671
x <- abs(x)
672672

673673
dat <- distinct(data[[x]])
674674

675+
default <- NULL
676+
if (has_default) default <- "default"
677+
675678
# check length, otherwise a certain spreadsheet software simply dies
676679
if (!is.null(item_order)) {
677680

@@ -725,7 +728,7 @@ get_items <- function(data, x, item_order, slicer = FALSE, choose = NULL) {
725728
USE.NAMES = FALSE
726729
)
727730
} else {
728-
vals <- c(item_order - 1L, "default")
731+
vals <- c(as.character(item_order - 1L), default)
729732
item <- sapply(
730733
seq_along(vals),
731734
# # TODO this sets the order of the pivot elements
@@ -842,7 +845,8 @@ create_pivot_table <- function(
842845
"show_multiple_label", "show_row_headers", "show_row_stripes",
843846
"sort_col", "sort_item", "sort_row", "subtotal_hidden_items",
844847
"table_style", "tag", "use_auto_formatting", "vacated_style",
845-
"visual_totals"
848+
"visual_totals",
849+
"subtotal_top", "default_subtotal"
846850
)
847851
params <- standardize_case_names(params, arguments = arguments, return = TRUE)
848852

@@ -855,6 +859,22 @@ create_pivot_table <- function(
855859
if (!is.null(params$outline))
856860
outline <- params$outline
857861

862+
subtotalTop <- NULL
863+
SubtotalsOnTopDefault <- NULL
864+
if (!is.null(params$subtotal_top)) {
865+
subtotalTop <- as_xml_attr(params$subtotal_top)
866+
SubtotalsOnTopDefault <- as_xml_attr(params$subtotal_top)
867+
}
868+
869+
has_default <- TRUE
870+
defaultSubtotal <- NULL
871+
EnabledSubtotalsDefault <- NULL
872+
if (!is.null(params$default_subtotal)) {
873+
has_default <- params$default_subtotal
874+
defaultSubtotal <- as_xml_attr(has_default)
875+
EnabledSubtotalsDefault <- as_xml_attr(has_default)
876+
}
877+
858878
for (i in seq_along(x)) {
859879

860880
dataField <- NULL
@@ -917,8 +937,14 @@ create_pivot_table <- function(
917937
multi <- if (is.null(choose)) NULL else as_xml_attr(TRUE)
918938

919939
attrs <- c(
920-
axis, dataField, showAll = "0", multipleItemSelectionAllowed = multi, sortType = sort,
921-
compact = as_xml_attr(compact), outline = as_xml_attr(outline)
940+
axis, dataField,
941+
subtotalTop = subtotalTop,
942+
showAll = "0",
943+
defaultSubtotal = defaultSubtotal,
944+
multipleItemSelectionAllowed = multi,
945+
sortType = sort,
946+
compact = as_xml_attr(compact),
947+
outline = as_xml_attr(outline)
922948
)
923949

924950
if (is_formula) {
@@ -947,7 +973,7 @@ create_pivot_table <- function(
947973
tmp <- xml_node_create(
948974
"pivotField",
949975
xml_attributes = attrs,
950-
xml_children = paste0(paste0(get_items(x, i, sort_itm, FALSE, choo), collapse = ""), autoSortScope))
976+
xml_children = paste0(paste0(get_items(x, i, sort_itm, FALSE, choo, has_default), collapse = ""), autoSortScope))
951977
}
952978

953979
pivotField <- c(pivotField, tmp)
@@ -1136,6 +1162,27 @@ create_pivot_table <- function(
11361162
if (!is.null(params$name))
11371163
pivot_table_name <- params$name
11381164

1165+
1166+
ptd16 <- xml_node_create(
1167+
"xpdl:pivotTableDefinition16",
1168+
xml_attributes = c(
1169+
EnabledSubtotalsDefault = EnabledSubtotalsDefault,
1170+
SubtotalsOnTopDefault = SubtotalsOnTopDefault
1171+
)
1172+
)
1173+
1174+
extLst <- sprintf(
1175+
'<extLst>
1176+
<ext uri="{962EF5D1-5CA2-4c93-8EF4-DBF5C05439D2}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">
1177+
<x14:pivotTableDefinition hideValuesRow="1" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main" />
1178+
</ext>
1179+
<ext uri="{747A6164-185A-40DC-8AA5-F01512510D54}" xmlns:xpdl="http://schemas.microsoft.com/office/spreadsheetml/2016/pivotdefaultlayout">
1180+
%s
1181+
</ext>
1182+
</extLst>',
1183+
ptd16
1184+
)
1185+
11391186
xml_node_create(
11401187
"pivotTableDefinition",
11411188
xml_attributes = c(
@@ -1220,8 +1267,8 @@ create_pivot_table <- function(
12201267
colsFields,
12211268
pageFields,
12221269
dataFields,
1223-
pivotTableStyleInfo
1224-
# extLst
1270+
pivotTableStyleInfo,
1271+
extLst
12251272
)
12261273
)
12271274

0 commit comments

Comments
 (0)