feat: Add folder browsing and dynamic library configuration fields
This commit is contained in:
parent
77e4db0d78
commit
b2a495af78
69
MainForm.ps1
69
MainForm.ps1
@ -8,54 +8,93 @@ Import-Module PnP.PowerShell
|
|||||||
# Create main form
|
# Create main form
|
||||||
$form = New-Object System.Windows.Forms.Form
|
$form = New-Object System.Windows.Forms.Form
|
||||||
$form.Text = "SharePoint XLS Converter"
|
$form.Text = "SharePoint XLS Converter"
|
||||||
$form.Size = New-Object System.Drawing.Size(600,400)
|
$form.Size = New-Object System.Drawing.Size(600,500)
|
||||||
|
|
||||||
# Add controls
|
# Add controls
|
||||||
$txtSiteUrl = New-Object System.Windows.Forms.TextBox
|
$lblConfig = New-Object System.Windows.Forms.Label
|
||||||
$txtSiteUrl.Location = New-Object System.Drawing.Point(20,120)
|
$lblConfig.Text = "SharePoint Configuration:"
|
||||||
$txtSiteUrl.Size = New-Object System.Drawing.Size(200,20)
|
$lblConfig.Location = New-Object System.Drawing.Point(20,20)
|
||||||
|
$lblConfig.Size = New-Object System.Drawing.Size(200,20)
|
||||||
|
$form.Controls.Add($lblConfig)
|
||||||
|
|
||||||
$btnSaveConfig = New-Object System.Windows.Forms.Button
|
$txtSiteUrl = New-Object System.Windows.Forms.TextBox
|
||||||
$btnSaveConfig.Location = New-Object System.Drawing.Point(230,120)
|
$txtSiteUrl.Location = New-Object System.Drawing.Point(20,40)
|
||||||
$btnSaveConfig.Size = New-Object System.Drawing.Size(80,20)
|
$txtSiteUrl.Size = New-Object System.Drawing.Size(200,20)
|
||||||
$btnSaveConfig.Text = "Save Config"
|
$txtSiteUrl.PlaceholderText = "Site URL"
|
||||||
|
|
||||||
|
$txtProdLib = New-Object System.Windows.Forms.TextBox
|
||||||
|
$txtProdLib.Location = New-Object System.Drawing.Point(20,70)
|
||||||
|
$txtProdLib.Size = New-Object System.Drawing.Size(200,20)
|
||||||
|
$txtProdLib.PlaceholderText = "Production Library"
|
||||||
|
|
||||||
|
$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(200,20)
|
||||||
|
$txtFolder.PlaceholderText = "Folder Path (optional)"
|
||||||
|
|
||||||
|
$btnBrowse = New-Object System.Windows.Forms.Button
|
||||||
|
$btnBrowse.Location = New-Object System.Drawing.Point(230,130)
|
||||||
|
$btnBrowse.Size = New-Object System.Drawing.Size(80,20)
|
||||||
|
$btnBrowse.Text = "Browse"
|
||||||
|
|
||||||
$btnList = New-Object System.Windows.Forms.Button
|
$btnList = New-Object System.Windows.Forms.Button
|
||||||
$btnList.Location = New-Object System.Drawing.Point(20,20)
|
$btnList.Location = New-Object System.Drawing.Point(20,170)
|
||||||
$btnList.Size = New-Object System.Drawing.Size(120,30)
|
$btnList.Size = New-Object System.Drawing.Size(120,30)
|
||||||
$btnList.Text = "List XLS Files"
|
$btnList.Text = "List XLS Files"
|
||||||
$form.Controls.Add($btnList)
|
$form.Controls.Add($btnList)
|
||||||
|
|
||||||
$btnConvert = New-Object System.Windows.Forms.Button
|
$btnConvert = New-Object System.Windows.Forms.Button
|
||||||
$btnConvert.Location = New-Object System.Drawing.Point(160,20)
|
$btnConvert.Location = New-Object System.Drawing.Point(160,170)
|
||||||
$btnConvert.Size = New-Object System.Drawing.Size(120,30)
|
$btnConvert.Size = New-Object System.Drawing.Size(120,30)
|
||||||
$btnConvert.Text = "Convert to XLSX"
|
$btnConvert.Text = "Convert to XLSX"
|
||||||
$form.Controls.Add($btnConvert)
|
$form.Controls.Add($btnConvert)
|
||||||
|
|
||||||
$btnMove = New-Object System.Windows.Forms.Button
|
$btnMove = New-Object System.Windows.Forms.Button
|
||||||
$btnMove.Location = New-Object System.Drawing.Point(300,20)
|
$btnMove.Location = New-Object System.Drawing.Point(300,170)
|
||||||
$btnMove.Size = New-Object System.Drawing.Size(120,30)
|
$btnMove.Size = New-Object System.Drawing.Size(120,30)
|
||||||
$btnMove.Text = "Move Files"
|
$btnMove.Text = "Move Files"
|
||||||
$form.Controls.Add($btnMove)
|
$form.Controls.Add($btnMove)
|
||||||
|
|
||||||
$txtStatus = New-Object System.Windows.Forms.TextBox
|
$txtStatus = New-Object System.Windows.Forms.TextBox
|
||||||
$txtStatus.Location = New-Object System.Drawing.Point(20,70)
|
$txtStatus.Location = New-Object System.Drawing.Point(20,210)
|
||||||
$txtStatus.Size = New-Object System.Drawing.Size(540,280)
|
$txtStatus.Size = New-Object System.Drawing.Size(540,240)
|
||||||
$txtStatus.Multiline = $true
|
$txtStatus.Multiline = $true
|
||||||
$txtStatus.ScrollBars = "Vertical"
|
$txtStatus.ScrollBars = "Vertical"
|
||||||
$form.Controls.Add($txtStatus)
|
$form.Controls.Add($txtStatus)
|
||||||
$form.Controls.Add($txtSiteUrl)
|
$form.Controls.AddRange(@(
|
||||||
$form.Controls.Add($btnSaveConfig)
|
$txtSiteUrl,
|
||||||
|
$txtProdLib,
|
||||||
|
$txtTempLib,
|
||||||
|
$txtFolder,
|
||||||
|
$btnBrowse
|
||||||
|
))
|
||||||
|
|
||||||
# Load configuration
|
# Load configuration
|
||||||
if (-not (Load-Config)) {
|
if (-not (Load-Config)) {
|
||||||
[System.Windows.Forms.MessageBox]::Show("Configuration missing! Please enter settings.")
|
[System.Windows.Forms.MessageBox]::Show("Configuration missing! Please enter settings.")
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$txtSiteUrl.Text = $config.SiteUrl
|
||||||
|
$txtProdLib.Text = $config.ProductionLibrary
|
||||||
|
$txtTempLib.Text = $config.TempLibrary
|
||||||
|
}
|
||||||
|
|
||||||
# Attach event handlers
|
# Attach event handlers
|
||||||
$btnList.Add_Click({ List-XlsFiles })
|
$btnList.Add_Click({ List-XlsFiles })
|
||||||
$btnConvert.Add_Click({ Convert-Files })
|
$btnConvert.Add_Click({ Convert-Files })
|
||||||
$btnMove.Add_Click({ Move-Files })
|
$btnMove.Add_Click({ Move-Files })
|
||||||
|
$btnBrowse.Add_Click({
|
||||||
|
$selectedFolder = Get-FolderBrowser -context (Get-PnPConnection)
|
||||||
|
if ($selectedFolder) {
|
||||||
|
$relativePath = $selectedFolder.ServerRelativeUrl -replace [regex]::Escape((Get-PnPWeb).ServerRelativeUrl), ""
|
||||||
|
$script:txtFolder.Text = $relativePath.Trim('/')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
# Show form
|
# Show form
|
||||||
$form.Add_Shown({$form.Activate()})
|
$form.Add_Shown({$form.Activate()})
|
||||||
|
|||||||
@ -51,11 +51,19 @@ function List-XlsFiles {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
$tempLibrary = $config.TempLibrary
|
$folderPath = $script:txtFolder.Text
|
||||||
$fileList = @()
|
$query = "<View Scope='RecursiveAll'><Query><Where>
|
||||||
|
<And>
|
||||||
|
<Contains><FieldRef Name='FileRef'/><Value Type='Text'>.xls</Value></Contains>
|
||||||
|
<Not><Contains><FieldRef Name='FileRef'/><Value Type='Text'>.xlsx</Value></Contains></Not>
|
||||||
|
</And>
|
||||||
|
</Where></Query></View>"
|
||||||
|
|
||||||
|
$items = Get-PnPListItem -List $script:txtProdLib.Text -PageSize 2000 -Query $query
|
||||||
|
|
||||||
$items = Get-PnPListItem -List $config.ProductionLibrary -PageSize 2000 | Where {
|
if (-not [string]::IsNullOrWhiteSpace($folderPath)) {
|
||||||
($_.FieldValues.FileRef -like "*.xls") -and (-not $_.FieldValues.FileRef.Contains(".xlsx"))
|
$folderFilter = "*$($folderPath.Replace('\','/'))*"
|
||||||
|
$items = $items | Where { $_.FieldValues.FileRef -like $folderFilter }
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($item in $items) {
|
foreach ($item in $items) {
|
||||||
@ -78,7 +86,7 @@ function Convert-Files {
|
|||||||
$excel.Visible = $false
|
$excel.Visible = $false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Get-PnPFile -Url "/sites/yoursite/Documents/Temp/FileList.json" -Path $env:TEMP -Filename "FileList.json"
|
Get-PnPFile -Url "/sites/yoursite/$($script:txtTempLib.Text)/FileList.json" -Path $env:TEMP -Filename "FileList.json"
|
||||||
$files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json
|
$files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json
|
||||||
|
|
||||||
foreach ($file in $files) {
|
foreach ($file in $files) {
|
||||||
@ -109,7 +117,25 @@ function Move-Files {
|
|||||||
foreach ($file in $files) {
|
foreach ($file in $files) {
|
||||||
Move-PnPFile -SourceUrl $file.OriginalPath -TargetUrl $file.TempPath -Force
|
Move-PnPFile -SourceUrl $file.OriginalPath -TargetUrl $file.TempPath -Force
|
||||||
$xlsxPath = [System.IO.Path]::ChangeExtension($file.OriginalPath, ".xlsx")
|
$xlsxPath = [System.IO.Path]::ChangeExtension($file.OriginalPath, ".xlsx")
|
||||||
Move-PnPFile -SourceUrl "$tempLibrary/$xlsxPath" -TargetUrl $xlsxPath -Force
|
Move-PnPFile -SourceUrl "$($script:txtProdLib.Text)/$xlsxPath" -TargetUrl $xlsxPath -Force
|
||||||
$script:txtStatus.Text += "Moved $($file.OriginalFileName) and its XLSX version`n"
|
$script:txtStatus.Text += "Moved $($file.OriginalFileName) and its XLSX version`n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function Get-FolderBrowser {
|
||||||
|
param($context)
|
||||||
|
try {
|
||||||
|
$folder = Get-PnPFolder -Url $script:txtProdLib.Text
|
||||||
|
$ctx = $context
|
||||||
|
$folders = @()
|
||||||
|
|
||||||
|
$fld = $ctx.Web.GetFolderByServerRelativePath($folder.ServerRelativeUrl)
|
||||||
|
$ctx.Load($fld.Folders)
|
||||||
|
$ctx.ExecuteQuery()
|
||||||
|
|
||||||
|
$folders += $fld.Folders | Select-Object Name, ServerRelativeUrl
|
||||||
|
return $folders | Out-GridView -Title "Select Folder" -PassThru
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$script:txtStatus.Text += "Error browsing folders: $_`n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user