dfinke / ImportExcel

PowerShell module to import/export Excel spreadsheets, without Excel
https://www.powershellgallery.com/packages/ImportExcel/
Apache License 2.0
2.47k stars 400 forks source link

Protect sheet and structure #1080

Closed theoami closed 2 years ago

theoami commented 3 years ago

Hello,

I don't see methods in module for protect sheet and structure.

it's possible to add this functionnality ?

Regards

dfinke commented 3 years ago

Sorry, don't know what that is and have not looked to see if the EPPlus library supports it. Can you provide more detail on that and google EPPlus to see if that is supported?

theoami commented 3 years ago

Hello @dfinke ,

With Com Excel Application, I used this lines :

=> Worksheet protect :

$workbook.Worksheets("Sheet1").Protect('<password>',1,1,1,0,0,0,0,0,0,0,0,0,0,0,0);

=> Workbook structure protect :

$workbook.Protect('<password>',$true,$true);

With EEPlus library, I see this in the documentation but I'm not sure it's this :

//Lock the workbook totally var workbook = package.Workbook; workbook.Protection.LockWindows = true; workbook.Protection.LockStructure = true;

Regards

globeinthesky commented 3 years ago

Hello @theoami, this is what I have

$lcSetPassword = "123"
$xlPkg = Export-Excel -ExcelPackage $xlPkg -PassThru

for ($ii = 1; $ii -le $xlPkg.WorkBook.WorkSheets.Count; $ii++) {
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.SetPassword($lcSetPassword)
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowSelectLockedCells=$true
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowSelectUnlockedCells=$true
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowEditObject=$true
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowEditScenarios=$true
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowFormatCells=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowFormatColumns=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowFormatRows=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowInsertColumns=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowInsertRows=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowInsertHyperlinks=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowDeleteColumns=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowDeleteRows=$false
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowSort=$true
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowAutoFilter=$true
    $xlPkg.WorkBook.WorkSheets[$ii].Protection.AllowPivotTables=$true
}
$xlPkg.WorkBook.Protection.LockStructure = $true
$xlPkg.WorkBook.Protection.LockWindows = $false
$xlPkg.WorkBook.Protection.LockRevision = $false
$xlPkg.WorkBook.Protection.SetPassword("$lcSetPassword")
$xlPkg.Save()
$xlPkg.Dispose()
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.