Skip to content

Commit

Permalink
Merge pull request #468 from StevenHibble-Concurrence/master
Browse files Browse the repository at this point in the history
Add `pair_rc()` to improve row/column pair lookups
  • Loading branch information
JanMarvin authored Aug 11, 2024
2 parents 4dd2774 + 1a6acb6 commit dcd992d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
20 changes: 11 additions & 9 deletions R/WorkbookClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,7 @@ Workbook$methods(
}


prev_sheet <- 0L
for (x in styleObjects) {
if (length(x$rows) > 0 & length(x$cols) > 0) {
this.sty <- x$style$copy()
Expand All @@ -3416,12 +3417,13 @@ Workbook$methods(
sId <-
.self$updateStyles(this.sty) ## this creates the XML for styles.XML

cells_to_style <- stri_join(x$rows, x$cols, sep = ",")
existing_cells <-
stri_join(worksheets[[sheet]]$sheet_data$rows,
worksheets[[sheet]]$sheet_data$cols,
sep = ","
)
cells_to_style <- pair_rc(x$rows, x$cols)

# Avoid recreating this if we're looking at the same sheet over and over
if (sheet != prev_sheet) {
existing_cells <- pair_rc(worksheets[[sheet]]$sheet_data$rows,
worksheets[[sheet]]$sheet_data$cols)
}

## In here we create any style_ids that don't yet exist in sheet_data
worksheets[[sheet]]$sheet_data$style_id[existing_cells %in% cells_to_style] <<-
Expand Down Expand Up @@ -3560,9 +3562,9 @@ Workbook$methods(
## toRemove are the elements that the new style doesn't apply to, we remove these from the style object as it
## is copied, merged with the new style and given the new data points

ex_row_cols <-
stri_join(styleObjects[[i]]$rows, styleObjects[[i]]$cols, sep = "-")
new_row_cols <- stri_join(rows, cols, sep = "-")
ex_row_cols <- pair_rc(styleObjects[[i]]$rows,
styleObjects[[i]]$cols)
new_row_cols <- pair_rc(rows, cols)


## mergeInds are the intersection of the two styles that will need to merge
Expand Down
4 changes: 2 additions & 2 deletions R/sheet_data_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Sheet_Data$methods(delete = function(rows_in, cols_in, grid_expand) {
stop("Length of rows and cols must be equal.")
}

inds <- which(paste(rows, cols, sep = ",") %in% paste(rows_in, cols_in, sep = ","))
inds <- which(pair_rc(rows, cols) %in% pair_rc(rows_in, cols_in))

if (length(inds) > 0) { ## writing over existing data

Expand Down Expand Up @@ -80,7 +80,7 @@ Sheet_Data$methods(write = function(rows_in, cols_in, t_in, v_in, f_in, any_func

inds <- integer(0)
if (possible_overlap) {
inds <- which(paste(rows, cols, sep = ",") %in% paste(rows_in, cols_in, sep = ","))
inds <- which(pair_rc(rows, cols) %in% pair_rc(rows_in, cols_in))
}

if (length(inds) > 0) {
Expand Down
7 changes: 7 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ is_true_false <- function(x) {
is.logical(x) && length(x) == 1L && !is.na(x)
}

pair_rc <- function(r, c) {
# Assumes that there can't be more than 10M columns
# Excel allows 16,384
# Google Sheets allows 18,278
r+c*1e-7
}

do_call_params <- function(fun, params, ..., .map = FALSE) {
fun <- match.fun(fun)
call_params <- c(list(...), params[names(params) %in% names(formals(fun))])
Expand Down
5 changes: 3 additions & 2 deletions R/workbook_column_widths.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ Workbook$methods(setColWidths = function(sheet) {
all_merged_cells <- do.call("rbind", all_merged_cells)

## only want the sheet data in here
refs <- paste(all_merged_cells[[1]], all_merged_cells[[2]], sep = ",")
existing_cells <- paste(worksheets[[sheet]]$sheet_data$rows, worksheets[[sheet]]$sheet_data$cols, sep = ",")
refs <- pair_rc(all_merged_cells[[1]], all_merged_cells[[2]])
existing_cells <- pair_rc(worksheets[[sheet]]$sheet_data$rows,
worksheets[[sheet]]$sheet_data$cols)
keep <- which(!existing_cells %in% refs & !is.na(worksheets[[sheet]]$sheet_data$v))

sd <- Sheet_Data$new()
Expand Down
2 changes: 1 addition & 1 deletion R/workbook_read_workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ read.xlsx.Workbook <- function(xlsxFile,
if (length(sO) > 0) {
style_rows <- unlist(lapply(sO, "[[", "rows"))
style_cols <- unlist(lapply(sO, "[[", "cols"))
isDate <- paste(rows, cols, sep = ",") %in% paste(style_rows, style_cols, sep = ",")
isDate <- pair_rc(rows, cols) %in% pair_rc(style_rows, style_cols)

## check numbers are also integers
not_an_integer <- suppressWarnings(as.numeric(v[isDate]))
Expand Down
2 changes: 1 addition & 1 deletion R/wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -4498,7 +4498,7 @@ groupColumns <- function(wb, sheet, cols, hidden = FALSE, level = -1) {
stop("First argument must be a Workbook.")
}

if (any(cols) < 1L) {
if (any(cols < 1L)) {
stop("Invalid columns selected (<= 0).")
}

Expand Down

0 comments on commit dcd992d

Please sign in to comment.