SharepointXLStoXLSX/MainForm.ps1
2025-02-24 15:27:25 -08:00

157 lines
5.8 KiB
PowerShell

Add-Type -AssemblyName System.Windows.Forms
Import-Module Microsoft.Graph.Sites
Import-Module Microsoft.Graph.Files
# Import our components
. "$PSScriptRoot\GraphConfig.ps1"
. "$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(800, 600)
# Set AutoScaleMode to Dpi
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
# Add controls
$lblConfig = New-Object System.Windows.Forms.Label
$lblConfig.Text = "SharePoint Configuration:"
$lblConfig.Location = New-Object System.Drawing.Point(20, 20)
$lblConfig.Size = New-Object System.Drawing.Size(200, 20)
$form.Controls.Add($lblConfig)
$txtSiteUrl = New-Object System.Windows.Forms.TextBox
$txtSiteUrl.Location = New-Object System.Drawing.Point(20, 40)
$txtSiteUrl.Size = New-Object System.Drawing.Size(300, 20)
$txtSiteUrl.PlaceholderText = "Site URL"
$txtSiteUrl.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$txtProdLib = New-Object System.Windows.Forms.TextBox
$txtProdLib.Location = New-Object System.Drawing.Point(20, 70)
$txtProdLib.Size = New-Object System.Drawing.Size(300, 20)
$txtProdLib.PlaceholderText = "Production Library"
$txtProdLib.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$txtTempLib = New-Object System.Windows.Forms.TextBox
$txtTempLib.Location = New-Object System.Drawing.Point(20, 100)
$txtTempLib.Size = New-Object System.Drawing.Size(200, 20)
$txtTempLib.PlaceholderText = "Temp Library"
$txtFolder = New-Object System.Windows.Forms.TextBox
$txtFolder.Location = New-Object System.Drawing.Point(20, 130)
$txtFolder.Size = New-Object System.Drawing.Size(220, 20)
$txtFolder.PlaceholderText = "Folder Path (optional)"
$txtFolder.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$btnBrowse = New-Object System.Windows.Forms.Button
$btnBrowse.Location = New-Object System.Drawing.Point(250, 130)
$btnBrowse.Size = New-Object System.Drawing.Size(80, 20)
$btnBrowse.Text = "Browse"
$btnBrowse.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$btnList = New-Object System.Windows.Forms.Button
$btnList.Location = New-Object System.Drawing.Point(20, 170)
$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(150, 170)
$btnConvert.Size = New-Object System.Drawing.Size(120, 30)
$btnConvert.Text = "Convert to XLSX"
$form.Controls.Add($btnConvert)
# Create Status TextBox (Top Right)
$txtStatus = New-Object System.Windows.Forms.TextBox
$txtStatus.Location = New-Object System.Drawing.Point(350, 20)
$txtStatus.Size = New-Object System.Drawing.Size(430, 180)
$txtStatus.Multiline = $true
$txtStatus.WordWrap = $true
$txtStatus.ScrollBars = "Vertical"
$txtStatus.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Left
$form.Controls.Add($txtStatus)
# Create DataGridView (Lower 2/3)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Location = New-Object System.Drawing.Point(20, 230)
$dataGridView.Size = New-Object System.Drawing.Size(760, 320) # Lower 2/3 of the form
$dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top
$dataGridView.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::Fill
$form.Controls.Add($dataGridView)
# Add columns to DataGridView
$dataGridView.ColumnCount = 2
$dataGridView.Columns[0].Name = "Original Path"
$dataGridView.Columns[1].Name = "Original File Name"
# Add Save Config button
$btnSaveConfig = New-Object System.Windows.Forms.Button
$btnSaveConfig.Location = New-Object System.Drawing.Point(20, 100)
$btnSaveConfig.Size = New-Object System.Drawing.Size(80, 20)
$btnSaveConfig.Text = "Save Config"
$btnSaveConfig.Add_Click({
Save-Config -SiteUrl $txtSiteUrl.Text `
-ProductionLibrary $txtProdLib.Text
})
# Add Connect button
$btnConnect = New-Object System.Windows.Forms.Button
$btnConnect.Location = New-Object System.Drawing.Point(110, 100)
$btnConnect.Size = New-Object System.Drawing.Size(80, 20)
$btnConnect.Text = "Connect"
$btnConnect.Add_Click({
if (Connect-SharePoint) {
$script:txtStatus.Text += "Successfully connected to SharePoint`n"
}
})
$form.Controls.AddRange(@(
$txtSiteUrl,
$txtProdLib,
$txtFolder,
$btnBrowse,
$btnSaveConfig,
$btnConnect
))
# Load configuration
if (-not (Load-Config)) {
[System.Windows.Forms.MessageBox]::Show("Configuration missing! Please enter settings.")
}
else {
$txtSiteUrl.Text = $config.SiteUrl
$txtProdLib.Text = $config.ProductionLibrary
}
# Attach event handlers
$btnList.Add_Click({ List-XlsFiles_Click })
$btnConvert.Add_Click({ Convert-Files_Click })
$btnBrowse.Add_Click({
try {
if (-not (Connect-SharePoint)) {
[System.Windows.Forms.MessageBox]::Show("Please configure and connect to SharePoint first!")
return
}
$selectedFolder = Get-FolderBrowser
if ($selectedFolder) {
# Use the ServerRelativeUrl directly from the folder browser
$script:txtFolder.Text = $selectedFolder.ServerRelativeUrl
}
}
catch {
$script:txtStatus.Text += "Folder browse error: $_`n"
}
})
# Store the DataGridView and txtStatus in script scope
$script:dataGridView = $dataGridView
$script:txtStatus = $txtStatus
# Show form
$form.Add_Shown({ $form.Activate() })
[void]$form.ShowDialog()