Skip to content

Commit

Permalink
[sparklines] fix logic and extend attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jan 29, 2024
1 parent f0a0413 commit be118e2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
41 changes: 34 additions & 7 deletions R/helper-functions.R
Expand Up @@ -370,13 +370,23 @@ hashPassword <- function(password) {
#' @param first highlight first value
#' @param last highlight last value
#' @param color_series colorSeries
#' @param color_segative colorNegative
#' @param color_negative colorNegative
#' @param color_axis colorAxis
#' @param color_markers colorMarkers
#' @param color_first colorFirst
#' @param color_last colorLast
#' @param color_high colorHigh
#' @param color_how colorLow
#' @param color_low colorLow
#' @param manual_max manualMax
#' @param manual_min manualMin
#' @param line_weight lineWeight
#' @param date_axis dateAxis
#' @param display_x_axis displayXAxis
#' @param display_hidden displayHidden
#' @param min_axis_type minAxisType
#' @param max_axis_type maxAxisType
#' @param right_to_left rightToLeft
#' @param ... additional arguments
#' @return A string containing XML code
#' @examples
#' # create sparklineGroup
Expand Down Expand Up @@ -416,6 +426,15 @@ create_sparklines <- function(
color_last = wb_color(hex = "FFD00000"),
color_high = wb_color(hex = "FFD00000"),
color_low = wb_color(hex = "FFD00000"),
manual_max = NULL,
manual_min = NULL,
line_weight = NULL,
date_axis = NULL,
display_x_axis = NULL,
display_hidden = NULL,
min_axis_type = NULL,
max_axis_type = NULL,
right_to_left = NULL,
...
) {

Expand All @@ -427,22 +446,30 @@ create_sparklines <- function(
if (!is.null(type) && !type %in% c("stacked", "column"))
stop("type must be NULL, stacked or column")

if (!is.null(markers) && is.null(type))
stop("markers only work with stacked or column")

if (!is.null(markers) && as_xml_attr(markers) == "" && !is.null(type) && type %in% c("stacked", "column"))
stop("markers only affect lines `type = NULL`, not stacked or column")

sparklineGroup <- xml_node_create(
"x14:sparklineGroup",
xml_attributes = c(
type = type,
type = type,
displayEmptyCellsAs = as_xml_attr(display_empty_cells_as),
markers = as_xml_attr(markers),
high = as_xml_attr(high),
low = as_xml_attr(low),
first = as_xml_attr(first),
last = as_xml_attr(last),
negative = as_xml_attr(negative),
"xr2:uid" = sprintf("{6F57B887-24F1-C14A-942C-%s}", random_string(length = 12, pattern = "[A-F0-9]"))
manualMin = as_xml_attr(manual_min),
manualMax = as_xml_attr(manual_max),
lineWeight = as_xml_attr(line_weight),
dateAxis = as_xml_attr(date_axis),
displayXAxis = as_xml_attr(display_x_axis),
displayHidden = as_xml_attr(display_hidden),
minAxisType = as_xml_attr(min_axis_type),
maxAxisType = as_xml_attr(max_axis_type),
rightToLeft = as_xml_attr(right_to_left),
"xr2:uid" = sprintf("{6F57B887-24F1-C14A-942C-%s}", random_string(length = 12, pattern = "[A-F0-9]"))
),
xml_children = c(
xml_node_create("x14:colorSeries", xml_attributes = color_series),
Expand Down
33 changes: 31 additions & 2 deletions man/create_sparklines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/test-helper-functions.R
Expand Up @@ -206,19 +206,19 @@ test_that("add_sparklines", {

test_that("more sparkline tests", {

sl1 <- create_sparklines("Sheet 1", "A3:K3", "L3")
sl2 <- create_sparklines("Sheet 1", "A4:K4", "L4", type = "column", markers = TRUE, high = TRUE, low = TRUE)
sl3 <- create_sparklines("Sheet 1", "A5:K5", "L5", type = "stacked", markers = TRUE, display_empty_cells_as = 0)

set.seed(123) # sparklines has a random uri string
options("openxlsx2_seed" = NULL)

sl1 <- create_sparklines("Sheet 1", "A3:K3", "L3")
sl2 <- create_sparklines("Sheet 1", "A4:K4", "L4", type = "column", high = TRUE, low = TRUE)
sl3 <- create_sparklines("Sheet 1", "A5:K5", "L5", type = "stacked", display_empty_cells_as = 0)

wb <- wb_workbook() %>%
wb_add_worksheet() %>%
wb_add_data(x = mtcars) %>%
wb_add_sparklines(sparklines = c(sl1, sl2, sl3))

exp <- "<ext xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\" uri=\"{05C60535-1F16-4fd2-B633-F4F36F0B64E0}\"><x14:sparklineGroups xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\"><x14:sparklineGroup displayEmptyCellsAs=\"gap\" xr2:uid=\"{6F57B887-24F1-C14A-942C-4C6EF08E87F7}\"><x14:colorSeries rgb=\"FF376092\"/><x14:colorNegative rgb=\"FFD00000\"/><x14:colorAxis rgb=\"FFD00000\"/><x14:colorMarkers rgb=\"FFD00000\"/><x14:colorFirst rgb=\"FFD00000\"/><x14:colorLast rgb=\"FFD00000\"/><x14:colorHigh rgb=\"FFD00000\"/><x14:colorLow rgb=\"FFD00000\"/><x14:sparklines><x14:sparkline><xm:f>'Sheet 1'!A3:K3</xm:f><xm:sqref>L3</xm:sqref></x14:sparkline></x14:sparklines></x14:sparklineGroup><x14:sparklineGroup type=\"column\" displayEmptyCellsAs=\"gap\" markers=\"1\" high=\"1\" low=\"1\" xr2:uid=\"{6F57B887-24F1-C14A-942C-459E3EFAA032}\"><x14:colorSeries rgb=\"FF376092\"/><x14:colorNegative rgb=\"FFD00000\"/><x14:colorAxis rgb=\"FFD00000\"/><x14:colorMarkers rgb=\"FFD00000\"/><x14:colorFirst rgb=\"FFD00000\"/><x14:colorLast rgb=\"FFD00000\"/><x14:colorHigh rgb=\"FFD00000\"/><x14:colorLow rgb=\"FFD00000\"/><x14:sparklines><x14:sparkline><xm:f>'Sheet 1'!A4:K4</xm:f><xm:sqref>L4</xm:sqref></x14:sparkline></x14:sparklines></x14:sparklineGroup><x14:sparklineGroup type=\"stacked\" displayEmptyCellsAs=\"0\" markers=\"1\" xr2:uid=\"{6F57B887-24F1-C14A-942C-2B92FF2D7883}\"><x14:colorSeries rgb=\"FF376092\"/><x14:colorNegative rgb=\"FFD00000\"/><x14:colorAxis rgb=\"FFD00000\"/><x14:colorMarkers rgb=\"FFD00000\"/><x14:colorFirst rgb=\"FFD00000\"/><x14:colorLast rgb=\"FFD00000\"/><x14:colorHigh rgb=\"FFD00000\"/><x14:colorLow rgb=\"FFD00000\"/><x14:sparklines><x14:sparkline><xm:f>'Sheet 1'!A5:K5</xm:f><xm:sqref>L5</xm:sqref></x14:sparkline></x14:sparklines></x14:sparklineGroup></x14:sparklineGroups></ext>"
exp <- "<ext xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\" uri=\"{05C60535-1F16-4fd2-B633-F4F36F0B64E0}\"><x14:sparklineGroups xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\"><x14:sparklineGroup displayEmptyCellsAs=\"gap\" xr2:uid=\"{6F57B887-24F1-C14A-942C-4C6EF08E87F7}\"><x14:colorSeries rgb=\"FF376092\"/><x14:colorNegative rgb=\"FFD00000\"/><x14:colorAxis rgb=\"FFD00000\"/><x14:colorMarkers rgb=\"FFD00000\"/><x14:colorFirst rgb=\"FFD00000\"/><x14:colorLast rgb=\"FFD00000\"/><x14:colorHigh rgb=\"FFD00000\"/><x14:colorLow rgb=\"FFD00000\"/><x14:sparklines><x14:sparkline><xm:f>'Sheet 1'!A3:K3</xm:f><xm:sqref>L3</xm:sqref></x14:sparkline></x14:sparklines></x14:sparklineGroup><x14:sparklineGroup type=\"column\" displayEmptyCellsAs=\"gap\" high=\"1\" low=\"1\" xr2:uid=\"{6F57B887-24F1-C14A-942C-459E3EFAA032}\"><x14:colorSeries rgb=\"FF376092\"/><x14:colorNegative rgb=\"FFD00000\"/><x14:colorAxis rgb=\"FFD00000\"/><x14:colorMarkers rgb=\"FFD00000\"/><x14:colorFirst rgb=\"FFD00000\"/><x14:colorLast rgb=\"FFD00000\"/><x14:colorHigh rgb=\"FFD00000\"/><x14:colorLow rgb=\"FFD00000\"/><x14:sparklines><x14:sparkline><xm:f>'Sheet 1'!A4:K4</xm:f><xm:sqref>L4</xm:sqref></x14:sparkline></x14:sparklines></x14:sparklineGroup><x14:sparklineGroup type=\"stacked\" displayEmptyCellsAs=\"0\" xr2:uid=\"{6F57B887-24F1-C14A-942C-2B92FF2D7883}\"><x14:colorSeries rgb=\"FF376092\"/><x14:colorNegative rgb=\"FFD00000\"/><x14:colorAxis rgb=\"FFD00000\"/><x14:colorMarkers rgb=\"FFD00000\"/><x14:colorFirst rgb=\"FFD00000\"/><x14:colorLast rgb=\"FFD00000\"/><x14:colorHigh rgb=\"FFD00000\"/><x14:colorLow rgb=\"FFD00000\"/><x14:sparklines><x14:sparkline><xm:f>'Sheet 1'!A5:K5</xm:f><xm:sqref>L5</xm:sqref></x14:sparkline></x14:sparklines></x14:sparklineGroup></x14:sparklineGroups></ext>"
got <- wb$worksheets[[1]]$extLst
expect_equal(exp, got)

Expand Down

0 comments on commit be118e2

Please sign in to comment.