Skip to content

Commit 1afcef6

Browse files
committed
- Modified so it can be used in the existing workbook that already has a data table with subtotals.
- Seperated Example into another script
1 parent d2a59f4 commit 1afcef6

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

output/subtotal irist sample.xlsx

-9.68 KB
Binary file not shown.

output/subtotal output example.xlsx

13.2 KB
Binary file not shown.

scripts/examples.r

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#-------------------------------------------------------------------------------
3+
# Example
4+
#-------------------------------------------------------------------------------
5+
source("scripts/subtotal.r")
6+
wb <- createWorkbook()
7+
set.seed(1024)
8+
9+
# Function adds data tables with subtotals into existing workbooks
10+
11+
data_with_subtotal(
12+
workbook = wb,
13+
data = sample_n(iris, 20, replace = FALSE),
14+
sheet.name = "data"
15+
)
16+
data_with_subtotal(
17+
workbook = wb,
18+
data = sample_n(mtcars, 20, replace = FALSE),
19+
sheet.name = "data2"
20+
)
21+
22+
# Save Example
23+
saveWorkbook(wb, "output/subtotal output example.xlsx", overwrite = TRUE)

scripts/subtotal.r

+25-26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ data_with_subtotal <- function(workbook, data, sheet.name){
2020
}
2121
return(index)
2222
}
23+
2324

2425
# Excel aggregate function list
2526
function_table <-
@@ -37,13 +38,21 @@ data_with_subtotal <- function(workbook, data, sheet.name){
3738
"VAR",
3839
"VARP"
3940
)
40-
)
41+
)
42+
43+
if(attr(wb, "Class") %>% is.null){
44+
# Number of sheet with subtotals
45+
sheet.counts <- 1
46+
# Set up function list
47+
addWorksheet(wb, "function.list", visible = FALSE)
48+
writeDataTable(wb, "function.list", function_table)
49+
} else {
50+
# Number of sheet with subtotals
51+
sheet.counts <- as.integer((attr(wb, "Class"))[2]) + 1
52+
}
4153

42-
# Set up function list
43-
addWorksheet(wb, "function.list", visible = FALSE)
44-
writeDataTable(wb, "function.list", function_table)
4554
addWorksheet(wb, sheet.name)
46-
table.name <- "data"
55+
table.name <- glue("data{sheet.counts}")
4756
writeDataTable(wb, sheet.name, data, tableName = table.name, stack = TRUE)
4857

4958
# Detect last row and last column
@@ -53,7 +62,8 @@ data_with_subtotal <- function(workbook, data, sheet.name){
5362
for (i in 1:ncol(data)) {
5463
writeFormula(wb,
5564
sheet.name,
56-
glue("=SUBTOTAL(function.list!$C2,data[{names(data)[i]}])"),
65+
glue("=SUBTOTAL(function.list!$C{sheet.counts + 1},",
66+
"{table.name}[{names(data)[i]}])"),
5767
i,
5868
last.row)
5969
}
@@ -71,32 +81,21 @@ data_with_subtotal <- function(workbook, data, sheet.name){
7181
value =
7282
glue("'function.list'!$B$2:$B${nrow(function_table) + 1}")
7383
)
84+
7485
writeData(wb, "function.list", "Detected Function", 3, 1)
7586
writeFormula(
7687
wb,
7788
"function.list",
7889
glue("=INDEX(function.list!$A$2:$A{nrow(function_table) + 1}, ",
79-
"MATCH(data!${excel_col_num(last.col)}${last.row}, ",
90+
"MATCH({sheet.name}!${excel_col_num(last.col)}${last.row}, ",
8091
"function.list!$B$2:$B{nrow(function_table) + 1}, 1))"),
8192
3,
82-
2
93+
sheet.counts + 1
8394
)
84-
setColWidths(wb, sheet.name, cols = 1:ncol(data), widths = "auto")
95+
setColWidths(wb, sheet.name, cols = 1:ncol(data), widths = 20)
96+
97+
#Set attribute
98+
wb <<-
99+
wb %>%
100+
structure(., "Class" = c("subtotal", sheet.counts))
85101
}
86-
87-
#-------------------------------------------------------------------------------
88-
# Example
89-
#-------------------------------------------------------------------------------
90-
91-
wb <- createWorkbook()
92-
set.seed(1024)
93-
94-
# Function allows one to add data table to existing workbook.
95-
data_with_subtotal(
96-
workbook = wb,
97-
data = sample_n(iris, 20, replace = FALSE),
98-
sheet.name = "data"
99-
)
100-
101-
# Save workbook
102-
saveWorkbook(wb, "output/subtotal irist sample.xlsx", overwrite = TRUE)

0 commit comments

Comments
 (0)