-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkronos-nationwide-import-prep.ps1
211 lines (154 loc) · 7.57 KB
/
kronos-nationwide-import-prep.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<#
File: kronos-nationwide-import-prep.ps1
Date: 2021APR15
Author: William Blair
Contact: [email protected]
Note: Runs from any folder but folder names with spaces will error if double-clicking to run
#>
<#
This script will do the following:
- save the kronos export xls as an xlsm
- import vba code into new xlsm file and create a module
- delete the first 7 rows
- merge 3 DEF columns into one column, delete old columns, format as currency, add title "Record DEF"
- merge 2 Roth columns into one column, delete old columns, format as currency, add title "Record Roth"
- save the kronos export xlsm file into a csv
#>
<#
Some links for review
Excel Saveas different formats https://stackoverflow.com/questions/6972494/how-save-excel-2007-in-html-format-using-powershell
Allow macros in excel https://stackoverflow.com/questions/35846996/running-excel-macro-from-windows-powershell-script
Disable pop-ups https://stackoverflow.com/questions/37979128/prevent-overwrite-pop-up-when-writing-into-excel-using-a-powershell
Run macros in powershell https://www.excell-en.com/blog/2018/8/20/powershell-run-macros-copy-files-do-cool-stuff-with-power
Clean up user defined variables http://blog.insidemicrosoft.com/2017/05/28/how-to-clean-up-powershell-script-variables-without-restarting-powershell-ise/
#>
# Delete leftover xlsm and csv files
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show("Warning! Excel is going to close! Please save all work before you click OK.")
Remove-Item * -Include *.xlsm, *csv
# These registry keys will allow macro security - version 16
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\16.0\excel\Security" -Name AccessVBOM -PropertyType DWORD -Value 1 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\16.0\excel\Security" -Name VBAWarnings -PropertyType DWORD -Value 1 -Force | Out-Null
# Kill all Excel processes
Stop-Process -Name "Excel"
# Get all files with xls extension
Get-ChildItem $PSScriptRoot -Filter *.xls |
# Run this For loop for a files with xls extension
Foreach-Object {
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host
$excelFile = $_.FullName
# Variables to set file format to saveas
$formatXLSM = 52 #xlOpenXMLWorkbookMacroEnabled
$formatCSV = 6 #xlCSV
# Filler for the saveas process
$missing = [type]::Missing
# Cycle through each file with .xls extension in the script's directory
# Create excel object
$excel = New-Object -ComObject Excel.Application
# disable visible updating of sheet
$excel.Visible = $false
$workBook = $excel.Workbooks.Open($excelFile)
# disables the pop up asking if it's ok to overwrite - just overwrite it
$excel.DisplayAlerts = $false;
# saveas xlsm
$excelFile = $excelFile + 'm'
$excel.ActiveWorkbook.SaveAs($excelFile,$formatXLSM,$missing,$missing,$missing,$missing,$missing,$missing,$missing,$missing,$missing,$missing)
$excel.Quit()
# Create excel object
$excel = New-Object -ComObject Excel.Application
# If you want to see what's going on while script runs, uncomment visible and displayalerts below
#$excel.Visible = $true
#$excel.DisplayAlerts = $true
Write-Host "Now processing file: " $excelFile
$workBook = $excel.Workbooks.Open($excelFile)
$excelModule = $workBook.VBProject.VBComponents.Add(1)
# This was supposed to load up the entire file as the macro from... the macro but I couldn't get it working
#$macroImport = [IO.File]::ReadAllText("R:\Nationwide\Nationwide_Export_Prep_Powershell.bas")
# This is the macro
$excelMacro = @"
Sub Nationwide_Export_Prep()
Application.ScreenUpdating = False
Sheets("report").Select
Rows("1:6").Select
Selection.Delete Shift:=xlUp
Columns("J:J").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("J2").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-3],RC[-2],RC[-1])"
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
While ActiveCell.Offset(rowOffset:=0, columnOffset:=-3) <> ""
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-3],RC[-2],RC[-1])"
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
Wend
Columns("M:M").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("M2").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],RC[-1])"
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
While ActiveCell.Offset(rowOffset:=0, columnOffset:=-3) <> ""
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],RC[-1])"
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
Wend
Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("J:J").Select
Selection.Copy
Columns("K:K").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("G:J").Select
Selection.Delete Shift:=xlToLeft
Columns("G:G").Select
Selection.Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.NumberFormat = "#,##0.00"
Columns("G:G").Select
Selection.NumberFormat = "$#,##0.00"
Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("J:J").Select
Selection.Copy
Columns("K:K").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("H:J").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("H:H").Select
Selection.Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.NumberFormat = "$#,##0.00"
Columns("H:H").Select
Selection.NumberFormat = "$#,##0.00"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Record DEF"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Record Roth"
Range("A1").Select
End Sub
"@
# This adds the macro to the xlsm file
$excelModule.CodeModule.AddFromString($excelMacro)
# This runs the macro
$excel.Run("Nationwide_Export_Prep")
# saveas csv
$excelFile = $_.FullName
$excelFile = [io.path]::GetFileNameWithoutExtension("$excelFile")
$excelFile = $excelFile + ".csv"
$excelFile = $PSScriptRoot + "\" + $excelFile
$excel.ActiveWorkbook.SaveAs($excelFile,$formatCSV,$missing,$missing,$missing,$missing,$missing,$missing,$missing,$missing,$missing,$missing)
$excel.Quit()
Stop-Process -Name "Excel"
}
# These registry keys will disable macro security - version 16
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\16.0\excel\Security" -Name AccessVBOM -PropertyType DWORD -Value 0 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\16.0\excel\Security" -Name VBAWarnings -PropertyType DWORD -Value 0 -Force | Out-Null