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

Cell text with <br/> is replaced with NA #894

Closed
dhicks opened this issue Jan 16, 2024 · 3 comments
Closed

Cell text with <br/> is replaced with NA #894

dhicks opened this issue Jan 16, 2024 · 3 comments
Labels
bug 🐛 Something isn't working

Comments

@dhicks
Copy link

dhicks commented Jan 16, 2024

# Works as expected
foo = data.frame(text = 'foo bar')
wb_workbook() |> 
    wb_add_worksheet() |> 
    wb_add_data_table(x = foo) |> 
    wb_to_df()
#      text
# 2 foo bar 

# Text is replaced with NA
bar = data.frame(text = 'foo <br/>bar')
wb_workbook() |> 
    wb_add_worksheet() |> 
    wb_add_data_table(x = bar) |> 
    wb_to_df()
#   text
# 2   NA
@dhicks
Copy link
Author

dhicks commented Jan 16, 2024

Same thing happens with any closing HTML tag:

bar = data.frame(text = 'foo <em>bar</em>')
wb_workbook() |> 
    wb_add_worksheet() |> 
    wb_add_data_table(x = bar) |> 
    wb_to_df()

@JanMarvin
Copy link
Owner

Hi @dhicks, thanks for the report. Indeed a tricky one. The xml cell needs to be escaped like this:

"<is><t>foo &lt;em&gt;bar&lt;/em&gt;</t></is>"

But since we detect something like an xml tag inside, we assume that there the cell is already escaped.

openxlsx2:::txt_to_is('foo <em>bar</em>')
#> "<is><em>bar</em></is>"

Fixing this is a bit of a nuisance:

library(openxlsx2)

bar <- data.frame(
  x = xml_node_create(
    xml_name = "t",
    xml_children = openxlsx2:::replace_legal_chars("foo <em>bar</em>")
  )
)

wb <- wb_workbook() |> 
  wb_add_worksheet() |> 
  wb_add_data_table(x = bar)

wb$to_df()
#>                  x
#> 2 foo <em>bar</em>

if (interactive()) wb$open()

Same for the first example:

library(openxlsx2)

bar <- data.frame(
  x = xml_node_create(
    xml_name = "t",
    xml_children = openxlsx2:::replace_legal_chars("foo <br/> bar")
  )
)

wb <- wb_workbook() |> 
  wb_add_worksheet() |> 
  wb_add_data_table(x = bar)

wb$to_df()
#>               x
#> 2 foo <br/> bar

if (interactive()) wb$open()

@JanMarvin JanMarvin added the bug 🐛 Something isn't working label Jan 16, 2024
@JanMarvin
Copy link
Owner

Interestingly my solution in is part of fmt_txt(). Therefore this works:

library(openxlsx2)
wb <- wb_workbook()$add_worksheet()$add_data(
  x = fmt_txt("a <br/> b")
)
wb$to_df()
#>           x
#> 2 a <br/> b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants