SharepointXLStoXLSX/SharePointFunctions.ps1
2025-02-19 15:34:44 -08:00

133 lines
4.2 KiB
PowerShell

$configPath = Join-Path $PSScriptRoot "config.json"
function Load-Config {
if (Test-Path $configPath) {
try {
$script:config = Get-Content $configPath | ConvertFrom-Json
$script:txtSiteUrl.Text = $config.SiteUrl
return $true
}
catch {
Write-Error "Invalid config file: $_"
return $false
}
}
return $false
}
function Save-Config {
param(
[string]$SiteUrl,
[string]$ProductionLibrary,
[string]$TempLibrary
)
$config = @{
SiteUrl = $SiteUrl
ProductionLibrary = $ProductionLibrary
TempLibrary = $TempLibrary
}
$config | ConvertTo-Json | Set-Content $configPath
}
function Connect-SharePoint {
return Connect-Graph
}
function List-XlsFiles {
if (-not (Connect-SharePoint)) {
return
}
$folderPath = $script:txtFolder.Text
$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
if (-not [string]::IsNullOrWhiteSpace($folderPath)) {
$folderFilter = "*$($folderPath.Replace('\','/'))*"
$items = $items | Where { $_.FieldValues.FileRef -like $folderFilter }
}
foreach ($item in $items) {
$fileList += [PSCustomObject]@{
OriginalPath = $item.FieldValues.FileRef
TempPath = "$($script:txtTempLib.Text)/$($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)) {
}
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
try {
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
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)) {
}
$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 "$($script:txtProdLib.Text)/$xlsxPath" -TargetUrl $xlsxPath -Force
$script:txtStatus.Text += "Moved $($file.OriginalFileName) and its XLSX version`n"
}
}
function Get-FolderBrowser {
try {
# Verify library exists and is accessible
$library = Get-PnPList -Identity $script:txtProdLib.Text -Includes RootFolder,HasUniqueRoleAssignments
if ($library.HasUniqueRoleAssignments) {
throw "This library has unique permissions - verify app has access"
}
# Get folders with elevated permissions
$ctx = Get-PnPContext
$ctx.Load($library.RootFolder.Folders)
$ctx.ExecuteQuery()
return $library.RootFolder.Folders |
Select-Object Name, ServerRelativeUrl |
Out-GridView -Title "Select Folder" -PassThru
}
catch {
$script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"
return $null
}
}