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

Data validation list #347

Merged
merged 4 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Reading from file or workbook with `showFormula` now returns all formulas found in the workbook. Previously only those with type `e` or `str` were returned. Now we will be able to see formulas like hyperlinks too. [352](https://github.com/JanMarvin/openxlsx2/issues/352)

## Internal changes

* Moved data validation list from x14 to data validation. This enables data validation lists without x14 extension [openxlsx 386](https://github.com/ycphs/openxlsx/issues/386). [347](https://github.com/JanMarvin/openxlsx2/pull/347)

## Fixes

* Adding cell styles has been streamlined to increase consistency. This includes all style functions like `wb_add_font()` and covers all cases of hyperlinks. [365](https://github.com/JanMarvin/openxlsx2/pull/365)
Expand Down
45 changes: 17 additions & 28 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -2740,36 +2740,25 @@ wbWorkbook <- R6::R6Class(
)

if (type == "list") {
self$worksheets[[sheet]]$.__enclos_env__$private$data_validation_list(
value = value,
allowBlank = allowBlank,
showInputMsg = showInputMsg,
showErrorMsg = showErrorMsg,
errorStyle = errorStyle,
errorTitle = errorTitle,
error = error,
promptTitle = promptTitle,
prompt = prompt,
sqref = sqref
)
} else {
self$worksheets[[sheet]]$.__enclos_env__$private$data_validation(
type = type,
operator = operator,
value = value,
allowBlank = allowBlank,
showInputMsg = showInputMsg,
showErrorMsg = showErrorMsg,
errorStyle = errorStyle,
errorTitle = errorTitle,
error = error,
promptTitle = promptTitle,
prompt = prompt,
origin = origin,
sqref = sqref
)
operator <- NULL
}

self$worksheets[[sheet]]$.__enclos_env__$private$data_validation(
type = type,
operator = operator,
value = value,
allowBlank = allowBlank,
showInputMsg = showInputMsg,
showErrorMsg = showErrorMsg,
errorStyle = errorStyle,
errorTitle = errorTitle,
error = error,
promptTitle = promptTitle,
prompt = prompt,
origin = origin,
sqref = sqref
)

invisible(self)
},

Expand Down
39 changes: 1 addition & 38 deletions R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ wbWorksheet <- R6::R6Class(
# different ext types have different uri ids. We support dataValidations
# and sparklineGroups.
uri <- ""
if (l_name == "x14:dataValidations") uri <- "{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}"
# if (l_name == "x14:dataValidations") uri <- "{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}"
if (l_name == "x14:sparklineGroups") uri <- "{05C60535-1F16-4fd2-B633-F4F36F0B64E0}"

is_needed_uri <- grepl(pattern = uri, extLst, fixed = TRUE)
Expand Down Expand Up @@ -681,43 +681,6 @@ wbWorksheet <- R6::R6Class(
)

self$append("dataValidations", xml_add_child(header, form))
invisible(self)
},

# data validations list goes to extLst not to worksheet
data_validation_list = function(
value,
allowBlank,
showInputMsg,
showErrorMsg,
errorStyle,
errorTitle,
error,
promptTitle,
prompt,
sqref
) {

data_val <- xml_node_create(
"x14:dataValidation",
xml_attributes = c(
type = "list",
allowBlank = allowBlank,
showInputMessage = showInputMsg,
showErrorMessage = showErrorMsg,
errorStyle = errorStyle,
errorTitle = errorTitle,
error = error,
promptTitle = promptTitle,
prompt = prompt
)
)

formula <- sprintf("<x14:formula1><xm:f>%s</xm:f></x14:formula1>", value)
sqref <- sprintf("<xm:sqref>%s</xm:sqref>", sqref)
xmlData <- xml_add_child(data_val, c(formula, sqref))
private$do_append_x14(xmlData, "x14:dataValidation", "x14:dataValidations")

invisible(self)
}
)
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ test_that("data validation", {


exp <- c(
"<x14:dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\"><x14:formula1><xm:f>'Sheet 4'!$A$1:$A$10</xm:f></x14:formula1><xm:sqref>A2:A31</xm:sqref></x14:dataValidation>"
"<dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\" sqref=\"A2:A31\"><formula1>'Sheet 4'!$A$1:$A$10</formula1></dataValidation>"
)
got <- xml_node(wb$worksheets[[3]]$extLst, "ext", "x14:dataValidations", "x14:dataValidation")
got <- wb$worksheets[[3]]$dataValidations
expect_equal(exp, got)

wb$save(temp)
Expand All @@ -170,18 +170,18 @@ test_that("data validation", {
)

expect_equal(
xml_node(wb$worksheets[[3]]$extLst, "ext", "x14:dataValidations", "x14:dataValidation"),
xml_node(wb2$worksheets[[3]]$extLst, "ext", "x14:dataValidations", "x14:dataValidation")
wb$worksheets[[3]]$dataValidations,
wb2$worksheets[[3]]$dataValidations
)

wb2$add_data_validation("Sheet 3", col = 2, rows = 2:31, type = "list",
value = "'Sheet 4'!$A$1:$A$10")

exp <- c(
"<x14:dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\"><x14:formula1><xm:f>'Sheet 4'!$A$1:$A$10</xm:f></x14:formula1><xm:sqref>A2:A31</xm:sqref></x14:dataValidation>",
"<x14:dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\"><x14:formula1><xm:f>'Sheet 4'!$A$1:$A$10</xm:f></x14:formula1><xm:sqref>B2:B31</xm:sqref></x14:dataValidation>"
"<dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\" sqref=\"A2:A31\"><formula1>'Sheet 4'!$A$1:$A$10</formula1></dataValidation>",
"<dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\" sqref=\"B2:B31\"><formula1>'Sheet 4'!$A$1:$A$10</formula1></dataValidation>"
)
got <- xml_node(wb2$worksheets[[3]]$extLst, "ext", "x14:dataValidations", "x14:dataValidation")
got <- wb2$worksheets[[3]]$dataValidations
expect_equal(exp, got)

### tests if conditions
Expand Down
32 changes: 31 additions & 1 deletion tests/testthat/test-class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,40 @@ test_that("test data validation list and sparklines", {
add_sparklines(sheet = 1, sparklines = s2)

exp <- c(
"<ext xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\" uri=\"{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}\"><x14:dataValidations xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\" count=\"2\"><x14:dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\"><x14:formula1><xm:f>\"O1,O2\"</xm:f></x14:formula1><xm:sqref>A2:A11</xm:sqref></x14:dataValidation><x14:dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\"><x14:formula1><xm:f>\"O2,O3\"</xm:f></x14:formula1><xm:sqref>A12:A21</xm:sqref></x14:dataValidation></x14:dataValidations></ext>",
"<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-ASEVX1JWJGYG}\"><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 displayEmptyCellsAs=\"gap\" xr2:uid=\"{6F57B887-24F1-C14A-942C-9DKW7WYNM276}\"><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:sparklineGroups></ext>"
)
got <- wb$worksheets[[1]]$extLst
expect_equal(exp, got)

})

test_that("old and new data validations", {

temp <- temp_xlsx()

wb <- wb_workbook()$
add_worksheet()$
add_data(x = sample(c("O1", "O2"), 10, TRUE))$
add_data(dims = "B1", x = sample(c("O1", "O2"), 10, TRUE))$
add_data_validation(sheet = 1, col = 2, rows = 1:10, type = "list", value = '"O1,O2"')

# add data validations list as x14. this was the default in openxlsx and openxlsx2 <= 0.3
wb$worksheets[[1]]$extLst <- "<ext xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\" uri=\"{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}\"><x14:dataValidations xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\" count=\"2\"><x14:dataValidation type=\"list\" allowBlank=\"1\" showInputMessage=\"1\" showErrorMessage=\"1\"><x14:formula1><xm:f>\"O1,O2\"</xm:f></x14:formula1><xm:sqref>A2:A11</xm:sqref></x14:dataValidation></x14:dataValidations></ext>"

wb$save(temp)

# make sure that it loads
wb2 <- wb_load(temp)

# test for equality
expect_equal(
wb$worksheets[[1]]$dataValidations,
wb2$worksheets[[1]]$dataValidations
)

expect_equal(
wb$worksheets[[1]]$extLst,
wb2$worksheets[[1]]$extLst
)

})