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

[pt] calculate pivot table field with formula #892

merged 2 commits into from Jan 14, 2024

Conversation

JanMarvin
Copy link
Owner

library(openxlsx2)

wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_data(x = mtcars)

df <- wb_data(wb, sheet = 1)

wb <- wb %>%
  wb_add_pivot_table(
    df, 
    dims = "A3",
    cols = NULL,
    rows = "vs",
    data = c("disp", "cyl", "=cyl/disp" = "Field1", "=Field1 * _xlfn.UNIQUE(am)" = "Rand"),
    param = list(
      numfmt = c(
        formatCode = c("#,##0.0"),
        formatCode = c("#,##0.0"),
        formatCode = c("0.0%"),
        formatCode = c("0.0%")
      )
    )
  )

if (interactive()) wb$open()
Screenshot 2024-01-13 at 21 40 49

@JanMarvin
Copy link
Owner Author

JanMarvin commented Jan 14, 2024

There’s an option to calculate items as well: https://www.excel-easy.com/examples/calculated-field-item.html
That seems a little non tirivial as it requires something like:

pivotCacheDefinition1.xml:

<sharedItems ...>
  ...
  <s v="ABS_1" f="1"/>
</sharedItems>
...
<calculatedItems count="1">
  <calculatedItem formula=" ABS(vs['1'])">
    <pivotArea cacheIndex="1" outline="0" fieldPosition="0">
      <references count="1">
        <reference field="7" count="1">
          <x v="2"/>
        </reference>
      </references>
    </pivotArea>
  </calculatedItem>
</calculatedItems>

I leave this for somebody else to figure out ...

* begin new development
@JanMarvin JanMarvin merged commit 1f8dffe into main Jan 14, 2024
9 checks passed
@JanMarvin JanMarvin deleted the pt_formula branch January 14, 2024 17:40
@JanMarvin JanMarvin mentioned this pull request Jan 14, 2024
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant