feat: Implement file conversion logic using DocumentFormat.OpenXml
This commit is contained in:
parent
fe308bc16f
commit
eb6c9adc6b
@ -2,6 +2,15 @@ Add-Type -AssemblyName System.Windows.Forms
|
|||||||
Import-Module Microsoft.Graph.Sites
|
Import-Module Microsoft.Graph.Sites
|
||||||
Import-Module Microsoft.Graph.Files
|
Import-Module Microsoft.Graph.Files
|
||||||
|
|
||||||
|
# Load Open XML assembly
|
||||||
|
try {
|
||||||
|
Add-Type -Path "C:\Program Files\Microsoft Office\root\Office16\DocumentFormat.OpenXml.dll"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
[System.Windows.Forms.MessageBox]::Show("Error loading DocumentFormat.OpenXml.dll. Make sure it is installed.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
# Import our components
|
# Import our components
|
||||||
. "$PSScriptRoot\GraphConfig.ps1"
|
. "$PSScriptRoot\GraphConfig.ps1"
|
||||||
. "$PSScriptRoot\SharePointFunctions.ps1"
|
. "$PSScriptRoot\SharePointFunctions.ps1"
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
# SharePointFunctions.ps1
|
||||||
|
|
||||||
# Load configuration from config.json
|
# Load configuration from config.json
|
||||||
function Load-Config {
|
function Load-Config {
|
||||||
try {
|
try {
|
||||||
@ -75,7 +77,6 @@ function Get-FolderBrowser {
|
|||||||
# Get root folder first
|
# Get root folder first
|
||||||
if ($ParentFolderUrl) {
|
if ($ParentFolderUrl) {
|
||||||
# Get the folder ID first
|
# Get the folder ID first
|
||||||
# Construct the correct URI for the folder
|
|
||||||
# Remove the /sites/tax/Shared Documents/ part from the folder path
|
# Remove the /sites/tax/Shared Documents/ part from the folder path
|
||||||
$relativePath = $ParentFolderUrl.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
|
$relativePath = $ParentFolderUrl.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
|
||||||
|
|
||||||
@ -392,5 +393,110 @@ function Get-XlsFilesCurrentFolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Convert-Files {
|
function Convert-Files {
|
||||||
$script:txtStatus.Text += "Convert-Files function called (placeholder).`n"
|
try {
|
||||||
|
# Load the file list from the JSON file
|
||||||
|
$fileListPath = "$env:TEMP\FileList.json"
|
||||||
|
if (Test-Path $fileListPath) {
|
||||||
|
$fileList = Get-Content $fileListPath -Raw | ConvertFrom-Json
|
||||||
|
} else {
|
||||||
|
$script:txtStatus.Text += "Error: FileList.json not found. Please list files first.`n"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if the file list is empty
|
||||||
|
if ($fileList.Count -eq 0) {
|
||||||
|
$script:txtStatus.Text += "No files to convert.`n"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
# Iterate through the file list and convert each file
|
||||||
|
foreach ($file in $fileList) {
|
||||||
|
$originalPath = $file.OriginalPath
|
||||||
|
$originalFileName = $file.OriginalFileName
|
||||||
|
$tempPath = $file.TempPath
|
||||||
|
|
||||||
|
$script:txtStatus.Text += "Converting file: $originalFileName`n"
|
||||||
|
|
||||||
|
# Download the file from SharePoint
|
||||||
|
$downloadPath = "$env:TEMP\$originalFileName"
|
||||||
|
try {
|
||||||
|
# Extract DriveId and ItemId from the originalPath
|
||||||
|
if ($originalPath -match "drives/([^/]+)/items/([^/]+)") {
|
||||||
|
$driveId = $matches[1]
|
||||||
|
$itemId = $matches[2]
|
||||||
|
} else {
|
||||||
|
throw "Could not extract DriveId and ItemId from OriginalPath"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Download the file
|
||||||
|
$fileContent = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$itemId/content" -Method GET -OutFile $downloadPath
|
||||||
|
if (-not (Test-Path $downloadPath)) {
|
||||||
|
throw "Failed to download file from SharePoint"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$script:txtStatus.Text += "Error downloading file: $($_.Exception.Message)`n"
|
||||||
|
continue # Move to the next file
|
||||||
|
}
|
||||||
|
|
||||||
|
# Convert the file to XLSX
|
||||||
|
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
||||||
|
$xlsxPath = "$env:TEMP\$xlsxFileName"
|
||||||
|
try {
|
||||||
|
# Open the Excel file
|
||||||
|
$excel = New-Object -ComObject Excel.Application
|
||||||
|
$excel.Visible = $false # Keep Excel hidden
|
||||||
|
$workbook = $excel.Workbooks.Open($downloadPath)
|
||||||
|
|
||||||
|
# Save as XLSX
|
||||||
|
$workbook.SaveAs($xlsxPath, 51) # 51 is the code for XLSX format
|
||||||
|
$workbook.Close()
|
||||||
|
$excel.Quit()
|
||||||
|
|
||||||
|
# Clean up COM objects
|
||||||
|
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
|
||||||
|
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
||||||
|
Remove-Variable excel, workbook
|
||||||
|
|
||||||
|
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$script:txtStatus.Text += "Error converting file: $($_.Exception.Message)`n"
|
||||||
|
continue # Move to the next file
|
||||||
|
}
|
||||||
|
|
||||||
|
# Upload the converted file to SharePoint
|
||||||
|
try {
|
||||||
|
# Get the Drive ID and destination folder
|
||||||
|
$destinationFolder = $script:txtTempLib.Text
|
||||||
|
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$driveId/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 {
|
||||||
|
Remove-Item $downloadPath -ErrorAction SilentlyContinue
|
||||||
|
Remove-Item $xlsxPath -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$script:txtStatus.Text += "Error cleaning up temporary files: $($_.Exception.Message)`n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$script:txtStatus.Text += "File conversion complete.`n"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$script:txtStatus.Text += "Error in Convert-Files function: $($_.Exception.Message)`n"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user