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

Is it possible to define formula1 in dataValidation when the type is list by giving a list of values rather than specifying a sheet range? Like #384

Closed
JanMarvin opened this issue Sep 23, 2022 · 8 comments

Comments

@JanMarvin
Copy link
Collaborator

JanMarvin commented Sep 23, 2022

Is it possible to define formula1 in dataValidation when the type is list by giving a list of values rather than specifying a sheet range? Like

library(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "Sheet 1")
addWorksheet(wb, "Sheet 2")

writeDataTable(wb, sheet = 1, x = iris[1:30, ])
writeData(wb, sheet = 2, x = sample(iris$Sepal.Length, 10))

dataValidation(wb, 1, col = 1, rows = 2:31, type = "list", value = "'option1,option2")

When I try to do this, I get an error in the resulting excel file.

Originally posted by @IRorpa in #342 (comment)

@JanMarvin
Copy link
Collaborator Author

@IRorpa I've moved this to a new issue. I can reproduce your observation, but I do not know what you are trying to achieve. Are option1 and option2 named regions? Other than that, the data validation is converted to some xml string. This string must be interpretable by spreadsheet software.

Something like this:

<ext xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" uri="{05C60535-1F16-4fd2-B633-F4F36F0B64E0}">
 <x14:dataValidations xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">
  <x14:dataValidation type="list" allowBlank="1" showInputMessage="1" showErrorMessage="1">
   <x14:formula1>
    <xm:f>option1, option2</xm:f>
   </x14:formula1>
   <xm:sqref>A2:A31</xm:sqref>
  </x14:dataValidation>
 </x14:dataValidations>
</ext>

The above is created by openxlsx2 and there is no error, but still no clue what's supposed to happen :)

library(openxlsx2)

wb <- wb_workbook()$
  add_worksheet()$add_data_table(x = iris[1:30,])$
  add_worksheet()$add_data(sheet = 2, x = sample(iris$Sepal.Length, 10))$
  add_data_validation(sheet = 1, col = 1, rows = 2:31, type = "list", value = "option1, option2")

wb$open()

@IRorpa
Copy link

IRorpa commented Sep 23, 2022

Thanks for looking into it. I tried installing openxlsx2 and i got a message saying it's not available for my version of R. Do you know where I can get it for R version 4.2.1?

@tentacles-from-outer-space
Copy link
Contributor

It is posible. I done it here: #16 (comment)

In your case

dataValidation(wb, 1, col = 1, rows = 2:31, type = "list", value = '"option1,option2"')

@IRorpa
Copy link

IRorpa commented Sep 23, 2022

Thank you so much. This is exactly what I needed @tentacles-from-outer-space.

@JanMarvin
Copy link
Collaborator Author

Glad it's solved

@JanMarvin
Copy link
Collaborator Author

Just to please my curiosity, what is supposed to happen? I've re-written a lot of the data validation code, but rarely use it.

@IRorpa
Copy link

IRorpa commented Sep 23, 2022

Just to please my curiosity, what is supposed to happen? I've re-written a lot of the data validation code, but rarely use it.

The cell that the data validation is applied to is supposed to display a dropdown of the options specified in the list.

@JanMarvin
Copy link
Collaborator Author

Thanks! btw openxlsx2 is currently in development and a first CRAN release should happen in the next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants