awalker89 / openxlsx

R package for .xlsx file reading and writing.
Other
364 stars 79 forks source link

Implement protectRange() #421

Closed DavisVaughan closed 5 years ago

DavisVaughan commented 6 years ago

This PR implements the protectRange() function. It supersedes #412. It protects an Excel range so that when a worksheet is protected with protectWorksheet(), specific ranges are still editable with or without a password specific to that range.

Note that this only works properly on a Windows version of Excel. On Mac, there is no UI window for protecting ranges. That said, on my Mac, I can use protectRange() without a password and it leaves the range open for editing when i lock the rest of it with protectWorksheet(). If I use a password, I get a strange "Activate" window that pops up when I try to edit the cells, and when I click okay it gives me an error saying I typed in the wrong password. I've made a note in the docs saying it only works properly on Windows.

There are no checks that the range is in a correct Excel format. If an incorrect format is supplied, upon opening the Excel workbook Excel will complain and ask if you want to remove the faulty XML. If you say yes, it will remove the protected range (in fact I think it removes all of them?).

It is meaningful for overlapping ranges to be protected with multiple passwords. See the documentation Details section for more info.

I see this PR being useful when creating data entry templates with R. A data scientist could create a boxed off "holy grail" sheet / range where an analyst can enter data and then pass it back to the data scientist for further munging. The protection prevents the analyst from entering data in cells that could cause the data scientist issues.