-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpreadsheetWrangler.ps1
More file actions
3013 lines (2629 loc) · 131 KB
/
SpreadsheetWrangler.ps1
File metadata and controls
3013 lines (2629 loc) · 131 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SpreadsheetWrangler.ps1
# GUI application for spreadsheet operations and folder backups
# Modular version - dot-sources components from lib/ folder
# Check for ImportExcel module availability
$script:UseImportExcel = $false
# Check if ImportExcel module is installed
if (-not (Get-Module -ListAvailable -Name ImportExcel)) {
Write-Host "ImportExcel module not found. Attempting to install..." -ForegroundColor Yellow
try {
Install-Module -Name ImportExcel -Scope CurrentUser -Force -ErrorAction Stop
Write-Host "ImportExcel module installed successfully." -ForegroundColor Green
} catch {
$errorMessage = $_.Exception.Message
Write-Host "Failed to install ImportExcel module. Please run 'Install-Module -Name ImportExcel -Scope CurrentUser -Force' manually." -ForegroundColor Red
Write-Host "Error``: $errorMessage" -ForegroundColor Red
exit
}
}
# Import the module
try {
Import-Module -Name ImportExcel -ErrorAction Stop
$script:UseImportExcel = $true
Write-Host "ImportExcel module loaded successfully." -ForegroundColor Green
} catch {
$errorMessage = $_.Exception.Message
Write-Host "Failed to load ImportExcel module. Error``: $errorMessage" -ForegroundColor Red
exit
}
# Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Xml
Add-Type -AssemblyName System.Xml.Linq
# Dot-source the module files (suppress any stray output)
$libPath = Join-Path -Path $PSScriptRoot -ChildPath "lib"
. (Join-Path -Path $libPath -ChildPath "Utilities.ps1") | Out-Null
. (Join-Path -Path $libPath -ChildPath "Backup.ps1") | Out-Null
. (Join-Path -Path $libPath -ChildPath "Spreadsheet.ps1") | Out-Null
. (Join-Path -Path $libPath -ChildPath "Labels.ps1") | Out-Null
. (Join-Path -Path $libPath -ChildPath "Config.ps1") | Out-Null
# Initialize script variables for label paths
$script:LabelInputFolder = ""
$script:LabelOutputFolder = ""
$script:LabelParamTemplate = ""
$script:LabelPrtTemplate = ""
$script:LabelDymoTemplate = ""
# Create the main form
$script:form = New-Object System.Windows.Forms.Form
$script:form.Text = "Spreadsheet Wrangler"
$script:form.Size = New-Object System.Drawing.Size(900, 870)
$script:form.MinimumSize = New-Object System.Drawing.Size(800, 750)
$script:form.StartPosition = "CenterScreen"
$script:form.FormBorderStyle = "Sizable"
$script:form.MaximizeBox = $true
$script:form.MinimizeBox = $true
$script:form.Font = New-Object System.Drawing.Font("Segoe UI", 10)
$script:form.KeyPreview = $true
# Track if an operation is running (for cancel functionality)
$script:IsRunning = $false
$script:CancelRequested = $false
# Dark mode state
$script:DarkMode = $false
# Set application icon if logo exists
$logoPath = Join-Path -Path $PSScriptRoot -ChildPath "logo.png"
if (Test-Path -Path $logoPath) {
try {
# Load the logo as an icon for the application
$logo = [System.Drawing.Image]::FromFile($logoPath)
# Create a simple icon from the logo
$icon = [System.Drawing.Icon]::FromHandle(($logo.GetThumbnailImage(64, 64, $null, [System.IntPtr]::Zero)).GetHicon())
$script:form.Icon = $icon
} catch {
Write-Log "Error setting application icon: $_" "Yellow"
}
}
# Create the menu bar
$menuBar = New-Object System.Windows.Forms.MenuStrip
$menuBar.BackColor = [System.Drawing.SystemColors]::Control
$script:form.MainMenuStrip = $menuBar
# File Menu
$fileMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$fileMenu.Text = "File"
# New Configuration
$newConfigMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$newConfigMenuItem.Text = "New Configuration"
$newConfigMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::N
$newConfigMenuItem.Add_Click({
Write-Log "Resetting to new configuration." "White"
# Clear text fields and list views
$script:backupLocations.Items.Clear()
$script:spreadsheetLocations.Items.Clear()
$script:destinationLocation.Text = ""
$script:skuListLocation.Text = ""
$script:finalOutputLocation.Text = ""
$script:textBoxSingleSpreadsheetFile.Text = ""
# Reset script-level variables for label paths
$script:LabelInputFolder = ""
$script:LabelOutputFolder = ""
$script:LabelParamTemplate = ""
$script:LabelPrtTemplate = ""
$script:LabelDymoTemplate = ""
# Reset all checkboxes to false
foreach ($checkbox in $script:optionCheckboxes) {
$checkbox.Checked = $false
}
# Reset current config file path
$script:CurrentConfigFile = $null
$script:form.Text = "Spreadsheet Wrangler"
Update-RecentFilesMenu # This will also save app settings if recent files are managed
})
$fileMenu.DropDownItems.Add($newConfigMenuItem) | Out-Null
# Open Configuration
$openConfigMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$openConfigMenuItem.Text = "Open Configuration..."
$openConfigMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::O
$openConfigMenuItem.Add_Click({
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*"
$openFileDialog.Title = "Open Configuration"
$openFileDialog.InitialDirectory = $PSScriptRoot
if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
Load-Configuration -ConfigPath $openFileDialog.FileName
}
})
$fileMenu.DropDownItems.Add($openConfigMenuItem) | Out-Null
# Save Configuration
$saveConfigMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$saveConfigMenuItem.Text = "Save Configuration"
$saveConfigMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::S
$saveConfigMenuItem.Add_Click({
# If we have a current config file, save to it, otherwise prompt for location
if ($script:CurrentConfigFile -and (Test-Path $script:CurrentConfigFile)) {
Save-Configuration -ConfigPath $script:CurrentConfigFile
} else {
$saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveFileDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*"
$saveFileDialog.Title = "Save Configuration"
$saveFileDialog.InitialDirectory = $PSScriptRoot
$saveFileDialog.DefaultExt = "xml"
if ($saveFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
Save-Configuration -ConfigPath $saveFileDialog.FileName
$script:CurrentConfigFile = $saveFileDialog.FileName
}
}
})
$fileMenu.DropDownItems.Add($saveConfigMenuItem) | Out-Null
# Save Configuration As
$saveAsConfigMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$saveAsConfigMenuItem.Text = "Save Configuration As..."
$saveAsConfigMenuItem.Add_Click({
$saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveFileDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*"
$saveFileDialog.Title = "Save Configuration As"
$saveFileDialog.InitialDirectory = $PSScriptRoot
$saveFileDialog.DefaultExt = "xml"
if ($saveFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
Save-Configuration -ConfigPath $saveFileDialog.FileName
$script:CurrentConfigFile = $saveFileDialog.FileName
}
})
$fileMenu.DropDownItems.Add($saveAsConfigMenuItem) | Out-Null
# Recent Files submenu
$script:recentFilesMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$script:recentFilesMenuItem.Text = "Recent Files"
$fileMenu.DropDownItems.Add($script:recentFilesMenuItem) | Out-Null
# Initialize with empty item (will be updated by Update-RecentFilesMenu)
$noRecentFilesItem = New-Object System.Windows.Forms.ToolStripMenuItem
$noRecentFilesItem.Text = "(No recent files)"
$noRecentFilesItem.Enabled = $false
$script:recentFilesMenuItem.DropDownItems.Add($noRecentFilesItem) | Out-Null
# Separator
$fileMenu.DropDownItems.Add("-") | Out-Null
# Exit
$exitMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$exitMenuItem.Text = "Exit"
$exitMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Alt -bor [System.Windows.Forms.Keys]::F4
$exitMenuItem.Add_Click({ $script:form.Close() })
$fileMenu.DropDownItems.Add($exitMenuItem) | Out-Null
# Labels Menu
$labelsMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$labelsMenu.Text = "Labels"
# Create Labels
$createLabelsMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$createLabelsMenuItem.Text = "Create Labels"
$createLabelsMenuItem.Add_Click({
Show-CreateLabelsDialog
})
$labelsMenu.DropDownItems.Add($createLabelsMenuItem) | Out-Null
# View Menu
$viewMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$viewMenu.Text = "View"
# Dark Mode Toggle
$darkModeMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$darkModeMenuItem.Text = "Dark Mode"
$darkModeMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::D
$darkModeMenuItem.CheckOnClick = $true
$darkModeMenuItem.Add_Click({
Toggle-DarkMode
})
$viewMenu.DropDownItems.Add($darkModeMenuItem) | Out-Null
# Separator
$viewMenu.DropDownItems.Add("-") | Out-Null
# Preview
$previewMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$previewMenuItem.Text = "Preview Operations"
$previewMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::P
$previewMenuItem.Add_Click({
Show-Preview
})
$viewMenu.DropDownItems.Add($previewMenuItem) | Out-Null
# Clear Terminal
$clearTerminalMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$clearTerminalMenuItem.Text = "Clear Terminal"
$clearTerminalMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::L
$clearTerminalMenuItem.Add_Click({
$script:outputTextbox.Clear()
Write-Log "Terminal cleared." "Gray"
})
$viewMenu.DropDownItems.Add($clearTerminalMenuItem) | Out-Null
# Export Terminal Log
$exportLogMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$exportLogMenuItem.Text = "Export Terminal Log..."
$exportLogMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::E
$exportLogMenuItem.Add_Click({
Export-TerminalLog
})
$viewMenu.DropDownItems.Add($exportLogMenuItem) | Out-Null
# Separator
$viewMenu.DropDownItems.Add("-") | Out-Null
# Copy Terminal to Clipboard
$copyTerminalMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$copyTerminalMenuItem.Text = "Copy Terminal to Clipboard"
$copyTerminalMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::Control -bor [System.Windows.Forms.Keys]::Shift -bor [System.Windows.Forms.Keys]::C
$copyTerminalMenuItem.Add_Click({
if ($script:outputTextbox.Text.Length -gt 0) {
[System.Windows.Forms.Clipboard]::SetText($script:outputTextbox.Text)
Write-Log "Terminal content copied to clipboard." "Green"
} else {
Write-Log "Terminal is empty - nothing to copy." "Yellow"
}
})
$viewMenu.DropDownItems.Add($copyTerminalMenuItem) | Out-Null
# Tools Menu
$toolsMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$toolsMenu.Text = "Tools"
# Run Operations
$runMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$runMenuItem.Text = "Run Operations"
$runMenuItem.ShortcutKeys = [System.Windows.Forms.Keys]::F5
$runMenuItem.Add_Click({
if (-not $script:IsRunning) {
$script:runButton.PerformClick()
}
})
$toolsMenu.DropDownItems.Add($runMenuItem) | Out-Null
# Cancel Operations
$script:cancelMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$script:cancelMenuItem.Text = "Cancel Operations (Esc)"
$script:cancelMenuItem.Enabled = $false
$script:cancelMenuItem.Add_Click({
if ($script:IsRunning) {
$script:CancelRequested = $true
Write-Log "Cancel requested..." "Yellow"
}
})
$toolsMenu.DropDownItems.Add($script:cancelMenuItem) | Out-Null
# Separator
$toolsMenu.DropDownItems.Add("-") | Out-Null
# Validate Paths
$validatePathsMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$validatePathsMenuItem.Text = "Validate All Paths"
$validatePathsMenuItem.Add_Click({
Write-Log "=== Validating Paths ===" "Cyan"
$allValid = $true
# Check backup locations
foreach ($item in $script:backupLocations.Items) {
if (Test-Path $item.Text) {
Write-Log " [OK] Backup: $($item.Text)" "Green"
} else {
Write-Log " [MISSING] Backup: $($item.Text)" "Red"
$allValid = $false
}
}
# Check spreadsheet locations
foreach ($item in $script:spreadsheetLocations.Items) {
if (Test-Path $item.Text) {
Write-Log " [OK] Spreadsheet: $($item.Text)" "Green"
} else {
Write-Log " [MISSING] Spreadsheet: $($item.Text)" "Red"
$allValid = $false
}
}
# Check other paths
if ($script:destinationLocation.Text -and -not (Test-Path $script:destinationLocation.Text)) {
Write-Log " [MISSING] Destination: $($script:destinationLocation.Text)" "Red"
$allValid = $false
} elseif ($script:destinationLocation.Text) {
Write-Log " [OK] Destination: $($script:destinationLocation.Text)" "Green"
}
if ($allValid) {
Write-Log "All paths are valid!" "Green"
} else {
Write-Log "Some paths are missing or invalid." "Yellow"
}
})
$toolsMenu.DropDownItems.Add($validatePathsMenuItem) | Out-Null
# Open Output Folder
$openOutputMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$openOutputMenuItem.Text = "Open Output Folder"
$openOutputMenuItem.Add_Click({
if ($script:destinationLocation.Text -and (Test-Path $script:destinationLocation.Text)) {
Start-Process "explorer.exe" -ArgumentList $script:destinationLocation.Text
} elseif ($script:finalOutputLocation.Text -and (Test-Path $script:finalOutputLocation.Text)) {
Start-Process "explorer.exe" -ArgumentList $script:finalOutputLocation.Text
} else {
Write-Log "No valid output folder to open." "Yellow"
}
})
$toolsMenu.DropDownItems.Add($openOutputMenuItem) | Out-Null
# Help Menu
$helpMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$helpMenu.Text = "Help"
# Check for Updates
$checkUpdatesMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$checkUpdatesMenuItem.Text = "Check for Updates"
$checkUpdatesMenuItem.Add_Click({
Check-ForUpdates
})
$helpMenu.DropDownItems.Add($checkUpdatesMenuItem) | Out-Null
# Separator
$helpMenu.DropDownItems.Add("-") | Out-Null
# About
$aboutMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$aboutMenuItem.Text = "About"
$aboutMenuItem.Add_Click({
$aboutForm = New-Object System.Windows.Forms.Form
$aboutForm.Text = "About Spreadsheet Wrangler"
# Adjust the size of the About dialog
$aboutForm.Size = New-Object System.Drawing.Size(500, 400)
$aboutForm.StartPosition = "CenterParent"
$aboutForm.FormBorderStyle = "FixedDialog"
$aboutForm.MaximizeBox = $false
$aboutForm.MinimizeBox = $false
$aboutPanel = New-Object System.Windows.Forms.TableLayoutPanel
$aboutPanel.Dock = "Fill"
$aboutPanel.RowCount = 4
$aboutPanel.ColumnCount = 1
# Adjust space allocation to reduce gaps
$aboutPanel.RowStyles.Add((New-Object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 40)))
$aboutPanel.RowStyles.Add((New-Object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 30)))
$aboutPanel.RowStyles.Add((New-Object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 15)))
$aboutPanel.RowStyles.Add((New-Object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 15)))
$aboutForm.Controls.Add($aboutPanel) | Out-Null
# Logo
$logoPanel = New-Object System.Windows.Forms.Panel
$logoPanel.Dock = "Fill"
# Reduce padding to 10 pixels
$logoPanel.Padding = New-Object System.Windows.Forms.Padding(10)
$aboutPanel.Controls.Add($logoPanel, 0, 0) | Out-Null
# Load the logo image
$logoPath = Join-Path -Path $PSScriptRoot -ChildPath "logo.png"
if (Test-Path -Path $logoPath) {
try {
$logoImage = [System.Drawing.Image]::FromFile($logoPath)
$logoPictureBox = New-Object System.Windows.Forms.PictureBox
$logoPictureBox.Image = $logoImage
$logoPictureBox.SizeMode = "Zoom"
$logoPictureBox.Dock = "Fill"
$logoPanel.Controls.Add($logoPictureBox) | Out-Null
} catch {
Write-Log "Error loading logo: $_" "Yellow"
}
}
# Main about text
$aboutLabel = New-Object System.Windows.Forms.Label
$aboutLabel.Text = "Spreadsheet Wrangler v2.4.0`n`nA powerful tool for backing up folders and combining spreadsheets.`n`nCreated by Bryant Welch`n`n(c) 2025 Bryant Welch. All Rights Reserved"
$aboutLabel.AutoSize = $false
$aboutLabel.Dock = "Fill"
$aboutLabel.TextAlign = "MiddleCenter"
$aboutPanel.Controls.Add($aboutLabel, 0, 1) | Out-Null
# GitHub link
$linkLabel = New-Object System.Windows.Forms.LinkLabel
$linkLabel.Text = "https://github.com/BryantWelch/Spreadsheet-Wrangler"
$linkLabel.AutoSize = $false
$linkLabel.Dock = "Fill"
$linkLabel.TextAlign = "MiddleCenter"
$linkLabel.LinkColor = [System.Drawing.Color]::Blue
$linkLabel.ActiveLinkColor = [System.Drawing.Color]::Red
$linkLabel.Add_LinkClicked({
param($senderObj, $e)
Start-Process "https://github.com/BryantWelch/Spreadsheet-Wrangler"
})
$aboutPanel.Controls.Add($linkLabel, 0, 2) | Out-Null
# OK button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Text = "OK"
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$okButton.Dock = "Fill"
$okButton.Margin = New-Object System.Windows.Forms.Padding(150, 10, 150, 10)
$aboutPanel.Controls.Add($okButton, 0, 3) | Out-Null
$aboutForm.AcceptButton = $okButton
$aboutForm.ShowDialog() | Out-Null
})
$helpMenu.DropDownItems.Add($aboutMenuItem) | Out-Null
# Spreadsheet Menu
$spreadsheetMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$spreadsheetMenu.Text = "Spreadsheet"
# Preview Spreadsheet
$previewSpreadsheetMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$previewSpreadsheetMenuItem.Text = "Preview Spreadsheet..."
$previewSpreadsheetMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx;*.xls)|*.xlsx;*.xls|CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
$openDialog.Title = "Select Spreadsheet to Preview"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "=== Previewing: $($openDialog.FileName) ===" "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
if ($data.Count -eq 0) {
Write-Log "Spreadsheet is empty." "Yellow"
return
}
# Show first 10 rows
$previewCount = [Math]::Min(10, $data.Count)
Write-Log "Showing first $previewCount of $($data.Count) rows:" "White"
Write-Log "" "White"
# Get column headers
$headers = $data[0].PSObject.Properties.Name
Write-Log (" " + ($headers -join " | ")) "Cyan"
Write-Log (" " + ("-" * 60)) "Gray"
for ($i = 0; $i -lt $previewCount; $i++) {
$row = $data[$i]
$values = $headers | ForEach-Object {
$val = $row.$_
if ($null -eq $val) { "" } else { $val.ToString().Substring(0, [Math]::Min(20, $val.ToString().Length)) }
}
Write-Log (" " + ($values -join " | ")) "White"
}
if ($data.Count -gt 10) {
Write-Log " ... and $($data.Count - 10) more rows" "Gray"
}
} catch {
Write-Log "Error previewing spreadsheet: $_" "Red"
}
}
})
$spreadsheetMenu.DropDownItems.Add($previewSpreadsheetMenuItem) | Out-Null
# Show Statistics
$statisticsMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$statisticsMenuItem.Text = "Show Statistics..."
$statisticsMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx;*.xls)|*.xlsx;*.xls|CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
$openDialog.Title = "Select Spreadsheet for Statistics"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "=== Statistics: $($openDialog.FileName) ===" "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
if ($data.Count -eq 0) {
Write-Log "Spreadsheet is empty." "Yellow"
return
}
$headers = $data[0].PSObject.Properties.Name
$fileInfo = Get-Item $openDialog.FileName
Write-Log "FILE INFO:" "Yellow"
Write-Log " Name: $($fileInfo.Name)" "White"
Write-Log " Size: $([Math]::Round($fileInfo.Length / 1KB, 2)) KB" "White"
Write-Log " Modified: $($fileInfo.LastWriteTime)" "White"
Write-Log "" "White"
Write-Log "DATA INFO:" "Yellow"
Write-Log " Total Rows: $($data.Count)" "White"
Write-Log " Total Columns: $($headers.Count)" "White"
Write-Log "" "White"
Write-Log "COLUMNS:" "Yellow"
foreach ($header in $headers) {
$nonEmpty = ($data | Where-Object { -not [string]::IsNullOrWhiteSpace($_.$header) }).Count
$emptyCount = $data.Count - $nonEmpty
Write-Log " $header - $nonEmpty values ($emptyCount empty)" "White"
}
# Check for potential duplicates
$uniqueRows = ($data | Select-Object -Property * -Unique).Count
if ($uniqueRows -lt $data.Count) {
Write-Log "" "White"
Write-Log "WARNING: $($data.Count - $uniqueRows) potential duplicate rows detected" "Yellow"
}
} catch {
Write-Log "Error analyzing spreadsheet: $_" "Red"
}
}
})
$spreadsheetMenu.DropDownItems.Add($statisticsMenuItem) | Out-Null
# Detect Duplicates
$detectDuplicatesMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$detectDuplicatesMenuItem.Text = "Detect Duplicates..."
$detectDuplicatesMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx;*.xls)|*.xlsx;*.xls|CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
$openDialog.Title = "Select Spreadsheet to Check for Duplicates"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "=== Checking Duplicates: $($openDialog.FileName) ===" "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
if ($data.Count -eq 0) {
Write-Log "Spreadsheet is empty." "Yellow"
return
}
# Group by all columns to find duplicates
$grouped = $data | Group-Object -Property { $_.PSObject.Properties.Value -join "|" }
$duplicates = $grouped | Where-Object { $_.Count -gt 1 }
if ($duplicates.Count -eq 0) {
Write-Log "No duplicate rows found!" "Green"
} else {
Write-Log "Found $($duplicates.Count) groups of duplicate rows:" "Yellow"
$totalDupes = 0
foreach ($group in $duplicates | Select-Object -First 10) {
$totalDupes += ($group.Count - 1)
$firstRow = $group.Group[0]
$preview = ($firstRow.PSObject.Properties | Select-Object -First 3 | ForEach-Object { "$($_.Name): $($_.Value)" }) -join ", "
Write-Log " $($group.Count)x: $preview..." "White"
}
if ($duplicates.Count -gt 10) {
Write-Log " ... and $($duplicates.Count - 10) more duplicate groups" "Gray"
}
Write-Log "" "White"
Write-Log "Total duplicate rows that could be removed: $totalDupes" "Cyan"
}
} catch {
Write-Log "Error checking duplicates: $_" "Red"
}
}
})
$spreadsheetMenu.DropDownItems.Add($detectDuplicatesMenuItem) | Out-Null
# Separator
$spreadsheetMenu.DropDownItems.Add("-") | Out-Null
# Validate Data
$validateDataMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$validateDataMenuItem.Text = "Validate Data..."
$validateDataMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx;*.xls)|*.xlsx;*.xls|CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
$openDialog.Title = "Select Spreadsheet to Validate"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "=== Validating: $($openDialog.FileName) ===" "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
if ($data.Count -eq 0) {
Write-Log "Spreadsheet is empty." "Yellow"
return
}
$headers = $data[0].PSObject.Properties.Name
$issues = @()
# Check for empty columns
foreach ($header in $headers) {
$nonEmpty = ($data | Where-Object { -not [string]::IsNullOrWhiteSpace($_.$header) }).Count
if ($nonEmpty -eq 0) {
$issues += "Column '$header' is completely empty"
}
}
# Check for rows with all empty values
$emptyRows = 0
for ($i = 0; $i -lt $data.Count; $i++) {
$row = $data[$i]
$allEmpty = $true
foreach ($header in $headers) {
if (-not [string]::IsNullOrWhiteSpace($row.$header)) {
$allEmpty = $false
break
}
}
if ($allEmpty) { $emptyRows++ }
}
if ($emptyRows -gt 0) {
$issues += "$emptyRows completely empty rows found"
}
# Check for inconsistent data types in columns
foreach ($header in $headers) {
$values = $data | Where-Object { -not [string]::IsNullOrWhiteSpace($_.$header) } | ForEach-Object { $_.$header }
if ($values.Count -gt 0) {
$types = $values | ForEach-Object { $_.GetType().Name } | Select-Object -Unique
if ($types.Count -gt 1) {
$issues += "Column '$header' has mixed data types: $($types -join ', ')"
}
}
}
if ($issues.Count -eq 0) {
Write-Log "Validation passed! No issues found." "Green"
} else {
Write-Log "Found $($issues.Count) issue(s):" "Yellow"
foreach ($issue in $issues) {
Write-Log " - $issue" "White"
}
}
} catch {
Write-Log "Error validating spreadsheet: $_" "Red"
}
}
})
$spreadsheetMenu.DropDownItems.Add($validateDataMenuItem) | Out-Null
# Compare Columns
$compareColumnsMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$compareColumnsMenuItem.Text = "Compare Column Headers..."
$compareColumnsMenuItem.Add_Click({
if ($script:spreadsheetLocations.Items.Count -lt 2) {
Write-Log "Add at least 2 spreadsheet folders to compare columns." "Yellow"
return
}
Write-Log "=== Comparing Column Headers ===" "Cyan"
$allHeaders = @{}
foreach ($item in $script:spreadsheetLocations.Items) {
$folderPath = $item.Text
if (Test-Path $folderPath) {
$files = Get-ChildItem -Path $folderPath -Filter "*.xlsx" -ErrorAction SilentlyContinue | Select-Object -First 1
foreach ($file in $files) {
try {
$data = Import-Excel -Path $file.FullName -ErrorAction Stop | Select-Object -First 1
if ($data) {
$headers = $data.PSObject.Properties.Name
$allHeaders[$file.Name] = $headers
Write-Log " $($file.Name): $($headers.Count) columns" "White"
}
} catch {
Write-Log " Error reading $($file.Name): $_" "Red"
}
}
}
}
if ($allHeaders.Count -ge 2) {
$firstHeaders = $allHeaders.Values | Select-Object -First 1
$allMatch = $true
foreach ($key in $allHeaders.Keys) {
$diff = Compare-Object -ReferenceObject $firstHeaders -DifferenceObject $allHeaders[$key]
if ($diff) {
$allMatch = $false
Write-Log " $key has different columns!" "Yellow"
}
}
if ($allMatch) {
Write-Log "All spreadsheets have matching column headers!" "Green"
}
}
})
$spreadsheetMenu.DropDownItems.Add($compareColumnsMenuItem) | Out-Null
# Separator
$spreadsheetMenu.DropDownItems.Add("-") | Out-Null
# Convert to CSV
$convertToCsvMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$convertToCsvMenuItem.Text = "Convert to CSV..."
$convertToCsvMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx;*.xls)|*.xlsx;*.xls"
$openDialog.Title = "Select Excel File to Convert"
if ($openDialog.ShowDialog() -eq 'OK') {
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveDialog.Filter = "CSV Files (*.csv)|*.csv"
$saveDialog.Title = "Save CSV As"
$saveDialog.FileName = [System.IO.Path]::GetFileNameWithoutExtension($openDialog.FileName) + ".csv"
if ($saveDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "Converting to CSV..." "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
$data | Export-Csv -Path $saveDialog.FileName -NoTypeInformation -Encoding UTF8
Write-Log "Saved: $($saveDialog.FileName)" "Green"
Write-Log "Rows exported: $($data.Count)" "White"
} catch {
Write-Log "Error converting: $_" "Red"
}
}
}
})
$spreadsheetMenu.DropDownItems.Add($convertToCsvMenuItem) | Out-Null
# Merge Multiple Files
$mergeFilesMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$mergeFilesMenuItem.Text = "Quick Merge Files..."
$mergeFilesMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv"
$openDialog.Title = "Select Files to Merge"
$openDialog.Multiselect = $true
if ($openDialog.ShowDialog() -eq 'OK' -and $openDialog.FileNames.Count -gt 1) {
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveDialog.Filter = "Excel Files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv"
$saveDialog.Title = "Save Merged File As"
$saveDialog.FileName = "Merged_$(Get-Date -Format 'yyyyMMdd_HHmmss').xlsx"
if ($saveDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "Merging $($openDialog.FileNames.Count) files..." "Cyan"
$allData = @()
$totalRows = 0
foreach ($file in $openDialog.FileNames) {
$data = Import-Excel -Path $file -ErrorAction Stop
$allData += $data
$totalRows += $data.Count
Write-Log " Added $($data.Count) rows from $(Split-Path $file -Leaf)" "White"
}
if ($saveDialog.FileName -match "\.csv$") {
$allData | Export-Csv -Path $saveDialog.FileName -NoTypeInformation -Encoding UTF8
} else {
$allData | Export-Excel -Path $saveDialog.FileName -AutoSize
}
Write-Log "Merged $totalRows total rows to: $($saveDialog.FileName)" "Green"
} catch {
Write-Log "Error merging: $_" "Red"
}
}
} else {
Write-Log "Select at least 2 files to merge." "Yellow"
}
})
$spreadsheetMenu.DropDownItems.Add($mergeFilesMenuItem) | Out-Null
# Separator
$spreadsheetMenu.DropDownItems.Add("-") | Out-Null
# Clean Data submenu
$cleanDataMenu = New-Object System.Windows.Forms.ToolStripMenuItem
$cleanDataMenu.Text = "Clean Data"
# Remove Empty Rows
$removeEmptyRowsMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$removeEmptyRowsMenuItem.Text = "Remove Empty Rows..."
$removeEmptyRowsMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$openDialog.Title = "Select File to Clean"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "Removing empty rows..." "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
$headers = $data[0].PSObject.Properties.Name
$cleanData = $data | Where-Object {
$row = $_
$hasValue = $false
foreach ($header in $headers) {
if (-not [string]::IsNullOrWhiteSpace($row.$header)) {
$hasValue = $true
break
}
}
$hasValue
}
$removed = $data.Count - $cleanData.Count
if ($removed -gt 0) {
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$saveDialog.FileName = [System.IO.Path]::GetFileNameWithoutExtension($openDialog.FileName) + "_cleaned.xlsx"
if ($saveDialog.ShowDialog() -eq 'OK') {
$cleanData | Export-Excel -Path $saveDialog.FileName -AutoSize
Write-Log "Removed $removed empty rows. Saved to: $($saveDialog.FileName)" "Green"
}
} else {
Write-Log "No empty rows found." "Green"
}
} catch {
Write-Log "Error: $_" "Red"
}
}
})
$cleanDataMenu.DropDownItems.Add($removeEmptyRowsMenuItem) | Out-Null
# Remove Duplicates
$removeDuplicatesMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$removeDuplicatesMenuItem.Text = "Remove Duplicates..."
$removeDuplicatesMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$openDialog.Title = "Select File to Remove Duplicates"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "Removing duplicates..." "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
$cleanData = $data | Select-Object -Property * -Unique
$removed = $data.Count - $cleanData.Count
if ($removed -gt 0) {
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$saveDialog.FileName = [System.IO.Path]::GetFileNameWithoutExtension($openDialog.FileName) + "_deduped.xlsx"
if ($saveDialog.ShowDialog() -eq 'OK') {
$cleanData | Export-Excel -Path $saveDialog.FileName -AutoSize
Write-Log "Removed $removed duplicate rows. Saved to: $($saveDialog.FileName)" "Green"
}
} else {
Write-Log "No duplicates found." "Green"
}
} catch {
Write-Log "Error: $_" "Red"
}
}
})
$cleanDataMenu.DropDownItems.Add($removeDuplicatesMenuItem) | Out-Null
# Trim Whitespace
$trimWhitespaceMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$trimWhitespaceMenuItem.Text = "Trim Whitespace..."
$trimWhitespaceMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$openDialog.Title = "Select File to Trim Whitespace"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
Write-Log "Trimming whitespace..." "Cyan"
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
$headers = $data[0].PSObject.Properties.Name
$trimCount = 0
foreach ($row in $data) {
foreach ($header in $headers) {
if ($row.$header -is [string]) {
$original = $row.$header
$trimmed = $row.$header.Trim()
if ($original -ne $trimmed) {
$row.$header = $trimmed
$trimCount++
}
}
}
}
if ($trimCount -gt 0) {
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$saveDialog.FileName = [System.IO.Path]::GetFileNameWithoutExtension($openDialog.FileName) + "_trimmed.xlsx"
if ($saveDialog.ShowDialog() -eq 'OK') {
$data | Export-Excel -Path $saveDialog.FileName -AutoSize
Write-Log "Trimmed $trimCount cells. Saved to: $($saveDialog.FileName)" "Green"
}
} else {
Write-Log "No whitespace to trim." "Green"
}
} catch {
Write-Log "Error: $_" "Red"
}
}
})
$cleanDataMenu.DropDownItems.Add($trimWhitespaceMenuItem) | Out-Null
$spreadsheetMenu.DropDownItems.Add($cleanDataMenu) | Out-Null
# Sort Data
$sortDataMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$sortDataMenuItem.Text = "Sort by Column..."
$sortDataMenuItem.Add_Click({
$openDialog = New-Object System.Windows.Forms.OpenFileDialog
$openDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
$openDialog.Title = "Select File to Sort"
if ($openDialog.ShowDialog() -eq 'OK') {
try {
$data = Import-Excel -Path $openDialog.FileName -ErrorAction Stop
$headers = $data[0].PSObject.Properties.Name
# Show column selection dialog
$columnForm = New-Object System.Windows.Forms.Form
$columnForm.Text = "Select Column to Sort By"
$columnForm.Size = New-Object System.Drawing.Size(300, 200)
$columnForm.StartPosition = "CenterParent"
$columnForm.FormBorderStyle = "FixedDialog"
$label = New-Object System.Windows.Forms.Label
$label.Text = "Sort by column:"
$label.Location = New-Object System.Drawing.Point(10, 20)
$label.AutoSize = $true
$columnForm.Controls.Add($label) | Out-Null
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Location = New-Object System.Drawing.Point(10, 45)
$comboBox.Width = 260
$comboBox.DropDownStyle = "DropDownList"
foreach ($h in $headers) { $comboBox.Items.Add($h) | Out-Null }
$comboBox.SelectedIndex = 0
$columnForm.Controls.Add($comboBox) | Out-Null
$descendingCheck = New-Object System.Windows.Forms.CheckBox
$descendingCheck.Text = "Descending order"
$descendingCheck.Location = New-Object System.Drawing.Point(10, 80)
$descendingCheck.AutoSize = $true
$columnForm.Controls.Add($descendingCheck) | Out-Null
$okBtn = New-Object System.Windows.Forms.Button
$okBtn.Text = "Sort"
$okBtn.Location = New-Object System.Drawing.Point(100, 120)
$okBtn.DialogResult = "OK"
$columnForm.Controls.Add($okBtn) | Out-Null
$columnForm.AcceptButton = $okBtn
if ($columnForm.ShowDialog() -eq 'OK') {
$sortColumn = $comboBox.SelectedItem
Write-Log "Sorting by '$sortColumn'..." "Cyan"