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

[WIP] add MIP support. closes #979 #983

Merged
merged 10 commits into from
Apr 7, 2024
Merged

[WIP] add MIP support. closes #979 #983

merged 10 commits into from
Apr 7, 2024

Conversation

JanMarvin
Copy link
Owner

@JanMarvin JanMarvin commented Mar 29, 2024

TODO list

  • feedback: does it work?
  • move code from wrapper function to workbook function
  • cleanups?
library(openxlsx2)
wb_w_mips <- wb_load("~/Downloads/SampleSensitivityIssue.xlsx")
#> Warning: Found unknown folders in the input file:
#> webextensions
#> 
#> These folders are not yet processed by openxlsx2, but depending on what you want to do, this may not be fatal.
#> Most likely a file with these folders has not yet been detected. If you want to contribute to the development of the package, maybe just to silence this warning, please open an issue about unknown content folders and, if possible, provide a file via our Github or by mail.

mips_xml <- wb_get_mips(wb_w_mips)
#> Found MIPS section: General

wb <- wb_workbook() |> 
  wb_add_worksheet() |> 
  wb_set_properties(
    custom = list(
      Software    = "openxlsx2",
      Version     = 1.5, 
      ReleaseDate = as.Date("2024-03-26"),
      CRAN        = TRUE,
      DEV         = FALSE
    )
  )

wb <- wb |>
  wb_add_mips(xml = mips_xml)

updated <- wb_get_mips(wb)
#> Found MIPS section: General

wb <- wb |>
  wb_add_mips(xml = mips_xml)
#> File has duplicated custom section

updated2 <- wb_get_mips(wb)
#> Found MIPS section: General

# expect difference, because of pid differences
all.equal(updated, updated2)
#> [1] TRUE

options("openxlsx2.mips_xml_string" = mips_xml)

wb <- wb |>
  wb_add_mips()

updated3 <- wb_get_mips(wb)
#> Found MIPS section: General

all.equal(updated2, updated3)
#> [1] TRUE

if (interactive()) wb$open()

Maybe you can have a look at this PR @retodomax

It's still WIP, but this should be a start. I only have this single file to play around with (it's from this issue: OfficeDev/office-js#2459) and as stated no way to test if it works. My only test is that the output opens in MS365, but there is no MIP banner for me.

[EDIT] updated code snippet above

@JanMarvin
Copy link
Owner Author

I've improved wb_set_properties() to accept custom file properties. Maybe you're interested @olivroy. Find an example above how to write custom text, numeric, date and bool properties.

The sensitivity label applied via MIP (the Microsoft Information Property thingy, see #979) are 7(?) custom text properties. Since there is probably no way for us to write this from scratch, I choose to read the xml string and apply this where required.

@retodomax
Copy link

Hi @JanMarvin,
Your example code above mostly worked for me. Two comments

  1. "Found MIPS section: General", for me this was not called "General" but instead some special ID code (1c79e38c-78...)
  2. If I run only the part with options() and wb_add_mips() then the resulting Excel file had no sensitivity label i.e.
library(openxlsx2)
wb_w_mips <- wb_load("~/Downloads/SampleSensitivityIssue.xlsx")

mips_xml <- wb_get_mips(wb_w_mips)
#> Found MIPS section: 1c79e38c-78...

wb <- wb_workbook() |> 
  wb_add_worksheet() |> 
  wb_set_properties(
    custom = list(
      Software    = "openxlsx2",
      Version     = 1.5, 
      ReleaseDate = as.Date("2024-03-26"),
      CRAN        = TRUE,
      DEV         = FALSE
    )
  )

options("openxlsx2.mips_xml_string" = mips_xml)

wb <- wb |>
  wb_add_mips()

updated3 <- wb_get_mips(wb)
#> Found MIPS section: 

updated3
#> [1] ""

if (interactive()) wb$open()
## Opens Excel with no sensitivity label

Everything else works as expected.

Thanks!

@JanMarvin
Copy link
Owner Author

JanMarvin commented Apr 2, 2024

Thanks for the feedback @retodomax ! Odd that it doesn't work with the options. There is no reason I can think of, why it shouldn't. But maybe I forgot something, I'll have a look later.

Could you have a look which of the sections in mips_xlsx looks like a name? I assume there should be something that looks human readable like General or Confidential etc? But maybe it is just numbers in your organization.

mips_xml |> as_xml()

[EDIT:] In the line below the first returns a character() and not NULL. That's why the option doesn't work. I'll fix it later

  mips <- xml_node(xml, "property") %||% getOption("openxlsx2.mips_xml_string")

@retodomax
Copy link

Unfortunately, there is nothing like General or Confidential...

<property fmtid="{D5CDD505-...AE}" pid="2" name="MSIP_Label_1c79e38c-78...05_Enabled">
 <vt:lpwstr>true</vt:lpwstr>
</property>
<property fmtid="{D5CDD505-...AE}" pid="3" name="MSIP_Label_1c79e38c-78...05_SetDate">
 <vt:lpwstr>2024-04-02T06:00:28Z</vt:lpwstr>
</property>
<property fmtid="{D5CDD505-...AE}" pid="4" name="MSIP_Label_1c79e38c-78...05_Method">
 <vt:lpwstr>Privileged</vt:lpwstr>
</property>
<property fmtid="{D5CDD505-...AE}" pid="5" name="MSIP_Label_1c79e38c-78...05_Name">
 <vt:lpwstr>1c79e38c-78...05</vt:lpwstr>
</property>

and it continues with SiteId, ActionId and ContentBits. I don't know from where our internal Excel gets the information that this is a Public file... probably there is somewhere another file which links the numbers and the labels.

@JanMarvin
Copy link
Owner Author

Thanks! Guess, as long as it works it does not have to bother us, but in this case I can disable the message. It should be used so that somebody how has multiple MIPS sections available, can differentiate them, but I doubt that anybody understands these digits.

It looks like they match your organizations MIPS string (from your example above, I've added ** to highlight the presumably matching sections). Maybe a public document has no need for a human readable name.

<property fmtid="{D5CDD505-...AE}" pid="5" name="MSIP_Label_**1c79e38c-78...05**_Name">
 <vt:lpwstr>**1c79e38c-78...05**</vt:lpwstr>
</property>

Two additional observations:

  • Out of curiosity, can you run Get-Label in Powershell? It's unlikely that I'll add a function to create these MIPS xml strings from scratch, but that might give another hint regarding the possible sensitivity levels.
  • Do you see the timestamp of the sensitivity label in Excel? Should we update this timestamp when writing the file? Aka if you modify it, does it matter? (you can simply dput() the string into a notepad, change it to some arbitrary date and assign the string as mips_xml in the example code above).

@retodomax
Copy link

Hi @JanMarvin

  1. Returns an Error "Get-Label : The term 'Get-Label' is not recognized as the name ..."
  2. I don't see any timestamp associated with sensitivity label. I don't notice any difference if I change it manually.

@JanMarvin
Copy link
Owner Author

Hi @retodomax , thanks for the testing! Guess it is good to go now.

@JanMarvin JanMarvin merged commit 4a6d9ba into main Apr 7, 2024
9 checks passed
@JanMarvin JanMarvin deleted the mip_support branch April 7, 2024 13:46
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.

None yet

2 participants