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

[pt] calculate pivot table field with formula #892

Merged
merged 2 commits into from
Jan 14, 2024
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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: openxlsx2
Title: Read, Write and Edit 'xlsx' Files
Version: 1.3
Version: 1.3.0.9000
Language: en-US
Authors@R: c(
person("Jordan Mark", "Barbone", email = "[email protected]", role = "aut", comment = c(ORCID = "0000-0001-9788-3628")),
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# openxlsx2 (development version)

## New features

* Experimental support for calculation of pivot table fields. [892](https://github.com/JanMarvin/openxlsx2/pull/892)


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


# openxlsx2 1.3

## Documentation improvement
Expand Down
4 changes: 4 additions & 0 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ wb_add_data_table <- function(
#' `show_data_as` can be any of `normal`, `difference`, `percent`, `percentDiff`,
#' `runTotal`, `percentOfRow`, `percentOfCol`, `percentOfTotal`, `index`.
#'
#' It is possible to calculate data fields if the formula is assigned as a
#' variable name for the field to calculate. This would look like this:
#' `data = c("am", "disp/cyl" = "New")`
#'
#' Possible `params` arguments are listed below. Pivot tables accepts more
#' parameters, but they were either not tested or misbehaved (probably because
#' we misunderstood how the parameter should be used).
Expand Down
16 changes: 16 additions & 0 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,22 @@ wbWorkbook <- R6::R6Class(
if (is.null(params$name) && !is.null(pivot_table))
params$name <- pivot_table

# not sure if rows & cols can be formulas too
if (any(sel <- !data %in% names(x))) {

varfun <- data[sel]

for (var in varfun) {
if (all(grepl("^=", names(varfun)))) {
x[[var]] <- names(varfun[varfun == var])
class(x[[var]]) <- c("is_formula", "character")
} else {
stop("missing variable found in pivot table: data object. Formula names must begin with '='.")
}
}

}

pivot_table <- create_pivot_table(
x = x,
dims = dims,
Expand Down
27 changes: 25 additions & 2 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,9 @@ cacheFields <- function(wbdata, filter, rows, cols, data, slicer) {
dat <- format(dat, format = "%Y-%m-%dT%H:%M:%S")
}

if (is_vars && !is_data) {
is_formula <- inherits(wbdata[[x]], what = "is_formula")

if (is_vars && !is_data && !is_formula) {
sharedItem <- vapply(
distinct(dat),
function(uni) {
Expand Down Expand Up @@ -594,9 +596,18 @@ cacheFields <- function(wbdata, filter, rows, cols, data, slicer) {
xml_children = sharedItem
)

formula <- NULL
databaseField <- NULL

if (is_formula) {
formula <- gsub("^=", "", names(vars[vars == x]))
databaseField <- "0"
sharedItems <- NULL
}

xml_node_create(
"cacheField",
xml_attributes = c(name = x, numFmtId = "0"),
xml_attributes = c(name = x, numFmtId = "0", formula = formula, databaseField = databaseField),
xml_children = sharedItems
)
},
Expand Down Expand Up @@ -814,6 +825,7 @@ create_pivot_table <- function(
axis <- NULL
sort <- NULL
autoSortScope <- NULL
is_formula <- inherits(x[[i]], "is_formula")

if (i %in% data_pos) dataField <- c(dataField = "1")

Expand Down Expand Up @@ -873,6 +885,17 @@ create_pivot_table <- function(
compact = as_xml_attr(compact), outline = as_xml_attr(outline)
)

if (is_formula) {
attrs <- c(
dataField = "1",
dragToRow = "0",
dragToCol = "0",
dragToPage = "0",
showAll = "0",
defaultSubtotal = "0"
)
}

tmp <- xml_node_create(
"pivotField",
xml_attributes = attrs)
Expand Down
4 changes: 4 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