From 63f41ceaec288613b17e1998753bcc10b7b26c1e Mon Sep 17 00:00:00 2001 From: jhoneill Date: Thu, 15 Aug 2019 16:03:42 +0100 Subject: [PATCH] WS protection & better control of export -now --- Export-Excel.ps1 | 10 +++++----- ImportExcel.psd1 | 1 + Set-Column.ps1 | 8 ++++++++ Set-WorkSheetProtection.ps1 | 13 ++++++++----- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 3e2dd1a..711e7cb 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -531,13 +531,13 @@ try { $script:Header = $null if ($Append -and $ClearSheet) {throw "You can't use -Append AND -ClearSheet."} + #if we have no params, or explicit -now, or no path/package. Set a path, and set show/autosize/Autofilter if not set (they maybe passed as $false) if ($PSBoundParameters.Keys.Count -eq 0 -Or $Now -or (-not $Path -and -not $ExcelPackage) ) { $Path = [System.IO.Path]::GetTempFileName() -replace '\.tmp', '.xlsx' - $Show = $true - $AutoSize = $true - if (-not $TableName) { - $AutoFilter = $true - } + if (-not $PSBoundParameters.ContainsKey('Show')) {$Show = $true} + if (-not $PSBoundParameters.ContainsKey('AutoSize')) {$AutoSize = $true} + if (-not $PSBoundParameters.ContainsKey('AutoFilter') -and + -not $TableName) {$AutoFilter = $true} } if ($ExcelPackage) { $pkg = $ExcelPackage diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 7e0c867..caed626 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -118,6 +118,7 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'Set-ExcelColumn', 'Set-ExcelRange', 'Set-ExcelRow', + 'Set-WorkSheetProtection', 'Test-Boolean', 'Test-Date', 'Test-Integer', diff --git a/Set-Column.ps1 b/Set-Column.ps1 index ecbcd7d..05543a5 100644 --- a/Set-Column.ps1 +++ b/Set-Column.ps1 @@ -25,6 +25,14 @@ "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc the new data cells should become a named range, which will also be named "WinsToFastLaps" and the column width will be set automatically. + When a value begins with "=", it is treated as a formula. + If value is a script block it will be evaluated, so here the string "=E$row/C$Row" + will have the number of the current row inserted. See the value parameter for a list of + variables which can be used. Note than when evaluating an expression in a string, + it is necessary to wrap it in $() so $row is valid but $($row+1) is needed. To prevent + Variables merging into other parts of the string, use the back tick "$columnName`4" will + be "G4" - withouth the backtick the string will look for a variable named "columnName4" + .EXAMPLE Set-ExcelColumn -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize diff --git a/Set-WorkSheetProtection.ps1 b/Set-WorkSheetProtection.ps1 index 95de184..1b46560 100644 --- a/Set-WorkSheetProtection.ps1 +++ b/Set-WorkSheetProtection.ps1 @@ -7,7 +7,7 @@ .Example Set-WorkSheetProtection -WorkSheet $planSheet -IsProtected -AllowAll -AllowInsertColumns:$false -AllowDeleteColumns:$false -UnLockAddress "A:N" - Turns on protection for the worksheet in $planSheet, checks all the allow boxes excel Insert and Delete columns and unlocks columns A-N + Turns on protection for the worksheet in $planSheet, checks all the allow boxes except Insert and Delete columns and unlocks columns A-N #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '',Justification='Does not change system state')] param ( @@ -48,7 +48,7 @@ [switch]$BlockEditObject, ##Opposite of the value in the 'Edit Scenarios' check box. Set to allow when Protect is first enabled [switch]$BlockEditScenarios, - #Address range for cells to lock in the form "A:Z" or "1:10" or "A1:Z10" + #Address range for cells to lock in the form "A:Z" or "1:10" or "A1:Z10". If No range is specified, the whole sheet is locked by default. [string]$LockAddress, #Address range for cells to Unlock in the form "A:Z" or "1:10" or "A1:Z10" [string]$UnLockAddress @@ -72,10 +72,13 @@ } Else {Write-Warning -Message "You haven't said if you want to turn protection off, or on." } + if ($LockAddress) { + Set-ExcelRange -Range $WorkSheet.cells[$LockAddress] -Locked + } + elseif ($IsProtected) { + Set-ExcelRange -Range $WorkSheet.Cells -Locked + } if ($UnlockAddress) { Set-ExcelRange -Range $WorkSheet.cells[$UnlockAddress] -Locked:$false } - if ($lockAddress) { - Set-ExcelRange -Range $WorkSheet.cells[$UnlockAddress] -Locked - } } \ No newline at end of file