116 lines
3.5 KiB
PowerShell
116 lines
3.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
|
|
}
|
|
|
|
$tempLibrary = $config.TempLibrary
|
|
$fileList = @()
|
|
|
|
$items = Get-PnPListItem -List $config.ProductionLibrary -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"
|
|
}
|
|
}
|