Skip to content

Commit 95822ac

Browse files
Standardise file cleanup
1 parent c4d20a9 commit 95822ac

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

Examples/ConditionalFormatting/Top10-DataBar-TwoColorScale.ps1

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
22

3-
$xlfile = "$env:TEMP\test.xlsx"
4-
Remove-Item $xlfile -ErrorAction SilentlyContinue
3+
#Get rid of pre-exisiting sheet
4+
$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx"
5+
Write-Verbose -Verbose -Message "Save location: $xlSourcefile"
6+
Remove-Item $xlSourcefile -ErrorAction Ignore
57

68
$data = ConvertFrom-csv @"
79
Store,January,February,March,April,May,June
@@ -17,12 +19,12 @@ store01,21292,82341,81339,12505,29516,41634
1719
store82,74047,93325,25002,40113,76278,45707
1820
"@
1921

20-
Export-Excel -InputObject $data -Path $xlfile -TableName RawData -WorksheetName RawData
21-
Export-Excel -InputObject $data -Path $xlfile -TableName TopData -WorksheetName StoresTop10Sales
22-
Export-Excel -InputObject $data -Path $xlfile -TableName Databar -WorksheetName StoresSalesDataBar
23-
Export-Excel -InputObject $data -Path $xlfile -TableName TwoColorScale -WorksheetName StoresSalesTwoColorScale
22+
Export-Excel -InputObject $data -Path $xlSourcefile -TableName RawData -WorksheetName RawData
23+
Export-Excel -InputObject $data -Path $xlSourcefile -TableName TopData -WorksheetName StoresTop10Sales
24+
Export-Excel -InputObject $data -Path $xlSourcefile -TableName Databar -WorksheetName StoresSalesDataBar
25+
Export-Excel -InputObject $data -Path $xlSourcefile -TableName TwoColorScale -WorksheetName StoresSalesTwoColorScale
2426

25-
$xl = Open-ExcelPackage -Path $xlfile
27+
$xl = Open-ExcelPackage -Path $xlSourcefile
2628

2729
Set-ExcelRange -Worksheet $xl.StoresTop10Sales -Range $xl.StoresTop10Sales.dimension.address -NumberFormat 'Currency' -AutoSize
2830
Set-ExcelRange -Worksheet $xl.StoresSalesDataBar -Range $xl.StoresSalesDataBar.dimension.address -NumberFormat 'Currency' -AutoSize

Examples/JoinWorksheet/Join-Worksheet.sample.ps1

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
22

33
#Get rid of pre-exisiting sheet
4-
$path = "$Env:TEMP\test.xlsx"
5-
remove-item -Path $path -ErrorAction SilentlyContinue
4+
$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx"
5+
Write-Verbose -Verbose -Message "Save location: $xlSourcefile"
6+
Remove-Item $xlSourcefile -ErrorAction Ignore
67

78
#Create simple pages for 3 stores with product ID, Product Name, quanity price and total
89

@@ -13,7 +14,7 @@ ID,Product,Quantity,Price,Total
1314
12003,Saw,12,15.37,184.44
1415
12010,Drill,20,8,160
1516
12011,Crowbar,7,23.48,164.36
16-
"@ | ConvertFrom-Csv| Export-Excel -Path $path -WorkSheetname Oxford
17+
"@ | ConvertFrom-Csv| Export-Excel -Path $xlSourcefile -WorkSheetname Oxford
1718

1819
@"
1920
ID,Product,Quantity,Price,Total
@@ -22,7 +23,7 @@ ID,Product,Quantity,Price,Total
2223
12003,Saw,10,15.37,153.70
2324
12010,Drill,10,8,80
2425
12012,Pliers,2,14.99,29.98
25-
"@ | ConvertFrom-Csv| Export-Excel -Path $path -WorkSheetname Abingdon
26+
"@ | ConvertFrom-Csv| Export-Excel -Path $xlSourcefile -WorkSheetname Abingdon
2627

2728

2829
@"
@@ -31,7 +32,7 @@ ID,Product,Quantity,Price,Total
3132
12002,Hammer,2,12.10,24.20
3233
12010,Drill,11,8,88
3334
12012,Pliers,3,14.99,44.97
34-
"@ | ConvertFrom-Csv| Export-Excel -Path $path -WorkSheetname Banbury
35+
"@ | ConvertFrom-Csv| Export-Excel -Path $xlSourcefile -WorkSheetname Banbury
3536

3637
#define a pivot table with a chart to show a sales by store, broken down by product
3738
$ptdef = New-PivotTableDefinition -PivotTableName "Summary" -PivotRows "Store" -PivotColumns "Product" -PivotData @{"Total"="SUM"} -IncludePivotChart -ChartTitle "Sales Breakdown" -ChartType ColumnStacked -ChartColumn 10
@@ -42,4 +43,4 @@ $ptdef = New-PivotTableDefinition -PivotTableName "Summary" -PivotRows "Store" -
4243
#Put in a title and freeze to top of the sheet including title and colmun headings
4344
#Add the Pivot table.
4445
#Show the result
45-
Join-Worksheet -Path $path -WorkSheetName "Total" -Clearsheet -FromLabel "Store" -TableName "Combined" -TableStyle Light1 -AutoSize -BoldTopRow -FreezePane 2,1 -Title "Store Sales Summary" -TitleBold -TitleSize 14 -PivotTableDefinition $ptdef -show
46+
Join-Worksheet -Path $xlSourcefile -WorkSheetName "Total" -Clearsheet -FromLabel "Store" -TableName "Combined" -TableStyle Light1 -AutoSize -BoldTopRow -FreezePane 2,1 -Title "Store Sales Summary" -TitleBold -TitleSize 14 -PivotTableDefinition $ptdef -show
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
22

3-
$path = "$env:TEMP\Test.xlsx"
4-
Remove-item -Path $path -ErrorAction SilentlyContinue
3+
#Get rid of pre-exisiting sheet
4+
$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx"
5+
Write-Verbose -Verbose -Message "Save location: $xlSourcefile"
6+
Remove-Item $xlSourcefile -ErrorAction Ignore
7+
58
#Export disk volume, and Network adapter to their own sheets.
69
Get-CimInstance -ClassName Win32_LogicalDisk |
710
Select-Object -Property DeviceId,VolumeName, Size,Freespace |
8-
Export-Excel -Path $path -WorkSheetname Volumes -NumberFormat "0,000"
11+
Export-Excel -Path $xlSourcefile -WorkSheetname Volumes -NumberFormat "0,000"
912
Get-NetAdapter |
1013
Select-Object -Property Name,InterfaceDescription,MacAddress,LinkSpeed |
11-
Export-Excel -Path $path -WorkSheetname NetAdapters
14+
Export-Excel -Path $xlSourcefile -WorkSheetname NetAdapters
1215

1316
#Create a summary page with a title of Summary, label the blocks with the name of the sheet they came from and hide the source sheets
14-
Join-Worksheet -Path $path -HideSource -WorkSheetName Summary -NoHeader -LabelBlocks -AutoSize -Title "Summary" -TitleBold -TitleSize 22 -show
17+
Join-Worksheet -Path $xlSourcefile -HideSource -WorkSheetName Summary -NoHeader -LabelBlocks -AutoSize -Title "Summary" -TitleBold -TitleSize 22 -show

Examples/PivotTable/TableAndPivotTable.ps1

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
$path = "$Env:TEMP\test.xlsx"
2-
remove-item -path $path -ErrorAction SilentlyContinue
1+
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
2+
3+
#Get rid of pre-exisiting sheet
4+
$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx"
5+
Write-Verbose -Verbose -Message "Save location: $xlSourcefile"
6+
Remove-Item $xlSourcefile -ErrorAction Ignore
37

48
#Export some sales data to Excel, format it as a table and put a data-bar in. For this example we won't create the pivot table during the export
59
$excel = ConvertFrom-Csv @"
@@ -10,7 +14,7 @@ Banana, London , 300, 200
1014
Orange, Paris, 600, 500
1115
Banana, Paris, 300, 200
1216
Apple, New York, 1200,700
13-
"@ | Export-Excel -PassThru -Path $path -TableStyle Medium13 -tablename "RawData" -ConditionalFormat @{Range="C2:C7"; DataBarColor="Green"}
17+
"@ | Export-Excel -PassThru -Path $xlSourcefile -TableStyle Medium13 -tablename "RawData" -ConditionalFormat @{Range="C2:C7"; DataBarColor="Green"}
1418

1519
#Add a pivot table, specify its address to put it on the same sheet, use the data that was just exported set the table style and number format.
1620
#Use the "City" for the row names, and "Product" for the columnnames, and sum both the gross and net values for each City/Product combination; add grand totals to rows and columns.

0 commit comments

Comments
 (0)