mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Module.Template 2.0.2
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -60,7 +60,6 @@ test.xlsx
|
||||
testCCFMT.ps1
|
||||
testHide.ps1
|
||||
ImportExcel.zip
|
||||
.vscode/launch.json
|
||||
.vscode/settings.json
|
||||
|
||||
~$*
|
||||
|
||||
56
.vscode/launch.json
vendored
Normal file
56
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "PowerShell",
|
||||
"request": "launch",
|
||||
"name": "PowerShell Pester Tests",
|
||||
"script": "Import-Module -Name '.\\' -Force ; Invoke-Pester", // Change to '.\\ModuleName.psd1' if Git name different
|
||||
"args": [""],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "PowerShell",
|
||||
"request": "launch",
|
||||
"name": "PowerShell Launch Current File",
|
||||
"script": "${file}",
|
||||
"args": [],
|
||||
"cwd": "${file}"
|
||||
},
|
||||
{
|
||||
"type": "PowerShell",
|
||||
"request": "launch",
|
||||
"name": "PowerShell Launch Current File in Temporary Console",
|
||||
"script": "${file}",
|
||||
"args": [],
|
||||
"cwd": "${file}",
|
||||
"createTemporaryIntegratedConsole": true
|
||||
},
|
||||
{
|
||||
"type": "PowerShell",
|
||||
"request": "launch",
|
||||
"name": "PowerShell Launch Current File w/Args Prompt",
|
||||
"script": "${file}",
|
||||
"args": [
|
||||
"${command:SpecifyScriptArgs}"
|
||||
],
|
||||
"cwd": "${file}"
|
||||
},
|
||||
{
|
||||
"type": "PowerShell",
|
||||
"request": "attach",
|
||||
"name": "PowerShell Attach to Host Process",
|
||||
"processId": "${command:PickPSHostProcess}",
|
||||
"runspaceId": 1
|
||||
},
|
||||
{
|
||||
"type": "PowerShell",
|
||||
"request": "launch",
|
||||
"name": "PowerShell Interactive Session",
|
||||
"cwd": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
286
Compare-WorkSheet.ps1
Normal file
286
Compare-WorkSheet.ps1
Normal file
@@ -0,0 +1,286 @@
|
||||
Function Compare-WorkSheet {
|
||||
<#
|
||||
.Synopsis
|
||||
Compares two worksheets and shows the differences.
|
||||
.Description
|
||||
This command takes two file names, one or two worksheet names and a name
|
||||
for a "key" column. It reads the worksheet from each file and decides the
|
||||
column names and builds a hashtable of the key-column values and the
|
||||
rows in which they appear.
|
||||
It then uses PowerShell's Compare-Object command to compare the sheets
|
||||
(explicitly checkingall the column names which have not been excluded).
|
||||
For the difference rows it adds the row number for the key of that row -
|
||||
we have to add the key after doing the comparison, otherwise identical
|
||||
rows at different positions in the file will not be considered a match.
|
||||
We also add the name of the file and sheet in which the difference occurs.
|
||||
If -BackgroundColor is specified the difference rows will be changed to
|
||||
that background in the orginal file.
|
||||
.Example
|
||||
Compare-WorkSheet -Referencefile 'Server56.xlsx' -Differencefile 'Server57.xlsx' -WorkSheetName Products -key IdentifyingNumber -ExcludeProperty Install* | Format-Table
|
||||
|
||||
The two workbooks in this example contain the result of redirecting a subset
|
||||
of properties from Get-WmiObject -Class win32_product to Export-Excel.
|
||||
The command compares the "Products" pages in the two workbooks, but we
|
||||
don't want to register a difference if the software was installed on a
|
||||
different date or from a different place, and excluding Install* removes
|
||||
InstallDate and InstallSource. This data doesn't have a "Name" column, so
|
||||
we specify the "IdentifyingNumber" column as the key.
|
||||
The results will be presented as a table.
|
||||
.Example
|
||||
Compare-WorkSheet "Server54.xlsx" "Server55.xlsx" -WorkSheetName Services -GridView
|
||||
|
||||
This time two workbooks contain the result of redirecting the command
|
||||
Get-WmiObject -Class win32_service to Export-Excel. Here the -Differencefile
|
||||
and -Referencefile parameter switches are assumed and the default setting for
|
||||
-Key ("Name") works for services. This will display the differences between
|
||||
the "Services" sheets using a grid view
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.xlsx' -WorkSheetName Services -BackgroundColor lightGreen
|
||||
|
||||
This version of the command outputs the differences between the "services" pages and highlights any different rows in the spreadsheet files.
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.xlsx' -WorkSheetName Services -BackgroundColor lightGreen -FontColor Red -Show
|
||||
|
||||
This example builds on the previous one: this time where two changed rows have
|
||||
the value in the "Name" column (the default value for -Key), this version adds
|
||||
highlighting of the changed cells in red; and then opens the Excel file.
|
||||
.Example
|
||||
Compare-WorkSheet 'Pester-tests.xlsx' 'Pester-tests.xlsx' -WorkSheetName 'Server1','Server2' -Property "full Description","Executed","Result" -Key "full Description"
|
||||
|
||||
This time the reference file and the difference file are the same file and
|
||||
two different sheets are used. Because the tests include the machine name
|
||||
and time the test was run, the command specifies that a limited set of
|
||||
columns should be used.
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.xlsx' -WorkSheetName general -Startrow 2 -Headername Label,value -Key Label -GridView -ExcludeDifferent
|
||||
|
||||
The "General" page in the two workbooks has a title and two unlabelled columns
|
||||
with a row each for CPU, Memory, Domain, Disk and so on. So the command is
|
||||
told to start at row 2 in order to skip the title and given names for the
|
||||
columns: the first is "label" and the second "Value"; the label acts as the key.
|
||||
This time we are interested in those rows which are the same in both sheets,
|
||||
and the result is displayed using grid view.
|
||||
Note that grid view works best when the number of columns is small.
|
||||
.Example
|
||||
Compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName general -Startrow 2 -Headername Label,value -Key Label -BackgroundColor White -Show -AllDataBackgroundColor LightGray
|
||||
|
||||
This version of the previous command highlights all the cells in LightGray
|
||||
and then sets the changed rows back to white.
|
||||
Only the unchanged rows are highlighted.
|
||||
#>
|
||||
[cmdletbinding(DefaultParameterSetName)]
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification="Write host used for sub-warning level message to operator which does not form output")]
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification="False positives when initializing variable in begin block")]
|
||||
Param(
|
||||
#First file to compare.
|
||||
[parameter(Mandatory=$true,Position=0)]
|
||||
$Referencefile ,
|
||||
#Second file to compare.
|
||||
[parameter(Mandatory=$true,Position=1)]
|
||||
$Differencefile ,
|
||||
#Name(s) of worksheets to compare.
|
||||
$WorkSheetName = "Sheet1",
|
||||
#Properties to include in the comparison - supports wildcards, default is "*".
|
||||
$Property = "*" ,
|
||||
#Properties to exclude from the comparison - supports wildcards.
|
||||
$ExcludeProperty ,
|
||||
#Specifies custom property names to use, instead of the values defined in the starting row of the sheet.
|
||||
[Parameter(ParameterSetName='B', Mandatory)]
|
||||
[String[]]$Headername,
|
||||
#Automatically generate property names (P1, P2, P3 ...) instead of the using the values the starting row of the sheet.
|
||||
[Parameter(ParameterSetName='C', Mandatory)]
|
||||
[switch]$NoHeader,
|
||||
#The row from where we start to import data: all rows above the start row are disregarded. By default, this is the first row.
|
||||
[int]$Startrow = 1,
|
||||
#If specified, highlights all the cells - so you can make Equal cells one color, and Different cells another.
|
||||
$AllDataBackgroundColor,
|
||||
#If specified, highlights the rows with differences.
|
||||
$BackgroundColor,
|
||||
#If specified identifies the tabs which contain difference rows (ignored if -BackgroundColor is omitted).
|
||||
$TabColor,
|
||||
#Name of a column which is unique and will be used to add a row to the DIFF object, defaults to "Name".
|
||||
$Key = "Name" ,
|
||||
#If specified, highlights the DIFF columns in rows which have the same key.
|
||||
$FontColor,
|
||||
#If specified, opens the Excel workbooks instead of outputting the diff to the console (unless -PassThru is also specified).
|
||||
[Switch]$Show,
|
||||
#If specified, the command tries to the show the DIFF in a Grid-View and not on the console. (unless-PassThru is also specified). This works best with few columns selected, and requires a key.
|
||||
[switch]$GridView,
|
||||
#If specifieda full set of DIFF data is returned without filtering to the specified properties.
|
||||
[Switch]$PassThru,
|
||||
#If specified the result will include equal rows as well. By default only different rows are returned.
|
||||
[Switch]$IncludeEqual,
|
||||
#If specified, the result includes only the rows where both are equal.
|
||||
[Switch]$ExcludeDifferent
|
||||
)
|
||||
|
||||
#if the filenames don't resolve, give up now.
|
||||
try { $oneFile = ((Resolve-Path -Path $Referencefile -ErrorAction Stop).path -eq (Resolve-Path -Path $Differencefile -ErrorAction Stop).path)}
|
||||
Catch { Write-Warning -Message "Could not Resolve the filenames." ; return }
|
||||
|
||||
#If we have one file , we must have two different worksheet names. If we have two files we can have a single string or two strings.
|
||||
if ($onefile -and ( ($WorkSheetName.count -ne 2) -or $WorkSheetName[0] -eq $WorkSheetName[1] ) ) {
|
||||
Write-Warning -Message "If both the Reference and difference file are the same then worksheet name must provide 2 different names"
|
||||
return
|
||||
}
|
||||
if ($WorkSheetName.count -eq 2) {$worksheet1 = $WorkSheetName[0] ; $workSheet2 = $WorkSheetName[1]}
|
||||
elseif ($WorkSheetName -is [string]) {$worksheet1 = $workSheet2 = $WorkSheetName}
|
||||
else {Write-Warning -Message "You must provide either a single worksheet name or two names." ; return }
|
||||
|
||||
$params= @{ ErrorAction = [System.Management.Automation.ActionPreference]::Stop }
|
||||
foreach ($p in @("HeaderName","NoHeader","StartRow")) {if ($PSBoundParameters[$p]) {$params[$p] = $PSBoundParameters[$p]}}
|
||||
try {
|
||||
$sheet1 = Import-Excel -Path $Referencefile -WorksheetName $WorkSheet1 @params
|
||||
$sheet2 = Import-Excel -Path $Differencefile -WorksheetName $WorkSheet2 @Params
|
||||
}
|
||||
Catch {Write-Warning -Message "Could not read the worksheet from $Referencefile and/or $Differencefile." ; return }
|
||||
|
||||
#Get Column headings and create a hash table of Name to column letter.
|
||||
$headings = $Sheet1[-1].psobject.Properties.name # This preserves the sequence - using get-member would sort them alphabetically!
|
||||
$headings | ForEach-Object -Begin {$columns = @{} ; $i= 1 } -Process {$Columns[$_] = [OfficeOpenXml.ExcelAddress]::GetAddress(1,($i ++)) -replace "\d","" }
|
||||
|
||||
#Make a list of property headings using the Property (default "*") and ExcludeProperty parameters
|
||||
if ($Key -eq "Name" -and $NoHeader) {$Key = "p1"}
|
||||
$propList = @()
|
||||
foreach ($p in $Property) {$propList += ($headings.where({$_ -like $p}) )}
|
||||
foreach ($p in $ExcludeProperty) {$propList = $propList.where({$_ -notlike $p}) }
|
||||
if (($headings -contains $key) -and ($propList -notcontains $Key)) {$propList += $Key}
|
||||
$propList = $propList | Select-Object -Unique
|
||||
if ($propList.Count -eq 0) {Write-Warning -Message "No Columns are selected with -Property = '$Property' and -excludeProperty = '$ExcludeProperty'." ; return}
|
||||
|
||||
#Add RowNumber, Sheetname and file name to every row
|
||||
$firstDataRow = $startRow + 1
|
||||
if ($Headername -or $NoHeader) {$firstDataRow -- }
|
||||
$i = $firstDataRow ; foreach ($row in $Sheet1) {Add-Member -InputObject $row -MemberType NoteProperty -Name "_Row" -Value ($i ++)
|
||||
Add-Member -InputObject $row -MemberType NoteProperty -Name "_Sheet" -Value $worksheet1
|
||||
Add-Member -InputObject $row -MemberType NoteProperty -Name "_File" -Value $Referencefile}
|
||||
$i = $firstDataRow ; foreach ($row in $Sheet2) {Add-Member -InputObject $row -MemberType NoteProperty -Name "_Row" -Value ($i ++)
|
||||
Add-Member -InputObject $row -MemberType NoteProperty -Name "_Sheet" -Value $worksheet2
|
||||
Add-Member -InputObject $row -MemberType NoteProperty -Name "_File" -Value $Differencefile}
|
||||
|
||||
if ($ExcludeDifferent -and -not $IncludeEqual) {$IncludeEqual = $true}
|
||||
#Do the comparison and add file,sheet and row to the result - these are prefixed with "_" to show they are added the addition will fail if the sheet has these properties so split the operations
|
||||
[PSCustomObject[]]$diff = Compare-Object -ReferenceObject $Sheet1 -DifferenceObject $Sheet2 -Property $propList -PassThru -IncludeEqual:$IncludeEqual -ExcludeDifferent:$ExcludeDifferent |
|
||||
Sort-Object -Property "_Row","File"
|
||||
|
||||
#if BackgroundColor was specified, set it on extra or extra or changed rows
|
||||
if ($diff -and $BackgroundColor) {
|
||||
#Differences may only exist in one file. So gather the changes for each file; open the file, update each impacted row in the shee, save the file
|
||||
$updates = $diff.where({$_.SideIndicator -ne "=="}) | Group-object -Property "_File"
|
||||
foreach ($file in $updates) {
|
||||
try {$xl = Open-ExcelPackage -Path $file.name }
|
||||
catch {Write-warning -Message "Can't open $($file.Name) for writing." ; return}
|
||||
if ($PSBoundParameters.ContainsKey("AllDataBackgroundColor")) {
|
||||
$file.Group._sheet | Sort-Object -Unique | ForEach-Object {
|
||||
$ws = $xl.Workbook.Worksheets[$_]
|
||||
if ($headerName) {$range = "A" + $startrow + ":" + $ws.dimension.end.address}
|
||||
else {$range = "A" + ($startrow + 1) + ":" + $ws.dimension.end.address}
|
||||
Set-Format -WorkSheet $ws -BackgroundColor $AllDataBackgroundColor -Range $Range
|
||||
}
|
||||
}
|
||||
foreach ($row in $file.group) {
|
||||
$ws = $xl.Workbook.Worksheets[$row._Sheet]
|
||||
$range = $ws.Dimension -replace "\d+",$row._row
|
||||
Set-Format -WorkSheet $ws -Range $range -BackgroundColor $BackgroundColor
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey("TabColor")) {
|
||||
if ($TabColor -is [string]) {$TabColor = [System.Drawing.Color]::$TabColor }
|
||||
foreach ($tab in ($file.group._sheet | Select-Object -Unique)) {
|
||||
$xl.Workbook.Worksheets[$tab].TabColor = $TabColor
|
||||
}
|
||||
}
|
||||
$xl.save() ; $xl.Stream.Close() ; $xl.Dispose()
|
||||
}
|
||||
}
|
||||
#if font color was specified, set it on changed properties where the same key appears in both sheets.
|
||||
if ($diff -and $FontColor -and (($propList -contains $Key) -or ($key -is [hashtable])) ) {
|
||||
$updates = $diff.where({$_.SideIndicator -ne "=="}) | Group-object -Property $Key | Where-Object {$_.count -eq 2}
|
||||
if ($updates) {
|
||||
$XL1 = Open-ExcelPackage -path $Referencefile
|
||||
if ($oneFile ) {$xl2 = $xl1}
|
||||
else {$xl2 = Open-ExcelPackage -path $Differencefile }
|
||||
foreach ($u in $updates) {
|
||||
foreach ($p in $propList) {
|
||||
if ($u.group[0]._file -eq $Referencefile) {
|
||||
$ws1 = $xl1.Workbook.Worksheets[$u.Group[0]._sheet]
|
||||
$ws2 = $xl2.Workbook.Worksheets[$u.Group[1]._sheet]
|
||||
}
|
||||
else {
|
||||
$ws1 = $xl2.Workbook.Worksheets[$u.Group[0]._sheet]
|
||||
$ws2 = $xl1.Workbook.Worksheets[$u.Group[1]._sheet]
|
||||
}
|
||||
if($u.Group[0].$p -ne $u.Group[1].$p ) {
|
||||
Set-Format -WorkSheet $ws1 -Range ($Columns[$p] + $u.Group[0]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $ws2 -Range ($Columns[$p] + $u.Group[1]._Row) -FontColor $FontColor
|
||||
}
|
||||
}
|
||||
}
|
||||
$xl1.Save() ; $xl1.Stream.Close() ; $xl1.Dispose()
|
||||
if (-not $oneFile) {$xl2.Save() ; $xl2.Stream.Close() ; $xl2.Dispose()}
|
||||
}
|
||||
}
|
||||
elseif ($diff -and $FontColor) {Write-Warning -Message "To match rows to set changed cells, you must specify -Key and it must match one of the included properties." }
|
||||
|
||||
#if nothing was found write a message which will not be redirected
|
||||
if (-not $diff) {Write-Host "Comparison of $Referencefile::$worksheet1 and $Differencefile::$WorkSheet2 returned no results." }
|
||||
|
||||
if ($Show) {
|
||||
Start-Process -FilePath $Referencefile
|
||||
if (-not $oneFile) { Start-Process -FilePath $Differencefile }
|
||||
if ($GridView) { Write-Warning -Message "-GridView is ignored when -Show is specified" }
|
||||
}
|
||||
elseif ($GridView -and $propList -contains $key) {
|
||||
|
||||
|
||||
if ($IncludeEqual -and -not $ExcludeDifferent) {
|
||||
$GroupedRows = $diff | Group-Object -Property $key
|
||||
}
|
||||
else { #to get the right now numbers on the grid we need to have all the rows.
|
||||
$GroupedRows = Compare-Object -ReferenceObject $Sheet1 -DifferenceObject $Sheet2 -Property $propList -PassThru -IncludeEqual |
|
||||
Group-Object -Property $key
|
||||
}
|
||||
#Additions, deletions and unchanged rows will give a group of 1; changes will give a group of 2 .
|
||||
|
||||
#If one sheet has extra rows we can get a single "==" result from compare, but with the row from the reference sheet
|
||||
#but the row in the other sheet might so we will look up the row number from the key field build a hash table for that
|
||||
$Sheet2 | ForEach-Object -Begin {$Rowhash = @{} } -Process {$Rowhash[$_.$key] = $_._row }
|
||||
|
||||
$ExpandedDiff = ForEach ($g in $GroupedRows) {
|
||||
#we're going to create a custom object from a hash table. We want the fields to be ordered
|
||||
$hash = [ordered]@{}
|
||||
foreach ($result IN $g.Group) {
|
||||
# if result indicates equal or "in Reference" set the reference side row. If we did that on a previous result keep it. Otherwise set to "blank"
|
||||
if ($result.sideindicator -ne "=>") {$hash["<Row"] = $result._Row }
|
||||
elseif (-not $hash["<Row"]) {$hash["<Row"] = "" }
|
||||
#if we have already set the side, this is the second record, so set side to indicate "changed"
|
||||
if ($hash.Side) {$hash.side = "<>"} else {$hash["Side"] = $result.sideindicator}
|
||||
#if result is "in reference" and we don't have a matching "in difference" (meaning a change) the lookup will be blank. Which we want.
|
||||
$hash[">Row"] = $Rowhash[$g.Name]
|
||||
#position the key as the next field (only appears once)
|
||||
$Hash[$key] = $g.Name
|
||||
#For all the other fields we care about create <=FieldName and/or =>FieldName
|
||||
foreach ($p in $propList.Where({$_ -ne $key})) {
|
||||
if ($result.SideIndicator -eq "==") {$hash[("=>$P")] = $hash[("<=$P")] =$result.$P}
|
||||
else {$hash[($result.SideIndicator+$P)] =$result.$P}
|
||||
}
|
||||
}
|
||||
[Pscustomobject]$hash
|
||||
}
|
||||
|
||||
#Sort by reference row number, and fill in any blanks in the difference-row column
|
||||
$ExpandedDiff = $ExpandedDiff | Sort-Object -Property "<row"
|
||||
for ($i = 1; $i -lt $ExpandedDiff.Count; $i++) {if (-not $ExpandedDiff[$i].">row") {$ExpandedDiff[$i].">row" = $ExpandedDiff[$i-1].">row" } }
|
||||
#Sort by difference row number, and fill in any blanks in the reference-row column
|
||||
$ExpandedDiff = $ExpandedDiff | Sort-Object -Property ">row"
|
||||
for ($i = 1; $i -lt $ExpandedDiff.Count; $i++) {if (-not $ExpandedDiff[$i]."<row") {$ExpandedDiff[$i]."<row" = $ExpandedDiff[$i-1]."<row" } }
|
||||
|
||||
#if we had to put the equal rows back, take them out; sort, make sure all the columns are present in row 1 so the grid puts them in, and output
|
||||
if ( $ExcludeDifferent) {$ExpandedDiff = $ExpandedDiff.where({$_.side -eq "=="}) | Sort-Object -Property "<row" ,">row" }
|
||||
elseif ( $IncludeEqual) {$ExpandedDiff = $ExpandedDiff | Sort-Object -Property "<row" ,">row" }
|
||||
else {$ExpandedDiff = $ExpandedDiff.where({$_.side -ne "=="}) | Sort-Object -Property "<row" ,">row" }
|
||||
$ExpandedDiff | Update-FirstObjectProperties | Out-GridView -Title "Comparing $Referencefile::$worksheet1 (<=) with $Differencefile::$WorkSheet2 (=>)"
|
||||
}
|
||||
elseif ($GridView ) {Write-Warning -Message "To use -GridView you must specify -Key and it must match one of the included properties." }
|
||||
elseif (-not $PassThru) {return ($diff | Select-Object -Property (@(@{n="_Side";e={$_.SideIndicator}},"_File" ,"_Sheet","_Row") + $propList))}
|
||||
if ( $PassThru) {return $diff }
|
||||
}
|
||||
28
DoTests.ps1
28
DoTests.ps1
@@ -1,28 +0,0 @@
|
||||
param(
|
||||
[Switch]$DontCreateZip
|
||||
)
|
||||
|
||||
##
|
||||
# Used in Appveyor.yml
|
||||
##
|
||||
|
||||
$PSVersionTable.PSVersion
|
||||
|
||||
## Create the zip before the tests run
|
||||
## Otherwise the EPPlus.dll is in use after the Pester run
|
||||
$ModuleVersion = (Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content -Raw .\ImportExcel.psd1)))).moduleVersion
|
||||
|
||||
if (!$DontCreateZip) {
|
||||
$dest = "ImportExcel-{0}-{1}.zip" -f $ModuleVersion, (Get-Date).ToString("yyyyMMddHHmmss")
|
||||
Compress-Archive -Path . -DestinationPath .\$dest
|
||||
}
|
||||
|
||||
if ($null -eq (Get-Module -ListAvailable pester)) {
|
||||
Install-Module -Name Pester -Repository PSGallery -Force -Scope CurrentUser
|
||||
}
|
||||
|
||||
$result = Invoke-Pester -Script $PSScriptRoot\__tests__ -Verbose -PassThru
|
||||
|
||||
if ($result.FailedCount -gt 0) {
|
||||
throw "$($result.FailedCount) tests failed."
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
Import-Module ..\ImportExcel.psd1 -Force
|
||||
try {. $PSScriptRoot\..\..\LoadPSD1.ps1} catch {}
|
||||
|
||||
$file = "C:\Temp\test.xlsx"
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
|
||||
. $PSScriptRoot\Charting.ps1
|
||||
. $PSScriptRoot\ColorCompletion.ps1
|
||||
. $PSScriptRoot\ConvertExcelToImageFile.ps1
|
||||
. $PSScriptRoot\compare-workSheet.ps1
|
||||
. $PSScriptRoot\Compare-WorkSheet.ps1
|
||||
. $PSScriptRoot\ConvertFromExcelData.ps1
|
||||
. $PSScriptRoot\ConvertFromExcelToSQLInsert.ps1
|
||||
. $PSScriptRoot\ConvertToExcelXlsx.ps1
|
||||
|
||||
238
Install.ps1
238
Install.ps1
@@ -1,104 +1,174 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Download the module files from GitHub.
|
||||
|
||||
.DESCRIPTION
|
||||
Download the module files from GitHub to the local client in the module folder.
|
||||
Installs module from Git clone or directly from GitHub.
|
||||
File must not have BOM for GitHub deploy to work.
|
||||
#>
|
||||
|
||||
[CmdLetBinding()]
|
||||
[CmdletBinding(DefaultParameterSetName = 'Default')]
|
||||
Param (
|
||||
# Path to install the module to, if not provided -Scope used.
|
||||
[Parameter(Mandatory, ParameterSetName = 'ModulePath')]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$ModuleName = 'ImportExcel',
|
||||
[String]$InstallDirectory,
|
||||
[String]$ModulePath,
|
||||
|
||||
# Path to install the module to, PSModulePath "CurrentUser" or "AllUsers", if not provided "CurrentUser" used.
|
||||
[Parameter(Mandatory, ParameterSetName = 'Scope')]
|
||||
[ValidateSet('CurrentUser', 'AllUsers')]
|
||||
[string]
|
||||
$Scope = 'CurrentUser',
|
||||
|
||||
# Get module from GitHub instead of local Git clone, for example "https://raw.githubusercontent.com/ili101/Module.Template/master/Install.ps1"
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$GitPath = 'https://raw.github.com/dfinke/ImportExcel/master'
|
||||
[Uri]$FromGitHub
|
||||
)
|
||||
# Set Files and Folders patterns to Include/Exclude.
|
||||
$IncludeFiles = @(
|
||||
'*.dll',
|
||||
'*.psd1',
|
||||
'*.psm1',
|
||||
'*.ps1',
|
||||
'BinFolder*'
|
||||
)
|
||||
$ExcludeFiles = @(
|
||||
'Install.ps1'
|
||||
)
|
||||
|
||||
Begin {
|
||||
Try {
|
||||
Write-Verbose "$ModuleName module installation started"
|
||||
|
||||
$Files = @(
|
||||
'AddConditionalFormatting.ps1',
|
||||
'Charting.ps1',
|
||||
'ColorCompletion.ps1',
|
||||
'ConvertFromExcelData.ps1',
|
||||
'ConvertFromExcelToSQLInsert.ps1',
|
||||
'ConvertExcelToImageFile.ps1',
|
||||
'ConvertToExcelXlsx.ps1',
|
||||
'Copy-ExcelWorkSheet.ps1',
|
||||
'EPPlus.dll',
|
||||
'Export-charts.ps1',
|
||||
'Export-Excel.ps1',
|
||||
'Export-ExcelSheet.ps1',
|
||||
'formatting.ps1',
|
||||
'Get-ExcelColumnName.ps1',
|
||||
'Get-ExcelSheetInfo.ps1',
|
||||
'Get-ExcelWorkbookInfo.ps1',
|
||||
'Get-HtmlTable.ps1',
|
||||
'Get-Range.ps1',
|
||||
'Get-XYRange.ps1',
|
||||
'Import-Html.ps1',
|
||||
'ImportExcel.psd1',
|
||||
'ImportExcel.psm1',
|
||||
'InferData.ps1',
|
||||
'Invoke-Sum.ps1',
|
||||
'New-ConditionalFormattingIconSet.ps1',
|
||||
'New-ConditionalText.ps1',
|
||||
'New-ExcelChart.ps1',
|
||||
'New-PSItem.ps1',
|
||||
'Open-ExcelPackage.ps1',
|
||||
'Pivot.ps1',
|
||||
'plot.ps1',
|
||||
'Send-SqlDataToExcel.ps1',
|
||||
'Set-CellStyle.ps1',
|
||||
'Set-Column.ps1',
|
||||
'Set-Row.ps1',
|
||||
'SetFormat.ps1',
|
||||
'TrackingUtils.ps1',
|
||||
'Update-FirstObjectProperties.ps1'
|
||||
)
|
||||
function Invoke-MultiLike {
|
||||
[alias("LikeAny")]
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
$InputObject,
|
||||
[Parameter(Mandatory)]
|
||||
[String[]]$Filters,
|
||||
[Switch]$Not
|
||||
)
|
||||
$FiltersRegex = foreach ($Filter In $Filters) {
|
||||
$Filter = [regex]::Escape($Filter)
|
||||
if ($Filter -match "^\\\*") {
|
||||
$Filter = $Filter.Remove(0, 2)
|
||||
}
|
||||
else {
|
||||
$Filter = '^' + $Filter
|
||||
}
|
||||
if ($Filter -match "\\\*$") {
|
||||
$Filter = $Filter.Substring(0, $Filter.Length - 2)
|
||||
}
|
||||
else {
|
||||
$Filter = $Filter + '$'
|
||||
}
|
||||
$Filter
|
||||
}
|
||||
Catch {
|
||||
throw "Failed installing the module in the install directory '$InstallDirectory': $_"
|
||||
if ($Not) {
|
||||
$InputObject -notmatch ($FiltersRegex -join '|').replace('\*', '.*').replace('\?', '.')
|
||||
}
|
||||
else {
|
||||
$InputObject -match ($FiltersRegex -join '|').replace('\*', '.*').replace('\?', '.')
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
Try {
|
||||
if (-not $InstallDirectory) {
|
||||
Write-Verbose "$ModuleName no installation directory provided"
|
||||
Try {
|
||||
Write-Verbose -Message 'Module installation started'
|
||||
|
||||
$PersonalModules = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath WindowsPowerShell\Modules
|
||||
|
||||
if (($env:PSModulePath -split ';') -notcontains $PersonalModules) {
|
||||
Write-Warning "$ModuleName personal module path '$PersonalModules' not found in '`$env:PSModulePath'"
|
||||
}
|
||||
|
||||
if (-not (Test-Path $PersonalModules)) {
|
||||
Write-Error "$ModuleName path '$PersonalModules' does not exist"
|
||||
}
|
||||
|
||||
$InstallDirectory = Join-Path -Path $PersonalModules -ChildPath $ModuleName
|
||||
Write-Verbose "$ModuleName default installation directory is '$InstallDirectory'"
|
||||
if (!$ModulePath) {
|
||||
if ($Scope -eq 'CurrentUser') {
|
||||
$ModulePathIndex = 0
|
||||
}
|
||||
|
||||
if (-not (Test-Path $InstallDirectory)) {
|
||||
$null = New-Item -Path $InstallDirectory -ItemType Directory -EA Stop
|
||||
Write-Verbose "$ModuleName created module folder '$InstallDirectory'"
|
||||
else {
|
||||
$ModulePathIndex = 1
|
||||
}
|
||||
|
||||
$WebClient = New-Object System.Net.WebClient
|
||||
|
||||
$Files | ForEach-Object {
|
||||
$WebClient.DownloadFile("$GitPath/$_","$installDirectory\$_")
|
||||
Write-Verbose "$ModuleName installed module file '$_'"
|
||||
if ($IsLinux -or $IsMacOS) {
|
||||
$ModulePathSeparator = ':'
|
||||
}
|
||||
|
||||
Write-Verbose "$ModuleName module installation successful"
|
||||
else {
|
||||
$ModulePathSeparator = ';'
|
||||
}
|
||||
$ModulePath = ($env:PSModulePath -split $ModulePathSeparator)[$ModulePathIndex]
|
||||
}
|
||||
Catch {
|
||||
throw "Failed installing the module in the install directory '$InstallDirectory': $_"
|
||||
|
||||
# Get $ModuleName, $TargetPath, [$Links]
|
||||
if ($FromGitHub) {
|
||||
# Fix Could not create SSL/TLS secure channel
|
||||
#$SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
|
||||
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
|
||||
$WebClient = [System.Net.WebClient]::new()
|
||||
$GitUri = $FromGitHub.AbsolutePath.Split('/')[1, 2] -join '/'
|
||||
$GitBranch = $FromGitHub.AbsolutePath.Split('/')[3]
|
||||
$Links = (Invoke-RestMethod -Uri "https://api.github.com/repos/$GitUri/contents" -Body @{ref = $GitBranch }) | Where-Object { (LikeAny $_.name $IncludeFiles) -and (LikeAny $_.name $ExcludeFiles -Not) }
|
||||
|
||||
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension(($Links | Where-Object { $_.name -like '*.psm1' }).name)
|
||||
$ModuleVersion = (. ([Scriptblock]::Create((Invoke-WebRequest -Uri ($Links | Where-Object { $_.name -eq "$ModuleName.psd1" }).download_url)))).ModuleVersion
|
||||
}
|
||||
else {
|
||||
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension((Get-ChildItem -File -Filter *.psm1 -Name -Path $PSScriptRoot))
|
||||
$ModuleVersion = (. ([Scriptblock]::Create((Get-Content -Path (Join-Path $PSScriptRoot "$ModuleName.psd1") | Out-String)))).ModuleVersion
|
||||
}
|
||||
$TargetPath = Join-Path -Path $ModulePath -ChildPath $ModuleName
|
||||
$TargetPath = Join-Path -Path $TargetPath -ChildPath $ModuleVersion
|
||||
|
||||
# Create Directory
|
||||
if (-not (Test-Path -Path $TargetPath)) {
|
||||
$null = New-Item -Path $TargetPath -ItemType Directory -ErrorAction Stop
|
||||
Write-Verbose -Message ('Created module folder: "{0}"' -f $TargetPath)
|
||||
}
|
||||
|
||||
# Copy Files
|
||||
if ($FromGitHub) {
|
||||
foreach ($Link in $Links) {
|
||||
$TargetPathItem = Join-Path -Path $TargetPath -ChildPath $Link.name
|
||||
if ($Link.type -ne 'dir') {
|
||||
$WebClient.DownloadFile($Link.download_url, $TargetPathItem)
|
||||
Write-Verbose -Message ('Installed module file: "{0}"' -f $Link.name)
|
||||
}
|
||||
else {
|
||||
if (-not (Test-Path -Path $TargetPathItem)) {
|
||||
$null = New-Item -Path $TargetPathItem -ItemType Directory -ErrorAction Stop
|
||||
Write-Verbose -Message 'Created module folder: "{0}"' -f $TargetPathItem
|
||||
}
|
||||
$SubLinks = (Invoke-RestMethod -Uri $Link.git_url -Body @{recursive = '1' }).tree
|
||||
foreach ($SubLink in $SubLinks) {
|
||||
$TargetPathSub = Join-Path -Path $TargetPathItem -ChildPath $SubLink.path
|
||||
if ($SubLink.'type' -EQ 'tree') {
|
||||
if (-not (Test-Path -Path $TargetPathSub)) {
|
||||
$null = New-Item -Path $TargetPathSub -ItemType Directory -ErrorAction Stop
|
||||
Write-Verbose -Message 'Created module folder: "{0}"' -f $TargetPathSub
|
||||
}
|
||||
}
|
||||
else {
|
||||
$WebClient.DownloadFile(
|
||||
('https://raw.githubusercontent.com/{0}/{1}/{2}/{3}' -f $GitUri, $GitBranch, $Link.name, $SubLink.path),
|
||||
$TargetPathSub
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Get-ChildItem -Path $PSScriptRoot -Exclude $ExcludeFiles | Where-Object { LikeAny $_.Name $IncludeFiles } | ForEach-Object {
|
||||
if ($_.Attributes -ne 'Directory') {
|
||||
Copy-Item -Path $_ -Destination $TargetPath
|
||||
Write-Verbose -Message ('Installed module file "{0}"' -f $_)
|
||||
}
|
||||
else {
|
||||
Copy-Item -Path $_ -Destination $TargetPath -Recurse -Force
|
||||
Write-Verbose -Message ('Installed module folder "{0}"' -f $_)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Import Module
|
||||
Write-Verbose -Message "$ModuleName module installation successful to $TargetPath"
|
||||
Import-Module -Name $ModuleName -Force
|
||||
Write-Verbose -Message "Module installed"
|
||||
}
|
||||
Catch {
|
||||
throw ('Failed installing module "{0}". Error: "{1}" in Line {2}' -f $ModuleName, $_, $_.InvocationInfo.ScriptLineNumber)
|
||||
}
|
||||
finally {
|
||||
#if ($FromGitHub) {
|
||||
# [Net.ServicePointManager]::SecurityProtocol = $SecurityProtocol
|
||||
#}
|
||||
Write-Verbose -Message 'Module installation end'
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
$fullPath = 'C:\Program Files\WindowsPowerShell\Modules\ImportExcel'
|
||||
|
||||
Robocopy . $fullPath /mir /XD .vscode .git examples testimonials images spikes /XF appveyor.yml .gitattributes .gitignore
|
||||
12
README.md
12
README.md
@@ -15,11 +15,13 @@ If this project helped you reduce the time to get your job done, let me know.
|
||||
<a href="./LICENSE.txt"><img src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a>
|
||||
</p>
|
||||
|
||||
|CI System |OS|Status|
|
||||
|---|---|---|
|
||||
|Azure DevOps|Windows|[](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=10)|
|
||||
|Azure DevOps|Windows, Linux, Mac|[](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=20&branchName=master)|
|
||||
|Appveyor|Windows|[](https://ci.appveyor.com/project/dfinke/importexcel/branch/master)|
|
||||
| CI System | Environment | Status |
|
||||
|--------------|-------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| AppVeyor | Windows, Core preview, Ubuntu | [](https://ci.appveyor.com/project/ili101/Module-Template) |
|
||||
| Azure DevOps | Windows | [](https://dev.azure.com/ili101/ImportExcel/_build/latest?definitionId=1&branchName=master) |
|
||||
| Azure DevOps | Windows (Core) | [](https://dev.azure.com/ili101/ImportExcel/_build/latest?definitionId=1&branchName=master) |
|
||||
| Azure DevOps | Ubuntu | [](https://dev.azure.com/ili101/ImportExcel/_build/latest?definitionId=1&branchName=master) |
|
||||
| Azure DevOps | macOS | [](https://dev.azure.com/ili101/ImportExcel/_build/latest?definitionId=1&branchName=master) |
|
||||
|
||||
<!-- /BADGES -->
|
||||
|
||||
|
||||
132
__tests__/CI.ps1
Normal file
132
__tests__/CI.ps1
Normal file
@@ -0,0 +1,132 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Handel Continuous Integration Testing in AppVeyor and Azure DevOps Pipelines.
|
||||
#>
|
||||
param
|
||||
(
|
||||
# AppVeyor Only - Update AppVeyor build name.
|
||||
[Switch]$Initialize,
|
||||
# Installs the module and invoke the Pester tests with the current version of PowerShell.
|
||||
[Switch]$Test,
|
||||
# AppVeyor Only - Upload results to AppVeyor "Tests" tab.
|
||||
[Switch]$Finalize,
|
||||
# AppVeyor and Azure - Upload module as AppVeyor Artifact.
|
||||
[Switch]$Artifact
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
if ($Initialize) {
|
||||
$Psd1 = (Get-ChildItem -File -Filter *.psd1 -Name -Path (Split-Path $PSScriptRoot)).PSPath
|
||||
$ModuleVersion = (. ([Scriptblock]::Create((Get-Content -Path $Psd1 | Out-String)))).ModuleVersion
|
||||
Update-AppveyorBuild -Version "$ModuleVersion ($env:APPVEYOR_BUILD_NUMBER) $env:APPVEYOR_REPO_BRANCH"
|
||||
}
|
||||
if ($Test) {
|
||||
function Get-EnvironmentInfo {
|
||||
if ($null -eq $IsWindows -or $IsWindows) {
|
||||
# Get Windows Version
|
||||
try {
|
||||
$WinRelease, $WinVer = Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ReleaseId, CurrentMajorVersionNumber, CurrentMinorVersionNumber, CurrentBuildNumber, UBR
|
||||
$WindowsVersion = "$($WinVer -join '.') ($WinRelease)"
|
||||
}
|
||||
catch {
|
||||
$WindowsVersion = [System.Environment]::OSVersion.Version
|
||||
}
|
||||
|
||||
# Get .Net Version
|
||||
# https://stackoverflow.com/questions/3487265/powershell-script-to-return-versions-of-net-framework-on-a-machine
|
||||
$Lookup = @{
|
||||
378389 = [version]'4.5'
|
||||
378675 = [version]'4.5.1'
|
||||
378758 = [version]'4.5.1'
|
||||
379893 = [version]'4.5.2'
|
||||
393295 = [version]'4.6'
|
||||
393297 = [version]'4.6'
|
||||
394254 = [version]'4.6.1'
|
||||
394271 = [version]'4.6.1'
|
||||
394802 = [version]'4.6.2'
|
||||
394806 = [version]'4.6.2'
|
||||
460798 = [version]'4.7'
|
||||
460805 = [version]'4.7'
|
||||
461308 = [version]'4.7.1'
|
||||
461310 = [version]'4.7.1'
|
||||
461808 = [version]'4.7.2'
|
||||
461814 = [version]'4.7.2'
|
||||
528040 = [version]'4.8'
|
||||
528049 = [version]'4.8'
|
||||
}
|
||||
|
||||
# For One True framework (latest .NET 4x), change the Where-Object match
|
||||
# to PSChildName -eq "Full":
|
||||
$DotNetVersion = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
|
||||
Get-ItemProperty -name Version, Release -EA 0 |
|
||||
Where-Object { $_.PSChildName -eq "Full" } |
|
||||
Select-Object @{name = ".NET Framework"; expression = { $_.PSChildName } },
|
||||
@{name = "Product"; expression = { $Lookup[$_.Release] } },
|
||||
Version, Release
|
||||
|
||||
# Output
|
||||
[PSCustomObject]($PSVersionTable + @{
|
||||
ComputerName = $env:Computername
|
||||
WindowsVersion = $WindowsVersion
|
||||
'.Net Version' = '{0} (Version: {1}, Release: {2})' -f $DotNetVersion.Product, $DotNetVersion.Version, $DotNetVersion.Release
|
||||
#EnvironmentPath = $env:Path
|
||||
})
|
||||
}
|
||||
else {
|
||||
# Output
|
||||
[PSCustomObject]($PSVersionTable + @{
|
||||
ComputerName = $env:Computername
|
||||
#EnvironmentPath = $env:Path
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
'[Info] Testing On:'
|
||||
Get-EnvironmentInfo
|
||||
'[Progress] Installing Module.'
|
||||
. .\Install.ps1
|
||||
'[Progress] Invoking Pester.'
|
||||
Invoke-Pester -OutputFile ('TestResultsPS{0}.xml' -f $PSVersionTable.PSVersion)
|
||||
}
|
||||
if ($Finalize) {
|
||||
'[Progress] Finalizing.'
|
||||
$Failure = $false
|
||||
$AppVeyorResultsUri = 'https://ci.appveyor.com/api/testresults/nunit/{0}' -f $env:APPVEYOR_JOB_ID
|
||||
foreach ($TestResultsFile in Get-ChildItem -Path 'TestResultsPS*.xml') {
|
||||
$TestResultsFilePath = $TestResultsFile.FullName
|
||||
"[Info] Uploading Files: $AppVeyorResultsUri, $TestResultsFilePath."
|
||||
# Add PowerShell version to test results
|
||||
$PSVersion = $TestResultsFile.Name.Replace('TestResults', '').Replace('.xml', '')
|
||||
[Xml]$Xml = Get-Content -Path $TestResultsFilePath
|
||||
Select-Xml -Xml $Xml -XPath '//test-case' | ForEach-Object { $_.Node.name = "$PSVersion " + $_.Node.name }
|
||||
$Xml.OuterXml | Out-File -FilePath $TestResultsFilePath
|
||||
|
||||
#Invoke-RestMethod -Method Post -Uri $AppVeyorResultsUri -Body $Xml
|
||||
[Net.WebClient]::new().UploadFile($AppVeyorResultsUri, $TestResultsFilePath)
|
||||
|
||||
if ($Xml.'test-results'.failures -ne '0') {
|
||||
$Failure = $true
|
||||
}
|
||||
}
|
||||
if ($Failure) {
|
||||
throw 'Tests failed.'
|
||||
}
|
||||
}
|
||||
if ($Artifact) {
|
||||
# Get Module Info
|
||||
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension((Get-ChildItem -File -Filter *.psm1 -Name -Path (Split-Path $PSScriptRoot)))
|
||||
$ModulePath = (Get-Module -Name $ModuleName -ListAvailable).ModuleBase | Split-Path
|
||||
$VersionLocal = ((Get-Module -Name $ModuleName -ListAvailable).Version | Measure-Object -Maximum).Maximum
|
||||
"[Progress] Artifact Start for Module: $ModuleName, Version: $VersionLocal."
|
||||
if ($env:APPVEYOR) {
|
||||
$ZipFileName = "{0} {1} {2} {3:yyyy-MM-dd HH-mm-ss}.zip" -f $ModuleName, $VersionLocal, $env:APPVEYOR_REPO_BRANCH, (Get-Date)
|
||||
$ZipFileFullPath = Join-Path -Path $PSScriptRoot -ChildPath $ZipFileName
|
||||
"[Info] Artifact. $ModuleName, ZipFileName: $ZipFileName."
|
||||
#Compress-Archive -Path $ModulePath -DestinationPath $ZipFileFullPath
|
||||
[System.IO.Compression.ZipFile]::CreateFromDirectory($ModulePath, $ZipFileFullPath, [System.IO.Compression.CompressionLevel]::Optimal, $true)
|
||||
Push-AppveyorArtifact $ZipFileFullPath -DeploymentName $ModuleName
|
||||
}
|
||||
elseif ($env:AGENT_NAME) {
|
||||
#Write-Host "##vso[task.setvariable variable=ModuleName]$ModuleName"
|
||||
Copy-Item -Path $ModulePath -Destination $env:Build_ArtifactStagingDirectory -Recurse
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
#Requires -Modules Pester
|
||||
if ($PSVersionTable.os -and $PSVersionTable.os -notMatch 'Windows' ) {return} #Currently this test outputs windows services so only run on Windows.
|
||||
if (-not $env:TEMP) {$env:TEMP = [IO.Path]::GetTempPath() }
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
if ($PSVersionTable.PSVersion.Major -gt 5) { Write-Warning "Can't test grid view on V6 and later" }
|
||||
else {Add-Type -AssemblyName System.Windows.Forms }
|
||||
Describe "Compare Worksheet" {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
if (-not $env:TEMP) {$env:TEMP = [IO.Path]::GetTempPath() -replace "/$","" }
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
$xlFile = "$env:TEMP\testSQL.xlsx"
|
||||
|
||||
Describe "ConvertFrom-ExcelToSQLInsert" {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#Requires -Modules Pester
|
||||
remove-module importExcel -erroraction silentlyContinue
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
#remove-module importExcel -erroraction silentlyContinue
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
|
||||
Describe "Check if Function aliases exist" {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1
|
||||
#Import-Module $PSScriptRoot\..\..\ImportExcel.psd1
|
||||
|
||||
Describe "Tests" {
|
||||
BeforeAll {
|
||||
|
||||
26
__tests__/InstallPowerShell.ps1
Normal file
26
__tests__/InstallPowerShell.ps1
Normal file
@@ -0,0 +1,26 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Installs PowerShell Core on Windows.
|
||||
#>
|
||||
[CmdLetBinding()]
|
||||
Param
|
||||
(
|
||||
# Version to install in the format from the .msi, for example "7.0.0-preview.1"
|
||||
[Parameter(Mandatory)]
|
||||
[String]$Version
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
'[Progress] Downloading PowerShell Core.'
|
||||
$MsiPath = Join-Path $env:TEMP "PowerShell-$Version-win-x64.msi"
|
||||
[System.Net.WebClient]::new().DownloadFile("https://github.com/PowerShell/PowerShell/releases/download/v$Version/PowerShell-$Version-win-x64.msi", $MsiPath)
|
||||
|
||||
'[Progress] Installing PowerShell Core.'
|
||||
Start-Process 'msiexec.exe' -Wait -ArgumentList "/i $MsiPath /quiet"
|
||||
Remove-Item -Path $MsiPath
|
||||
$PowerShellFolder = $Version[0]
|
||||
if ($Version -like "*preview*") {
|
||||
$PowerShellFolder += '-preview'
|
||||
}
|
||||
$env:Path = "$env:ProgramFiles\PowerShell\$PowerShellFolder;$env:Path"
|
||||
'[Progress] PowerShell Core Installed.'
|
||||
105
__tests__/Publish.ps1
Normal file
105
__tests__/Publish.ps1
Normal file
@@ -0,0 +1,105 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Deploy module to PowerShellGallery.
|
||||
#>
|
||||
[CmdletBinding(DefaultParameterSetName = 'ModuleName')]
|
||||
Param
|
||||
(
|
||||
# The name of the installed module to be deployed, if not provided the name of the .psm1 file in the parent folder is used.
|
||||
[Parameter(ParameterSetName = 'ModuleName')]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$ModuleName,
|
||||
|
||||
# Publish module from path (module folder), if not provided -ModuleName is used.
|
||||
[Parameter(Mandatory, ParameterSetName = 'Path')]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$Path,
|
||||
|
||||
# Key for PowerShellGallery deployment, if not provided $env:NugetApiKey is used.
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$NugetApiKey,
|
||||
|
||||
# Skip Version verification for PowerShellGallery deployment, can be used for first release.
|
||||
[Switch]$Force
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
if ($Path) {
|
||||
$Path = Resolve-Path -Path $Path
|
||||
if ($Path.Count -ne 1) {
|
||||
throw ('Invalid Path, $Path.Count: {0}.' -f $Path.Count)
|
||||
}
|
||||
$Psd1Path = (Get-ChildItem -File -Filter *.psd1 -Path $Path -Recurse)[0].FullName
|
||||
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension($Psd1Path)
|
||||
$VersionLocal = (. ([Scriptblock]::Create((Get-Content -Path $Psd1Path | Out-String)))).ModuleVersion
|
||||
}
|
||||
else {
|
||||
# Get Script Root
|
||||
if ($PSScriptRoot) {
|
||||
$ScriptRoot = $PSScriptRoot
|
||||
}
|
||||
elseif ($psISE.CurrentFile.IsUntitled -eq $false) {
|
||||
$ScriptRoot = Split-Path -Path $psISE.CurrentFile.FullPath
|
||||
}
|
||||
elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') {
|
||||
$ScriptRoot = Split-Path -Path $psEditor.GetEditorContext().CurrentFile.Path
|
||||
}
|
||||
else {
|
||||
$ScriptRoot = '.'
|
||||
}
|
||||
|
||||
# Get Module Info
|
||||
if (!$ModuleName) {
|
||||
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension((Get-ChildItem -File -Filter *.psm1 -Name -Path (Split-Path $ScriptRoot)))
|
||||
}
|
||||
$VersionLocal = ((Get-Module -Name $ModuleName -ListAvailable).Version | Measure-Object -Maximum).Maximum
|
||||
}
|
||||
|
||||
"[Progress] Deploy Script Start for Module: $ModuleName, Version: $VersionLocal."
|
||||
|
||||
# Deploy to PowerShell Gallery if run locally OR from AppVeyor & GitHub master
|
||||
if (!$env:APPVEYOR -or $env:APPVEYOR_REPO_BRANCH -eq 'master') {
|
||||
if ($env:APPVEYOR) {
|
||||
$Success = $true
|
||||
$AppVeyorProject = Invoke-RestMethod -Uri "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG"
|
||||
$AppVeyorProject.build.jobs | ForEach-Object {
|
||||
'[Info] AppVeyor job name: "{0}", Id: {1}, Status: {2}.' -f $_.name, $_.jobId, $_.status
|
||||
if ($_.jobId -ne $env:APPVEYOR_JOB_ID -and $_.status -ne "success") {
|
||||
$Success = $false
|
||||
}
|
||||
}
|
||||
if (!$Success) {
|
||||
'[Info] There are filed jobs skipping PowerShell Gallery deploy.'
|
||||
break
|
||||
}
|
||||
}
|
||||
try {
|
||||
$VersionGallery = (Find-Module -Name $ModuleName -ErrorAction Stop).Version
|
||||
}
|
||||
catch {
|
||||
if ($_.Exception.Message -notlike 'No match was found for the specified search criteria*' -or !$Force) {
|
||||
throw $_
|
||||
}
|
||||
}
|
||||
|
||||
"[Info] PowerShellGallery. $ModuleName, VersionGallery: $VersionGallery, VersionLocal: $VersionLocal."
|
||||
if ($VersionGallery -lt $VersionLocal -or $Force) {
|
||||
if (!$NugetApiKey) {
|
||||
$NugetApiKey = $env:NugetApiKey
|
||||
}
|
||||
"[Info] PowerShellGallery. Deploying $ModuleName version $VersionLocal."
|
||||
if ($Path) {
|
||||
Publish-Module -NuGetApiKey $NugetApiKey -Path $Path
|
||||
}
|
||||
else {
|
||||
Publish-Module -NuGetApiKey $NugetApiKey -Name $ModuleName -RequiredVersion $VersionLocal
|
||||
}
|
||||
}
|
||||
else {
|
||||
'[Info] PowerShellGallery Deploy Skipped (Version Check).'
|
||||
}
|
||||
}
|
||||
else {
|
||||
'[Info] PowerShellGallery Deploy Skipped.'
|
||||
}
|
||||
'[Progress] Deploy Ended.'
|
||||
@@ -1,6 +1,5 @@
|
||||
#Requires -Modules Pester
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
if (-not $env:TEMP) {$env:TEMP = [IO.Path]::GetTempPath() -replace "/$","" }
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
Describe "Remove Worksheet" {
|
||||
Context "Remove a worksheet output" {
|
||||
|
||||
72
appveyor.yml
72
appveyor.yml
@@ -1,17 +1,67 @@
|
||||
image:
|
||||
- Visual Studio 2015
|
||||
# - Ubuntu
|
||||
# Version format
|
||||
version: '({build})'
|
||||
|
||||
# Build worker image (VM templates)
|
||||
image:
|
||||
- Ubuntu1804
|
||||
- 'Visual Studio 2019'
|
||||
|
||||
# Fix CRLF on Windows
|
||||
init:
|
||||
- cmd: 'git config --global --unset core.autocrlf'
|
||||
|
||||
# To disable automatic builds
|
||||
build: off
|
||||
|
||||
test_script:
|
||||
- ps: .\DoTests.ps1
|
||||
- pwsh: .\DoTests.ps1 -DontCreateZip
|
||||
|
||||
# Skipping commits with particular message or from specific user
|
||||
skip_commits:
|
||||
message: '/\[skip av\]/'
|
||||
files:
|
||||
- README.md
|
||||
- '*.md'
|
||||
|
||||
artifacts:
|
||||
- path: ImportExcel*.zip
|
||||
name: ImportExcel
|
||||
# Including commits with particular message or from specific user
|
||||
#only_commits:
|
||||
# message: '/\[build\]/' # Start a new build if message contains 'build'
|
||||
|
||||
# Scripts that run after cloning repository
|
||||
install:
|
||||
- ps: 'Install-Module -Name Pester -Force -SkipPublisherCheck'
|
||||
- ps: 'Install-Module -Name Assert -Force'
|
||||
# PowerShell Core
|
||||
- ps: '& .\__tests__\InstallPowerShell.ps1 -Version "7.0.0-preview.3"' # Install other PowerShell Core version (Optional)
|
||||
- pwsh: 'Install-Module -Name Pester -Force'
|
||||
- pwsh: 'Install-Module -Name Assert -Force'
|
||||
|
||||
# To run your custom scripts instead of automatic tests
|
||||
test_script:
|
||||
- ps: '& .\__tests__\CI.ps1 -Test'
|
||||
- pwsh: '& .\__tests__\CI.ps1 -Test'
|
||||
- ps: '& .\__tests__\CI.ps1 -Finalize' # Collect and upload results
|
||||
|
||||
# Deploy
|
||||
deploy_script:
|
||||
- ps: '& .\__tests__\CI.ps1 -Artifact'
|
||||
- ps: '$null = Install-PackageProvider -Name NuGet -Force ; & .\__tests__\Publish.ps1'
|
||||
|
||||
# Linux setup
|
||||
for:
|
||||
-
|
||||
matrix:
|
||||
only:
|
||||
- image: Ubuntu1804
|
||||
# Install other PowerShell Core version (Optional)
|
||||
init:
|
||||
- sh: 'sudo apt-get -qq update && sudo apt-get -qq install powershell-preview && sudo rm /usr/bin/pwsh && sudo ln -s /opt/microsoft/powershell/7-preview/pwsh /usr/bin/pwsh'
|
||||
- sh: 'export LANG=en_US.UTF-8' # Fix for PowerShell 7.0.0-preview.2, Remove if using other version.
|
||||
# Scripts that run after cloning repository
|
||||
install:
|
||||
- pwsh: '& .\__tests__\CI.ps1 -Initialize' # Set AppVeyor build version
|
||||
- pwsh: 'Install-Module -Name Pester -Force'
|
||||
- pwsh: 'Install-Module -Name Assert -Force'
|
||||
# To run your custom scripts instead of automatic tests
|
||||
test_script:
|
||||
- pwsh: '& .\__tests__\CI.ps1 -Test'
|
||||
- pwsh: '& .\__tests__\CI.ps1 -Finalize' # Collect and upload results
|
||||
# Skip Deploy
|
||||
deploy_script:
|
||||
- pwsh: '"Deploy skiped on Linux."'
|
||||
@@ -1,44 +1,91 @@
|
||||
jobs:
|
||||
- job: Build_PS_Win2016
|
||||
pool:
|
||||
vmImage: vs2017-win2016
|
||||
steps:
|
||||
- powershell: |
|
||||
.\DoTests.ps1
|
||||
displayName: 'Run Tests on Windows'
|
||||
|
||||
- job: Build_PSCore_Ubuntu1604
|
||||
|
||||
pool:
|
||||
vmImage: ubuntu-16.04
|
||||
|
||||
steps:
|
||||
- script: |
|
||||
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
|
||||
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y powershell
|
||||
displayName: 'Install PowerShell Core'
|
||||
|
||||
- script: |
|
||||
pwsh -c '.\DoTests.ps1'
|
||||
displayName: 'Run Tests on Linux'
|
||||
|
||||
- job: Build_PSCore_MacOS1013
|
||||
pool:
|
||||
vmImage: xcode9-macos10.13
|
||||
steps:
|
||||
- script: |
|
||||
brew update
|
||||
brew tap caskroom/cask
|
||||
brew cask install powershell
|
||||
displayName: 'Install PowerShell Core'
|
||||
|
||||
- script: |
|
||||
pwsh -c '.\DoTests.ps1'
|
||||
displayName: 'Run Tests on macOS'
|
||||
# Starter pipeline
|
||||
# Start with a minimal pipeline that you can customize to build and deploy your code.
|
||||
# Add steps that build, run tests, deploy, and more:
|
||||
# https://aka.ms/yaml
|
||||
|
||||
trigger:
|
||||
branches:
|
||||
include:
|
||||
- '*'
|
||||
# - master
|
||||
# - releases/*
|
||||
paths:
|
||||
exclude:
|
||||
- README.md
|
||||
- CHANGELOG.md
|
||||
|
||||
jobs:
|
||||
- job: Windows
|
||||
pool:
|
||||
vmImage: 'windows-latest'
|
||||
|
||||
steps:
|
||||
- powershell: 'Install-Module -Name Pester -Force -SkipPublisherCheck'
|
||||
displayName: 'Update Pester'
|
||||
- powershell: './__tests__/CI.ps1 -Test'
|
||||
displayName: 'Install and Test'
|
||||
|
||||
- task: PublishTestResults@2
|
||||
inputs:
|
||||
testResultsFormat: 'NUnit'
|
||||
testResultsFiles: '**/TestResults*.xml'
|
||||
failTaskOnFailedTests: true
|
||||
|
||||
- powershell: './__tests__/CI.ps1 -Artifact'
|
||||
displayName: 'Prepare Artifact'
|
||||
- task: PublishPipelineArtifact@1
|
||||
inputs:
|
||||
targetPath: '$(Build.ArtifactStagingDirectory)'
|
||||
artifact: 'Modules'
|
||||
- task: PublishPipelineArtifact@1
|
||||
inputs:
|
||||
targetPath: '$(Build.SourcesDirectory)'
|
||||
artifact: 'Source'
|
||||
|
||||
- job: WindowsPSCore
|
||||
pool:
|
||||
vmImage: 'windows-latest'
|
||||
|
||||
steps:
|
||||
- pwsh: 'Install-Module -Name Pester -Force'
|
||||
displayName: 'Update Pester'
|
||||
- pwsh: './__tests__/CI.ps1 -Test'
|
||||
displayName: 'Install and Test'
|
||||
|
||||
- task: PublishTestResults@2
|
||||
inputs:
|
||||
testResultsFormat: 'NUnit'
|
||||
testResultsFiles: '**/TestResults*.xml'
|
||||
failTaskOnFailedTests: true
|
||||
|
||||
- job: Ubuntu
|
||||
pool:
|
||||
vmImage: 'ubuntu-latest'
|
||||
|
||||
steps:
|
||||
- powershell: 'Install-Module -Name Pester -Force'
|
||||
displayName: 'Update Pester'
|
||||
- powershell: './__tests__/CI.ps1 -Test'
|
||||
displayName: 'Install and Test'
|
||||
|
||||
- task: PublishTestResults@2
|
||||
inputs:
|
||||
testResultsFormat: 'NUnit'
|
||||
testResultsFiles: '**/TestResults*.xml'
|
||||
failTaskOnFailedTests: true
|
||||
|
||||
- job: macOS
|
||||
pool:
|
||||
vmImage: 'macOS-latest'
|
||||
|
||||
steps:
|
||||
- powershell: 'Install-Module -Name Pester -Force'
|
||||
displayName: 'Update Pester'
|
||||
- powershell: './__tests__/CI.ps1 -Test'
|
||||
displayName: 'Install and Test'
|
||||
|
||||
- task: PublishTestResults@2
|
||||
inputs:
|
||||
testResultsFormat: 'NUnit'
|
||||
testResultsFiles: '**/TestResults*.xml'
|
||||
failTaskOnFailedTests: true
|
||||
Reference in New Issue
Block a user