add: Create public function for merge
This commit is contained in:
33
source/Private/Update-CISExcelWorksheet.ps1
Normal file
33
source/Private/Update-CISExcelWorksheet.ps1
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
function Update-CISExcelWorksheet {
|
||||||
|
[CmdletBinding()]
|
||||||
|
param (
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$ExcelPath,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$WorksheetName,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[psobject[]]$Data,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $false)]
|
||||||
|
[int]$StartingRowIndex = 2 # Default starting row index, assuming row 1 has headers
|
||||||
|
)
|
||||||
|
|
||||||
|
process {
|
||||||
|
# Load the existing Excel sheet
|
||||||
|
$excelPackage = Open-ExcelPackage -Path $ExcelPath
|
||||||
|
$worksheet = $excelPackage.Workbook.Worksheets[$WorksheetName]
|
||||||
|
|
||||||
|
if (-not $worksheet) {
|
||||||
|
throw "Worksheet '$WorksheetName' not found in '$ExcelPath'"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Update the worksheet with the provided data
|
||||||
|
Update-WorksheetCells -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex
|
||||||
|
|
||||||
|
# Save and close the Excel package
|
||||||
|
Close-ExcelPackage $excelPackage
|
||||||
|
}
|
||||||
|
}
|
28
source/Private/Update-WorksheetCells.ps1
Normal file
28
source/Private/Update-WorksheetCells.ps1
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
function Update-WorksheetCells {
|
||||||
|
param (
|
||||||
|
$Worksheet,
|
||||||
|
$Data,
|
||||||
|
$StartingRowIndex
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check and set headers
|
||||||
|
$firstItem = $Data[0]
|
||||||
|
$colIndex = 1
|
||||||
|
foreach ($property in $firstItem.PSObject.Properties) {
|
||||||
|
if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) {
|
||||||
|
$Worksheet.Cells[1, $colIndex].Value = $property.Name
|
||||||
|
}
|
||||||
|
$colIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
# Iterate over each row in the data and update cells
|
||||||
|
$rowIndex = $StartingRowIndex
|
||||||
|
foreach ($item in $Data) {
|
||||||
|
$colIndex = 1
|
||||||
|
foreach ($property in $item.PSObject.Properties) {
|
||||||
|
$Worksheet.Cells[$rowIndex, $colIndex].Value = $property.Value
|
||||||
|
$colIndex++
|
||||||
|
}
|
||||||
|
$rowIndex++
|
||||||
|
}
|
||||||
|
}
|
61
source/Public/Sync-CISExcelAndCsvData.ps1
Normal file
61
source/Public/Sync-CISExcelAndCsvData.ps1
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Synchronizes data between an Excel file and a CSV file and optionally updates the Excel worksheet.
|
||||||
|
.DESCRIPTION
|
||||||
|
The Sync-CISExcelAndCsvData function merges data from a specified Excel file and a CSV file based on a common key. It can also update the Excel worksheet with the merged data. This function is particularly useful for updating Excel records with additional data from a CSV file while preserving the original formatting and structure of the Excel worksheet.
|
||||||
|
.PARAMETER ExcelPath
|
||||||
|
The path to the Excel file that contains the original data. This parameter is mandatory.
|
||||||
|
.PARAMETER WorksheetName
|
||||||
|
The name of the worksheet within the Excel file that contains the data to be synchronized. This parameter is mandatory.
|
||||||
|
.PARAMETER CsvPath
|
||||||
|
The path to the CSV file containing data to be merged with the Excel data. This parameter is mandatory.
|
||||||
|
.PARAMETER SkipUpdate
|
||||||
|
If specified, the function will return the merged data object without updating the Excel worksheet. This is useful for previewing the merged data.
|
||||||
|
.EXAMPLE
|
||||||
|
PS> Sync-CISExcelAndCsvData -ExcelPath "path\to\excel.xlsx" -WorksheetName "DataSheet" -CsvPath "path\to\data.csv"
|
||||||
|
Merges data from 'data.csv' into 'excel.xlsx' on the 'DataSheet' worksheet and updates the worksheet with the merged data.
|
||||||
|
.EXAMPLE
|
||||||
|
PS> $mergedData = Sync-CISExcelAndCsvData -ExcelPath "path\to\excel.xlsx" -WorksheetName "DataSheet" -CsvPath "path\to\data.csv" -SkipUpdate
|
||||||
|
Retrieves the merged data object for preview without updating the Excel worksheet.
|
||||||
|
.INPUTS
|
||||||
|
None. You cannot pipe objects to Sync-CISExcelAndCsvData.
|
||||||
|
.OUTPUTS
|
||||||
|
Object[]
|
||||||
|
If the SkipUpdate switch is used, the function returns an array of custom objects representing the merged data.
|
||||||
|
.NOTES
|
||||||
|
- Ensure that the 'ImportExcel' module is installed and up to date.
|
||||||
|
- It is recommended to backup the Excel file before running this script to prevent accidental data loss.
|
||||||
|
- This function is part of the CIS Excel and CSV Data Management Toolkit.
|
||||||
|
.LINK
|
||||||
|
Online documentation: [Your Documentation Link Here]
|
||||||
|
#>
|
||||||
|
|
||||||
|
function Sync-CISExcelAndCsvData {
|
||||||
|
[CmdletBinding()]
|
||||||
|
param (
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$ExcelPath,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$WorksheetName,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$CsvPath,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $false)]
|
||||||
|
[switch]$SkipUpdate
|
||||||
|
)
|
||||||
|
|
||||||
|
process {
|
||||||
|
# Merge Excel and CSV data
|
||||||
|
$mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -CsvPath $CsvPath
|
||||||
|
|
||||||
|
# Output the merged data if the user chooses to skip the update
|
||||||
|
if ($SkipUpdate) {
|
||||||
|
return $mergedData
|
||||||
|
} else {
|
||||||
|
# Update the Excel worksheet with the merged data
|
||||||
|
Update-CISExcelWorksheet -ExcelPath $ExcelPath -WorksheetName $WorksheetName -Data $mergedData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,62 +0,0 @@
|
|||||||
function Update-CISExcelWorksheet {
|
|
||||||
[CmdletBinding()]
|
|
||||||
param (
|
|
||||||
[Parameter(Mandatory = $true)]
|
|
||||||
[string]$ExcelPath,
|
|
||||||
|
|
||||||
[Parameter(Mandatory = $true)]
|
|
||||||
[string]$WorksheetName,
|
|
||||||
|
|
||||||
[Parameter(Mandatory = $true)]
|
|
||||||
[psobject[]]$Data,
|
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
|
||||||
[int]$StartingRowIndex = 2 # Default starting row index, assuming row 1 has headers
|
|
||||||
)
|
|
||||||
|
|
||||||
process {
|
|
||||||
# Load the existing Excel sheet
|
|
||||||
$excelPackage = Open-ExcelPackage -Path $ExcelPath
|
|
||||||
$worksheet = $excelPackage.Workbook.Worksheets[$WorksheetName]
|
|
||||||
|
|
||||||
if (-not $worksheet) {
|
|
||||||
throw "Worksheet '$WorksheetName' not found in '$ExcelPath'"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function to update cells in the worksheet
|
|
||||||
function Update-WorksheetCells {
|
|
||||||
param (
|
|
||||||
$Worksheet,
|
|
||||||
$Data,
|
|
||||||
$StartingRowIndex
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check and set headers
|
|
||||||
$firstItem = $Data[0]
|
|
||||||
$colIndex = 1
|
|
||||||
foreach ($property in $firstItem.PSObject.Properties) {
|
|
||||||
if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) {
|
|
||||||
$Worksheet.Cells[1, $colIndex].Value = $property.Name
|
|
||||||
}
|
|
||||||
$colIndex++
|
|
||||||
}
|
|
||||||
|
|
||||||
# Iterate over each row in the data and update cells
|
|
||||||
$rowIndex = $StartingRowIndex
|
|
||||||
foreach ($item in $Data) {
|
|
||||||
$colIndex = 1
|
|
||||||
foreach ($property in $item.PSObject.Properties) {
|
|
||||||
$Worksheet.Cells[$rowIndex, $colIndex].Value = $property.Value
|
|
||||||
$colIndex++
|
|
||||||
}
|
|
||||||
$rowIndex++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Update the worksheet with the provided data
|
|
||||||
Update-WorksheetCells -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex
|
|
||||||
|
|
||||||
# Save and close the Excel package
|
|
||||||
Close-ExcelPackage $excelPackage
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user