SharepointXLStoXLSX/SharePointFunctions.ps1

142 lines
4.5 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,
[string]$ClientId,
[string]$ClientSecret
)
$config = @{
SiteUrl = $SiteUrl
ProductionLibrary = $ProductionLibrary
TempLibrary = $TempLibrary
ClientId = $ClientId
ClientSecret = $ClientSecret
}
$config | ConvertTo-Json | Set-Content $configPath
}
function Connect-SharePoint {
try {
Connect-PnPOnline -Url $config.SiteUrl -ClientId $config.ClientId -ClientSecret $config.ClientSecret
return $true
}
catch {
$script:txtStatus.Text += "Error connecting: $_`n"
return $false
}
}
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 = "$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/$($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 -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 "$($script:txtProdLib.Text)/$xlsxPath" -TargetUrl $xlsxPath -Force
$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"
}
}