Skip to content

Commit

Permalink
[read] handle wb_to_df() where shared cells origin is outside of the …
Browse files Browse the repository at this point in the history
…selected dims region
  • Loading branch information
JanMarvin committed Jul 23, 2024
1 parent b2b1df9 commit e167661
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,11 @@ wb_to_df <- function(

if (any(cc$f_t == "shared")) {

cc_shared <- cc[cc$f_t == "shared", ]
cc_shared <- cc_shared[order(as.integer(cc$f_si)), ]
cc_shared <- wb$worksheets[[sheet]]$sheet_data$cc
cc_shared <- cc_shared[cc_shared$f_t == "shared", ]
cc_shared <- cc_shared[order(as.integer(cc_shared$f_si)), ]

# extend shared formula into all formula cells
carry_forward <- function(x) {
last_val <- NA
for (i in seq_along(x)) {
Expand All @@ -448,6 +450,7 @@ wb_to_df <- function(
FUN = carry_forward
)

# calculate difference for each shared formula to the origin
calc_distance <- function(x) {
x - x[1]
}
Expand All @@ -458,11 +461,12 @@ wb_to_df <- function(
FUN = calc_distance
)
cc_shared$rows <- ave(
col2int(cc_shared$row_r),
as.integer(cc_shared$row_r),
as.integer(cc_shared$f_si),
FUN = calc_distance
)

# TODO skip non A1 shared cells
cells <- find_a1_notation(cc_shared$f)
repls <- cells

Expand All @@ -474,6 +478,8 @@ wb_to_df <- function(
cc_shared$cols <- NULL
cc_shared$rows <- NULL

# reduce and assign
cc_shared <- cc_shared[which(cc_shared$r %in% cc$r), ]
cc[match(cc_shared$r, cc$r), ] <- cc_shared

}
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-read_from_created_wb.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,20 @@ test_that("check_names works", {
expect_equal(exp, got)

})

test_that("shared formulas are handled", {
wb <- wb_workbook()$
add_worksheet()$
add_data(x = matrix(rnorm(5*5), ncol = 5, nrow = 5))$

Check warning on line 228 in tests/testthat/test-read_from_created_wb.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-read_from_created_wb.R,line=228,col=32,[infix_spaces_linter] Put spaces around all infix operators.
add_formula(x = "SUM($A2:A2) + B$1", dims = "A8:E12", shared = TRUE)

exp <- structure(
c("SUM($A4:A4) + B$1", "SUM($A5:A5) + B$1", "SUM($A6:A6) + B$1",
"SUM($A4:B4) + C$1", "SUM($A5:B5) + C$1", "SUM($A6:B6) + C$1",
"SUM($A4:C4) + D$1", "SUM($A5:C5) + D$1", "SUM($A6:C6) + D$1"),
dim = c(3L, 3L),
dimnames = list(c("10", "11", "12"), c("A", "B", "C"))
)
got <- as.matrix(wb_to_df(wb, dims = "A10:C12", col_names = FALSE, show_formula = TRUE))
expect_equal(exp, got)
})

0 comments on commit e167661

Please sign in to comment.