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

[pivot tables] add show_data_as and compact/outline #833

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# openxlsx2 (development version)

* Add new params to `wb_add_pivot_table()`. It is now possible to set the `show_data_as` value and set a tabular table design. [833](https://github.com/JanMarvin/openxlsx2/pull/833)


***************************************************************************

Expand Down
3 changes: 3 additions & 0 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ wb_add_data_table <- function(
#' `fun` can be any of `AVERAGE`, `COUNT`, `COUNTA`, `MAX`, `MIN`,
#' `PRODUCT`, `STDEV`, `STDEVP`, `SUM`, `VAR`, `VARP`.
#'
#' `show_data_as` can be any of `normal`, `difference`, `percent`, `percentDiff`,
#' `runTotal`, `percentOfRow`, `percentOfCol`, `percentOfTotal`, `index`.
#'
#' The sheet will be empty unless it is opened in spreadsheet software.
#'
#' @param wb A Workbook object containing a #' worksheet.
Expand Down
36 changes: 25 additions & 11 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,15 @@ create_pivot_table <- function(
}

pivotField <- NULL

compact <- ""
if (!is.null(params$compact))
compact <- params$compact

outline <- ""
if (!is.null(params$outline))
outline <- params$outline

for (i in seq_along(x)) {

dataField <- NULL
Expand Down Expand Up @@ -814,7 +823,10 @@ create_pivot_table <- function(
else sort <- "ascending"
}

attrs <- c(axis, dataField, showAll = "0", sortType = sort)
attrs <- c(
axis, dataField, showAll = "0", sortType = sort,
compact = as_xml_attr(compact), outline = as_xml_attr(outline)
)

tmp <- xml_node_create(
"pivotField",
Expand Down Expand Up @@ -879,6 +891,11 @@ create_pivot_table <- function(
if (use_data) {

dataField <- NULL

show_data_as <- rep("", length(data))
if (!is.null(params$show_data_as))
show_data_as <- params$show_data_as

for (i in seq_along(data)) {

if (missing(fun)) fun <- NULL
Expand All @@ -888,12 +905,13 @@ create_pivot_table <- function(
xml_node_create(
"dataField",
xml_attributes = c(
name = sprintf("%s of %s", ifelse(is.null(fun[i]), "Sum", fun[i]), data[i]),
fld = sprintf("%s", data_pos[i] - 1L),
subtotal = fun[i],
baseField = "0",
baseItem = "0",
numFmtId = numfmts[i]
name = sprintf("%s of %s", ifelse(is.null(fun[i]), "Sum", fun[i]), data[i]),
fld = sprintf("%s", data_pos[i] - 1L),
showDataAs = as_xml_attr(ifelse(is.null(show_data_as[i]), "", show_data_as[i])),
subtotal = fun[i],
baseField = "0",
baseItem = "0",
numFmtId = numfmts[i]
)
)
)
Expand Down Expand Up @@ -986,10 +1004,6 @@ create_pivot_table <- function(
if (!is.null(params$multipleFieldFilters))
multipleFieldFilters <- params$multipleFieldFilters

outline <- "1"
if (!is.null(params$outline))
outline <- params$outline

outlineData <- "1"
if (!is.null(params$outlineData))
outlineData <- params$outlineData
Expand Down
3 changes: 3 additions & 0 deletions man/wb_add_pivot_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading