From 7822f074c4cf5e07914dbe3e3ce3849b5450db08 Mon Sep 17 00:00:00 2001 From: Bobby Abellana Date: Mon, 24 Feb 2025 14:37:47 -0800 Subject: [PATCH] ConvertsXLStoXLSX and save to original location --- EventHandlers.ps1 | 5 - MainForm.ps1 | 15 +-- SharePointFunctions.ps1 | 207 ++-------------------------------------- 3 files changed, 11 insertions(+), 216 deletions(-) diff --git a/EventHandlers.ps1 b/EventHandlers.ps1 index 4bc054b..7d3c6d2 100644 --- a/EventHandlers.ps1 +++ b/EventHandlers.ps1 @@ -8,8 +8,3 @@ function Convert-Files_Click { # Rename to avoid conflict $script:txtStatus.Text += "Starting conversion...`n" Convert-Files # Call the function in SharePointFunctions.ps1 } - -function Move-Files_Click { # Rename to avoid conflict - $script:txtStatus.Text += "Moving files...`n" - Move-Files # Call the function in SharePointFunctions.ps1 -} diff --git a/MainForm.ps1 b/MainForm.ps1 index 5fd9a34..554c49b 100644 --- a/MainForm.ps1 +++ b/MainForm.ps1 @@ -60,12 +60,6 @@ $btnConvert.Size = New-Object System.Drawing.Size(120, 30) $btnConvert.Text = "Convert to XLSX" $form.Controls.Add($btnConvert) -$btnMove = New-Object System.Windows.Forms.Button -$btnMove.Location = New-Object System.Drawing.Point(300, 170) -$btnMove.Size = New-Object System.Drawing.Size(120, 30) -$btnMove.Text = "Move Files" -$form.Controls.Add($btnMove) - # Create Checkbox for Recursive Listing $chkRecursive = New-Object System.Windows.Forms.CheckBox $chkRecursive.Location = New-Object System.Drawing.Point(20, 205) @@ -96,10 +90,9 @@ $dataGridView.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeCo $form.Controls.Add($dataGridView) # Add columns to DataGridView -$dataGridView.ColumnCount = 3 +$dataGridView.ColumnCount = 2 $dataGridView.Columns[0].Name = "Original Path" $dataGridView.Columns[1].Name = "Original File Name" -$dataGridView.Columns[2].Name = "Temp Path" # Add Save Config button $btnSaveConfig = New-Object System.Windows.Forms.Button @@ -108,8 +101,7 @@ $btnSaveConfig.Size = New-Object System.Drawing.Size(80, 20) $btnSaveConfig.Text = "Save Config" $btnSaveConfig.Add_Click({ Save-Config -SiteUrl $txtSiteUrl.Text ` - -ProductionLibrary $txtProdLib.Text ` - -TempLibrary $txtTempLib.Text + -ProductionLibrary $txtProdLib.Text }) # Add Connect button @@ -126,7 +118,6 @@ $btnConnect.Add_Click({ $form.Controls.AddRange(@( $txtSiteUrl, $txtProdLib, - $txtTempLib, $txtFolder, $btnBrowse, $btnSaveConfig, @@ -141,13 +132,11 @@ if (-not (Load-Config)) { else { $txtSiteUrl.Text = $config.SiteUrl $txtProdLib.Text = $config.ProductionLibrary - $txtTempLib.Text = $config.TempLibrary } # Attach event handlers $btnList.Add_Click({ List-XlsFiles_Click }) $btnConvert.Add_Click({ Convert-Files_Click }) -$btnMove.Add_Click({ Move-Files_Click }) $btnBrowse.Add_Click({ try { if (-not (Connect-SharePoint)) { diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index a98eb14..41655a8 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -282,7 +282,7 @@ function List-XlsFiles { $script:txtStatus.Text += "Saving file list...`n" # Display each file on a new line foreach ($file in $fileList) { - $row = @($file.OriginalPath, $file.OriginalFileName, $file.TempPath) + $row = @($file.OriginalPath, $file.OriginalFileName) $script:dataGridView.Rows.Add($row) } #$fileList | ForEach-Object { @@ -341,17 +341,6 @@ function Get-XlsFilesRecursive { $allFiles += [PSCustomObject]@{ OriginalPath = $item.WebUrl OriginalFileName = $item.Name - TempPath = if ($script:txtFolder.Text) { - # Extract the relative path from the original path - $relativePath = $item.ParentReference.Path -replace ".*root:/", "" - if ($relativePath) { - "$($script:txtTempLib.Text)/$relativePath/$($item.Name)".Replace(".xls", ".xlsx") - } else { - "$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx") - } - } else { - "$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx") - } } } } @@ -396,17 +385,6 @@ function Get-XlsFilesCurrentFolder { $allFiles += [PSCustomObject]@{ OriginalPath = $item.WebUrl OriginalFileName = $item.Name - TempPath = if ($script:txtFolder.Text) { - # Extract the relative path from the original path - $relativePath = $item.ParentReference.Path -replace ".*root:/", "" - if ($relativePath) { - "$($script:txtTempLib.Text)/$relativePath/$($item.Name)".Replace(".xls", ".xlsx") - } else { - "$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx") - } - } else { - "$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx") - } } } } @@ -416,11 +394,6 @@ function Get-XlsFilesCurrentFolder { function Convert-Files { try { - # Ensure the Testing library exists before proceeding - if (-not (Ensure-TestingLibraryExists)) { - throw "Failed to ensure Testing library exists" - } - # Get site and drive information first $script:txtStatus.Text += "Getting SharePoint site information...`n" $site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop @@ -668,27 +641,14 @@ function Convert-Files { throw "Converted file not found at: $xlsxPath" } - # Get the drive for the temp library - $tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") { - "Documents" - } else { - $script:txtTempLib.Text - } - $tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary } - if (-not $tempDrive) { - throw "Temp library not found: $tempLibrary" - } - - # Use the correct drive ID - $driveId = $tempDrive.Id - - # Extract folder path from TempPath, but remove the library name prefix - $relativePath = $file.TempPath -replace "/[^/]+$", "" # Extract folder path from TempPath - $relativePath = $relativePath -replace "^$($script:txtTempLib.Text)/", "" # Remove library name prefix + # Get the folder path from the original file + $folderPath = $script:txtFolder.Text $uploadFileName = $xlsxFileName # Use the converted file name - # Construct the upload URI for the temp library, properly encoding the path - $uploadPath = if ($relativePath) { + # Construct the upload URI for the original library + $uploadPath = if ($folderPath) { + # Extract the relative path if needed + $relativePath = $folderPath -replace "/sites/tax/Shared Documents/", "" "$relativePath/$uploadFileName" # Include folder structure } else { $uploadFileName # Just use the filename to save in root @@ -703,6 +663,7 @@ function Convert-Files { # Fix the URI construction to avoid the colon issue $baseUri = "https://graph.microsoft.com/v1.0/drives" + $driveId = $script:driveId $uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content" $script:txtStatus.Text += "Uploading to: $uploadUri`n" @@ -713,9 +674,7 @@ function Convert-Files { # Upload the file Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream" - if ($script:txtStatus) { - $script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadPath`n" - } + $script:txtStatus.Text += "Successfully uploaded to library: /sites/tax/$libraryName/$uploadPath`n" } catch { $script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n" @@ -856,151 +815,3 @@ function Move-Files { $script:txtStatus.Text += "Stack Trace: $($_.Exception.StackTrace)`n" } } - -# Function to ensure the temp library exists -function Ensure-TestingLibraryExists { - try { - # Get the library name from TempLibrary field - $tempLibraryName = if ($script:txtTempLib.Text -eq "Shared Documents") { - "Documents" - } else { - $script:txtTempLib.Text - } - - if ($script:txtStatus) { - $script:txtStatus.Text += "Checking if $tempLibraryName library exists...`n" - } else { - Write-Host "Checking if $tempLibraryName library exists..." - } - - # Get site and drive information - $site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop - - if (-not $site) { - throw "Could not find SharePoint site" - } - - $drives = Get-MgSiteDrive -SiteId $site.id - $tempLibrary = $drives | Where-Object { $_.Name -eq $tempLibraryName } - - if (-not $tempLibrary) { - if ($script:txtStatus) { - $script:txtStatus.Text += "$tempLibraryName library not found. Creating library...`n" - } else { - Write-Host "$tempLibraryName library not found. Creating library..." - } - - # Create the library - $createLibraryUri = "https://graph.microsoft.com/v1.0/sites/$($site.id)/lists" - $body = @{ - displayName = $tempLibraryName - list = @{ - template = "documentLibrary" - } - } | ConvertTo-Json - - Invoke-MgGraphRequest -Uri $createLibraryUri -Method POST -Body $body -ContentType "application/json" - - if ($script:txtStatus) { - $script:txtStatus.Text += "$tempLibraryName library created successfully.`n" - } else { - Write-Host "$tempLibraryName library created successfully." - } - } else { - if ($script:txtStatus) { - $script:txtStatus.Text += "$tempLibraryName library already exists.`n" - } else { - Write-Host "$tempLibraryName library already exists." - } - } - - return $true - } - catch { - if ($script:txtStatus) { - $script:txtStatus.Text += "Error ensuring library exists: $($_.Exception.Message)`n" - } else { - Write-Host "Error ensuring library exists: $($_.Exception.Message)" - } - return $false - } -} - -function Upload-ConvertedFile { - param ( - [Parameter(Mandatory=$true)] - [string]$xlsxPath, - [Parameter(Mandatory=$true)] - [string]$uploadFolder, - [Parameter(Mandatory=$true)] - [string]$uploadFileName - ) - - try { - # Verify file exists before trying to read it - if (-not (Test-Path -LiteralPath $xlsxPath)) { - throw "Converted file not found at: $xlsxPath" - } - - # 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" - } - - # Use the correct drive ID - $driveId = $tempDrive.Id - - # Extract folder path from TempPath, but remove the library name prefix - $relativePath = $file.TempPath -replace "/[^/]+$", "" # Extract folder path from TempPath - $relativePath = $relativePath -replace "^$($script:txtTempLib.Text)/", "" # Remove library name prefix - $uploadFileName = $xlsxFileName # Use the converted file name - - # Construct the upload URI for the temp library, properly encoding the path - $uploadPath = if ($relativePath) { - "$relativePath/$uploadFileName" # Include folder structure - } else { - $uploadFileName # Just use the filename to save in root - } - - # URL encode the path but preserve forward slashes and encode spaces as %20 - $encodedPath = $uploadPath.Split('/') | - ForEach-Object { - [System.Web.HttpUtility]::UrlEncode($_).Replace("+", "%20") - } | - Join-String -Separator '/' - - # Fix the URI construction to avoid the colon issue - $baseUri = "https://graph.microsoft.com/v1.0/drives" - $uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content" - - if ($script:txtStatus) { - $script:txtStatus.Text += "Uploading to: $uploadUri`n" - } - - # 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" - - if ($script:txtStatus) { - $script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadPath`n" - } - } - catch { - if ($script:txtStatus) { - $script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n" - } else { - Write-Host "Error uploading file: $($_.Exception.Message)" - } - throw - } -}