feat: add PowerShell GUI for SharePoint XLS conversion

This commit is contained in:
Bobby Abellana (aider) 2025-02-19 13:57:22 -08:00
commit 5e13c146c0
3 changed files with 140 additions and 0 deletions

15
EventHandlers.ps1 Normal file
View File

@ -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
}

46
MainForm.ps1 Normal file
View File

@ -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()

79
SharePointFunctions.ps1 Normal file
View File

@ -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"
}
}