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

Write sst with inline_string = FALSE #499

Merged
merged 13 commits into from
Jan 21, 2023
Merged

Write sst with inline_string = FALSE #499

merged 13 commits into from
Jan 21, 2023

Conversation

JanMarvin
Copy link
Owner

@JanMarvin JanMarvin commented Dec 26, 2022

closes #67

what is this about?

Currently we write every character as inlineStr. Therefore we have a couple of cells like this

<c t="inlineStr">
 <is>
   <t>foo</t>
 </is>
</c>

This is completely fine with the open xml standard. But Excel usually writes characters as sharedStrings. They provide a sharedStrings.xml file looking like this:

<sst uniqueCount="1">
  <si>
    <t>foo</t>
  </si>
</sst>

and a cell that looks like this:

<c t="s">
  <v>0</v>
</c>

update

I have decided to go a different route. For now we treat sharedStrings similar to inlineStrings and simply create the shared strings once we are done with adding data.

  • write strings as <is><t>...</t></is> to cc
  • check for unique strings in cc
  • combine these with wb$sharedStrings
  • replace cc values with index positions
  • update cc and wb$shareStrings

This brings the benefit of having shared strings over the workbook which can reduce the overall file size with string duplicates in a workbook. But, since we do not check for these initially, it is a bit slower, because of the unique and match calls once writing is done. On the other side it brings the benefit of potentially smaller files and the code is a lot cleaner.

library(openxlsx2)

res <- microbenchmark::microbenchmark(
  wb_workbook()$add_worksheet()$add_data(x = iris, inline_strings = FALSE),
  wb_workbook()$add_worksheet()$add_data(x = iris, inline_strings = TRUE),
  times = 100
); res
#> Unit: milliseconds
#>                                                                      expr
#>  wb_workbook()$add_worksheet()$add_data(x = iris, inline_strings = FALSE)
#>   wb_workbook()$add_worksheet()$add_data(x = iris, inline_strings = TRUE)
#>       min       lq     mean   median       uq       max neval
#>  5.419856 5.581581 6.763844 5.901882 6.227162 54.860594   100
#>  5.115616 5.287041 5.785157 5.533161 5.982857  9.783031   100

@JanMarvin JanMarvin added this to the future milestone Dec 26, 2022
R/write.R Outdated Show resolved Hide resolved
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

Successfully merging this pull request may close these issues.

convert characters to sharedStrings when saving
1 participant