__tests__/Copy-ExcelWorksheet.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
$path1 = "$env:TEMP\Test1.xlsx"
$path2 = "$env:TEMP\Test2.xlsx"
Remove-item -Path $path1, $path2  -ErrorAction SilentlyContinue

$ProcRange = Get-Process | Export-Excel $path1 -DisplayPropertySet -WorkSheetname Processes -ReturnRange

if ((Get-Culture).NumberFormat.CurrencySymbol -eq "�") {$OtherCurrencySymbol = "$"}
else {$OtherCurrencySymbol = "�"}
[PSCustOmobject][Ordered]@{
    Date             = Get-Date
    Formula1         = '=SUM(F2:G2)'
    String1          = 'My String'
    Float            = [math]::pi
    IPAddress        = '10.10.25.5'
    StrLeadZero      = '07670'
    StrComma         = '0,26'
    StrEngThousand   = '1,234.56'
    StrEuroThousand  = '1.555,83'
    StrDot           = '1.2'
    StrNegInt        = '-31'
    StrTrailingNeg   = '31-'
    StrParens        = '(123)'
    strLocalCurrency = ('{0}123.45' -f (Get-Culture).NumberFormat.CurrencySymbol )
    strOtherCurrency = ('{0}123.45' -f $OtherCurrencySymbol )
    StrE164Phone     = '+32 (444) 444 4444'
    StrAltPhone1     = '+32 4 4444 444'
    StrAltPhone2     = '+3244444444'
    StrLeadSpace     = ' 123'
    StrTrailSpace    = '123 '
    Link1            = [uri]"https://github.com/dfinke/ImportExcel"
    Link2            = "https://github.com/dfinke/ImportExcel"     # Links are not copied correctly, hopefully this will be fixed at some future date
} | Export-Excel  -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -WorkSheetname MixedTypes -Path $path2
Describe "Copy-Worksheet" {
    Context "Simplest copy" {
        BeforeAll {
            Copy-ExcelWorkSheet -SourceWorkbook $path1 -DestinationWorkbook $path2
            $excel = Open-ExcelPackage -Path $path2
            $ws = $excel.Workbook.Worksheets["Processes"]
        }
        it "Inserted a worksheet " {
            $Excel.Workbook.Worksheets.count                            | Should     be 2
            $ws                                                         | Should not benullorEmpty
            $ws.Dimension.Address                                       | should be $ProcRange
        }
    }
    Context "Mixed types using a package object" {
        BeforeAll {
            Copy-ExcelWorkSheet -SourceWorkbook $excel -DestinationWorkbook $excel -DestinationWorkSheet "CopyOfMixedTypes"
            Close-ExcelPackage -ExcelPackage $excel
            $excel = Open-ExcelPackage -Path $path2
            $ws = $Excel.Workbook.Worksheets[3]
        }
        it "Copied a worksheet, giving the expected name, number of rows and number of columns " {
            $Excel.Workbook.Worksheets.count                            | Should     be 3
            $ws                                                         | Should not benullorEmpty
            $ws.Name                                                    | Should     be "CopyOfMixedTypes"
            $ws.Dimension.Columns                                       | Should     be  22
            $ws.Dimension.Rows                                          | Should     be  2
        }
        it "Copied the expected data into the worksheet " {
            $ws.Cells[2, 1].Value.Gettype().name                        | Should     be  'DateTime'
            $ws.Cells[2, 2].Formula                                     | Should     be  'SUM(F2:G2)'
            $ws.Cells[2, 5].Value.GetType().name                       | Should     be  'String'
            $ws.Cells[2, 6].Value.GetType().name                       | Should     be  'String'
            $ws.Cells[2, 18].Value.GetType().name                       | Should     be  'String'
            ($ws.Cells[2, 11].Value -is [valuetype] )                   | Should     be  $true
            ($ws.Cells[2, 12].Value -is [valuetype] )                   | Should     be  $true
            ($ws.Cells[2, 13].Value -is [valuetype] )                   | Should     be  $true
            $ws.Cells[2, 11].Value                                     | Should     beLessThan 0
            $ws.Cells[2, 12].Value                                     | Should     beLessThan 0
            $ws.Cells[2, 13].Value                                     | Should     beLessThan 0
            if ((Get-Culture).NumberFormat.NumberGroupSeparator -EQ ",") {
                ($ws.Cells[2, 8].Value -is [valuetype] )                | Should     be  $true
                $ws.Cells[2, 9].Value.GetType().name                   | Should     be  'String'
            }
            elseif ((Get-Culture).NumberFormat.NumberGroupSeparator -EQ ".") {
                ($ws.Cells[2, 9].Value -is [valuetype] )                | Should     be  $true
                $ws.Cells[2, 8].Value.GetType().name                   | Should     be  'String'
            }
            ($ws.Cells[2, 14].Value -is [valuetype] )                   | Should     be  $true
            $ws.Cells[2, 15].Value.GetType().name                      | Should     be  'String'
            $ws.Cells[2, 16].Value.GetType().name                      | Should     be  'String'
            $ws.Cells[2, 17].Value.GetType().name                      | Should     be  'String'
            ($ws.Cells[2, 19].Value -is [valuetype] )                   | Should     be  $true
            ($ws.Cells[2, 20].Value -is [valuetype] )                   | Should     be  $true
        }
    }

    Context "Copy worksheet should close all files" {
        BeforeAll {
            $xlfile = "$env:TEMP\reports.xlsx"
            $xlfileArchive = "$env:TEMP\reportsArchive.xlsx"

            rm $xlfile -ErrorAction SilentlyContinue
            rm $xlfileArchive -ErrorAction SilentlyContinue

            $sheets = echo 1.1.2019 1.2.2019 1.3.2019 1.4.2019 1.5.2019

            $sheets | ForEach-Object {
                "Hello World" | Export-Excel $xlfile -WorksheetName $_
            }
        }

        it "Should copy and remove sheets" {
            $targetSheets = echo 1.1.2019 1.4.2019

            $targetSheets | ForEach-Object {
                Copy-ExcelWorkSheet -SourceWorkbook $xlfile -DestinationWorkbook $xlfileArchive -SourceWorkSheet $_ -DestinationWorkSheet $_
            }

            $targetSheets | ForEach-Object { Remove-WorkSheet -FullName $xlfile -WorksheetName $_ }

            (Get-ExcelSheetInfo -Path $xlfile ).Count | Should Be 3
        }
    }
}