JanMarvin / openxlsx2

openxlsx2 - read, write and modify xlsx files
https://janmarvin.github.io/openxlsx2/
Other
116 stars 10 forks source link

MIP sensitivity labels don't appear to save #1144

Open PD-LUHC opened 2 days ago

PD-LUHC commented 2 days ago

Having followed the thread of issues starting at https://github.com/ycphs/openxlsx/issues/461 and ending at https://github.com/JanMarvin/openxlsx2/pull/983 I have had limited success in getting MIP labels to successfully apply to files saved using wb_save. Possibly I'm being dim, but I don't see why the simplified, example below shouldn't work:

xlsx_path <- "~/simple_test2.xlsx" # desired output path
wb_w_mips <- wb_load("~/mip_template.xlsx") # existing xlsx with correct mip label
mip_xml <- wb_get_mips(wb_w_mips) # also tested this by manually extracting LabelInfo.xml and pasting string in here - no joy.

wb <- wb_workbook() |>
    wb_add_worksheet() |> 
    wb_add_mips(xml = mip_xml) |>
    wb_save(file = xlsx_path, overwrite = TRUE) # this saves a file with no mip label set

if (interactive()) wb$open() # this opens a temp file with mip label set

Interestingly saving the file does not appear to apply the label. But using open() launches a temp file with the label applied. Is it just me or is that unintuitive? What is it about saving a fresh file that prevents the mip label being applied? image image

This is running on openxlsx2 1.9

JanMarvin commented 2 days ago

Hi @PD-LUHC , your code looks valid and I’m not sure what happens. The difference between wb$open() and wb$save() should be that the first writes a file to tempdir and opens it and the latter simply saves the file.

As stated before, I have no way to test this. I can look into it sometime next week, but unless something strikes my eye …

Can you save the file to your desktop and open it with xl_open() just to make sure that there isn’t something else going on with your directory.

JanMarvin commented 2 days ago

Another idea. Could you try the chained code? In case that there’s something going wrong in the wrapper code.

JanMarvin commented 2 days ago
wb$save(xlsx_path)
wb_load(xlsx_path)$get_mips()

Please check this as well, to see if the mips string is found in the file.

PD-LUHC commented 2 days ago

Hi @JanMarvin, thanks for you swift reply and helpful suggestions. FYI, I have a small update - because I was worried I might be doing something silly, I copied your example from here https://github.com/JanMarvin/openxlsx2/pull/983#issue-2215472480 and made two changes:

  1. changed the reference file to one I generated based on our sensitivity settings
  2. added a wb_save line pointing to the same folder that the reference file was stored in

All the intermediate validation from your example returned TRUE, but it still replicated the behaviour of my initial post.

Either way, thanks for your time. I'll definitely give your suggestions a whirl and report back when I can.

JanMarvin commented 2 days ago

Hm, you could save the file that works fine via excel and inspect if there’s something going on. (Unzip and compare the xml files line by line). If you fancy you could send me both files. Otherwise I can look into the issue next week, but no promises. I can only guess if it works and if it doesn’t, excel won’t tell me.

These mips strings are basically xml stamps like „confidential“ that only appear in environments that have (maybe this specific) mips setting enabled and Microsoft believes that this is something only businesses can have.

JanMarvin commented 2 days ago

Here is save (and a couple of lines more) https://github.com/JanMarvin/openxlsx2/blob/b3f915054625c5314ee028a479443062ea1ec712/R/class-workbook.R#L2864

And wb_open uses xl_open https://github.com/JanMarvin/openxlsx2/blob/b3f915054625c5314ee028a479443062ea1ec712/R/xl_open.R#L49

Simply saving the file and opening the workbook.

PD-LUHC commented 2 days ago

Hm, you could save the file that works fine via excel and inspect if there’s something going on. (Unzip and compare the xml files line by line). If you fancy you could send me both files. Otherwise I can look into the issue next week, but no promises. I can only guess if it works and if it doesn’t, excel won’t tell me.

These mips strings are basically xml stamps like „confidential“ that only appear in environments that have (maybe this specific) mips setting enabled and Microsoft believes that this is something only businesses can have.

Yeah I think there is some corporate voodoo going on with mips. I had a look through the xml files of the two xlsx and found references to LabelInfo.xml in 2 other files (.rels and [Content_Types].xml). I found no major differences (some of the entries had a different ordering, but the entries were the same).

Interestingly I zipped the files back up and opened in excel to find that both were now showing as having no label applied. So the act of unzipping and zipping was enough to disrupt the reading/application of the label. For completeness, unzipping the re-zipped file showed exactly the same contents and get_mips() returns the expected value

So beyond setting the right label xml, I think maybe there is some other process/rule that is determining how LabelInfo is being read and applied.

Corporate voodoo.

JanMarvin commented 2 days ago

What bothers me, though. That it works for one file and not for the other.

I just don’t see why one fails and the other works.