From 5e13c146c044d5c11d3d48806e50333584510c6d Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Wed, 19 Feb 2025 13:57:22 -0800 Subject: [PATCH] feat: add PowerShell GUI for SharePoint XLS conversion --- EventHandlers.ps1 | 15 ++++++++ MainForm.ps1 | 46 ++++++++++++++++++++++++ SharePointFunctions.ps1 | 79 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 EventHandlers.ps1 create mode 100644 MainForm.ps1 create mode 100644 SharePointFunctions.ps1 diff --git a/EventHandlers.ps1 b/EventHandlers.ps1 new file mode 100644 index 0000000..b4952e9 --- /dev/null +++ b/EventHandlers.ps1 @@ -0,0 +1,15 @@ +# Event handlers for button clicks +function List-XlsFiles { + $script:txtStatus.Text = "Listing XLS files...`n" + List-XlsFiles +} + +function Convert-Files { + $script:txtStatus.Text += "Starting conversion...`n" + Convert-Files +} + +function Move-Files { + $script:txtStatus.Text += "Moving files...`n" + Move-Files +} diff --git a/MainForm.ps1 b/MainForm.ps1 new file mode 100644 index 0000000..981288e --- /dev/null +++ b/MainForm.ps1 @@ -0,0 +1,46 @@ +Add-Type -AssemblyName System.Windows.Forms +Import-Module PnP.PowerShell + +# Import our components +. "$PSScriptRoot\SharePointFunctions.ps1" +. "$PSScriptRoot\EventHandlers.ps1" + +# Create main form +$form = New-Object System.Windows.Forms.Form +$form.Text = "SharePoint XLS Converter" +$form.Size = New-Object System.Drawing.Size(600,400) + +# Add controls +$btnList = New-Object System.Windows.Forms.Button +$btnList.Location = New-Object System.Drawing.Point(20,20) +$btnList.Size = New-Object System.Drawing.Size(120,30) +$btnList.Text = "List XLS Files" +$form.Controls.Add($btnList) + +$btnConvert = New-Object System.Windows.Forms.Button +$btnConvert.Location = New-Object System.Drawing.Point(160,20) +$btnConvert.Size = New-Object System.Drawing.Size(120,30) +$btnConvert.Text = "Convert to XLSX" +$form.Controls.Add($btnConvert) + +$btnMove = New-Object System.Windows.Forms.Button +$btnMove.Location = New-Object System.Drawing.Point(300,20) +$btnMove.Size = New-Object System.Drawing.Size(120,30) +$btnMove.Text = "Move Files" +$form.Controls.Add($btnMove) + +$txtStatus = New-Object System.Windows.Forms.TextBox +$txtStatus.Location = New-Object System.Drawing.Point(20,70) +$txtStatus.Size = New-Object System.Drawing.Size(540,280) +$txtStatus.Multiline = $true +$txtStatus.ScrollBars = "Vertical" +$form.Controls.Add($txtStatus) + +# Attach event handlers +$btnList.Add_Click({ List-XlsFiles }) +$btnConvert.Add_Click({ Convert-Files }) +$btnMove.Add_Click({ Move-Files }) + +# Show form +$form.Add_Shown({$form.Activate()}) +[void]$form.ShowDialog() diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 new file mode 100644 index 0000000..71468ca --- /dev/null +++ b/SharePointFunctions.ps1 @@ -0,0 +1,79 @@ +function Connect-SharePoint { + param($SiteUrl) + try { + Connect-PnPOnline -Url $SiteUrl -UseWebLogin + return $true + } + catch { + $script:txtStatus.Text += "Error connecting: $_`n" + return $false + } +} + +function List-XlsFiles { + if (-not (Connect-SharePoint -SiteUrl "https://yoursite.sharepoint.com")) { + return + } + + $tempLibrary = "Documents/Temp" + $fileList = @() + + $items = Get-PnPListItem -List "Documents" -PageSize 2000 | Where { + ($_.FieldValues.FileRef -like "*.xls") -and (-not $_.FieldValues.FileRef.Contains(".xlsx")) + } + + foreach ($item in $items) { + $fileList += [PSCustomObject]@{ + OriginalPath = $item.FieldValues.FileRef + TempPath = "$tempLibrary/$($item.FieldValues.FileLeafRef)" + } + } + + $fileList | ConvertTo-Json | Set-PnPFileContent -Folder $tempLibrary -FileName "FileList.json" + $script:txtStatus.Text += "Found $($fileList.Count) XLS files`n" +} + +function Convert-Files { + if (-not (Connect-SharePoint -SiteUrl "https://yoursite.sharepoint.com")) { + return + } + + $excel = New-Object -ComObject Excel.Application + $excel.Visible = $false + + try { + Get-PnPFile -Url "/sites/yoursite/Documents/Temp/FileList.json" -Path $env:TEMP -Filename "FileList.json" + $files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json + + foreach ($file in $files) { + try { + $workbook = $excel.Workbooks.Open((Convert-PnPFile -Url $file.OriginalPath -Path $env:TEMP -Filename $file.OriginalFileName)) + $newName = [System.IO.Path]::ChangeExtension($file.OriginalFileName, ".xlsx") + $workbook.SaveAs([System.IO.Path]::Combine($env:TEMP, $newName), 51) # 51 = xlsx + Add-PnPFile -Folder "Documents" -Path "$env:TEMP\$newName" + $script:txtStatus.Text += "Converted $($file.OriginalFileName) to XLSX`n" + } + finally { + $workbook.Close() + } + } + } + finally { + $excel.Quit() + } +} + +function Move-Files { + if (-not (Connect-SharePoint -SiteUrl "https://yoursite.sharepoint.com")) { + return + } + + $files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json + + foreach ($file in $files) { + Move-PnPFile -SourceUrl $file.OriginalPath -TargetUrl $file.TempPath -Force + $xlsxPath = [System.IO.Path]::ChangeExtension($file.OriginalPath, ".xlsx") + Move-PnPFile -SourceUrl "$tempLibrary/$xlsxPath" -TargetUrl $xlsxPath -Force + $script:txtStatus.Text += "Moved $($file.OriginalFileName) and its XLSX version`n" + } +}