Compare commits
10 Commits
b55058161a
...
ffbe44da0a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ffbe44da0a | ||
|
|
e615e043e3 | ||
|
|
64540f9a0e | ||
|
|
ff2500707f | ||
|
|
b725f52366 | ||
|
|
93ffe6a5a2 | ||
|
|
31407f0330 | ||
|
|
2077423bc0 | ||
|
|
b8049f6f85 | ||
|
|
cc887b4d3e |
@ -5,8 +5,7 @@ Import-Module Microsoft.Graph.Files
|
||||
|
||||
# Import our components
|
||||
. "$PSScriptRoot\GraphConfig.ps1"
|
||||
$SharePointFunctions = "$PSScriptRoot\SharePointFunctions.ps1"
|
||||
Import-Module $SharePointFunctions -Force
|
||||
. "$PSScriptRoot\SharePointFunctions.ps1"
|
||||
. "$PSScriptRoot\EventHandlers.ps1"
|
||||
|
||||
# Create main form
|
||||
|
||||
@ -2,17 +2,6 @@
|
||||
|
||||
$script:config = $null # Initialize config variable
|
||||
|
||||
# Export functions that will be called from other scripts
|
||||
Export-ModuleMember -Function @(
|
||||
'Load-Config',
|
||||
'Save-Config',
|
||||
'Get-FolderBrowser',
|
||||
'Connect-SharePoint',
|
||||
'List-XlsFiles',
|
||||
'Convert-Files',
|
||||
'Move-Files'
|
||||
)
|
||||
|
||||
# Load configuration from config.json
|
||||
function Load-Config {
|
||||
try {
|
||||
@ -469,36 +458,50 @@ function Convert-Files {
|
||||
|
||||
# Convert the file to XLSX
|
||||
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
||||
# Use Join-Path to properly handle paths with spaces
|
||||
# Before the Excel conversion, get the full paths and clean them
|
||||
$xlsxPath = Join-Path $env:TEMP $xlsxFileName
|
||||
$downloadPath = Join-Path $env:TEMP $originalFileName
|
||||
|
||||
# Convert the paths to proper Windows paths
|
||||
# Clean the paths and ensure they're valid
|
||||
$xlsxPath = [System.IO.Path]::GetFullPath($xlsxPath)
|
||||
$downloadPath = [System.IO.Path]::GetFullPath($downloadPath)
|
||||
|
||||
# Verify the download file exists
|
||||
if (-not (Test-Path -LiteralPath $downloadPath)) {
|
||||
throw "Downloaded file not found at: $downloadPath"
|
||||
}
|
||||
|
||||
try {
|
||||
# Open Excel
|
||||
$excel = New-Object -ComObject Excel.Application
|
||||
$excel.Visible = $false
|
||||
$excel.DisplayAlerts = $false # Suppress Excel alerts
|
||||
$excel.DisplayAlerts = $false
|
||||
|
||||
try {
|
||||
Write-Host "Opening workbook: $downloadPath"
|
||||
$workbook = $excel.Workbooks.Open($downloadPath)
|
||||
|
||||
# Save as XLSX using the full path
|
||||
$workbook.SaveAs([string]$xlsxPath, 51) # 51 = xlOpenXMLWorkbook (*.xlsx)
|
||||
$workbook.Close($true) # True to save changes
|
||||
|
||||
Start-Sleep -Seconds 1 # Give Excel time to finish saving
|
||||
|
||||
# Verify the file was created
|
||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||
throw "Excel SaveAs succeeded but file not found at: $xlsxPath"
|
||||
Write-Host "Saving as XLSX: $xlsxPath"
|
||||
try {
|
||||
# Save directly to the final path
|
||||
$workbook.SaveAs([string]$xlsxPath, 51) # 51 = xlWorkbookDefault (xlsx format)
|
||||
$workbook.Close($true)
|
||||
}
|
||||
catch {
|
||||
$script:txtStatus.Text += "Error during SaveAs: $($_.Exception.Message)`n"
|
||||
throw "SaveAs failed: $($_.Exception.Message)" # Re-throw the exception to be caught by the outer catch
|
||||
}
|
||||
|
||||
Start-Sleep -Seconds 2 # Give Excel time to finish
|
||||
|
||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||
throw "Failed to create XLSX file at: $xlsxPath"
|
||||
}
|
||||
|
||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||
}
|
||||
finally {
|
||||
# Proper cleanup of Excel COM objects
|
||||
# Cleanup
|
||||
$excel.Quit()
|
||||
if ($workbook) {
|
||||
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
||||
@ -506,6 +509,11 @@ function Convert-Files {
|
||||
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
|
||||
[System.GC]::Collect()
|
||||
[System.GC]::WaitForPendingFinalizers()
|
||||
|
||||
# Force cleanup of any remaining Excel processes
|
||||
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } |
|
||||
Stop-Process -Force
|
||||
}
|
||||
|
||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||
@ -513,13 +521,25 @@ function Convert-Files {
|
||||
# Upload the converted file to SharePoint
|
||||
try {
|
||||
# Verify file exists before trying to read it
|
||||
if (-not (Test-Path $xlsxPath)) {
|
||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||
throw "Converted file not found at: $xlsxPath"
|
||||
}
|
||||
|
||||
# Get the destination folder
|
||||
$destinationFolder = $script:txtTempLib.Text
|
||||
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content"
|
||||
# Get the destination folder from the TempLibrary field
|
||||
$tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
||||
"Documents"
|
||||
} else {
|
||||
$script:txtTempLib.Text
|
||||
}
|
||||
|
||||
# Get the drive for the temp library
|
||||
$tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary }
|
||||
if (-not $tempDrive) {
|
||||
throw "Temp library not found: $tempLibrary"
|
||||
}
|
||||
|
||||
# Construct the upload URI for the temp library
|
||||
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($tempDrive.Id)/root:/$($xlsxFileName):/content"
|
||||
|
||||
# Read the file content
|
||||
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
||||
@ -527,11 +547,11 @@ function Convert-Files {
|
||||
# Upload the file
|
||||
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
||||
|
||||
$script:txtStatus.Text += "Successfully uploaded to SharePoint: $destinationFolder/$xlsxFileName`n"
|
||||
$script:txtStatus.Text += "Successfully uploaded to temp library: $tempLibrary/$xlsxFileName`n"
|
||||
}
|
||||
catch {
|
||||
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||
if (-not (Test-Path $xlsxPath)) {
|
||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||
$script:txtStatus.Text += "File not found at: $xlsxPath`n"
|
||||
}
|
||||
continue # Move to the next file
|
||||
@ -542,24 +562,6 @@ function Convert-Files {
|
||||
continue # Move to the next file
|
||||
}
|
||||
|
||||
# Upload the converted file to SharePoint
|
||||
try {
|
||||
# Get the destination folder
|
||||
$destinationFolder = $script:txtTempLib.Text
|
||||
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content"
|
||||
|
||||
# Read the file content
|
||||
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
||||
|
||||
# Upload the file
|
||||
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
||||
|
||||
$script:txtStatus.Text += "Successfully uploaded to SharePoint: $destinationFolder/$xlsxFileName`n"
|
||||
}
|
||||
catch {
|
||||
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||
continue # Move to the next file
|
||||
}
|
||||
|
||||
# Clean up temporary files
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user