I am attempting to automate the process of adding a worksheet (with data) per clientname in excel for a monthly report type workbook
I thought it should be straight forward... but the method I am using isnt working.... it doesn't even get to the sheet making mode... can you help me figure out what I did wrong?
The following is the function I made
function Excelclientstatstemplate ($clients) {
$Exl = New-Object -ComObject "Excel.Application"
$Exl.Visible = $false
$Exl.DisplayAlerts = $false
$WB = $Exl.Workbooks.Open($excelmonthlysummary)
$clientws = $WB.worksheets | where {$_.name -like "*$clients*"}
#### Check if Clients worksheet exists, if no then make one with client name ###
$sheetcheck = if (($clientws)) {} Else {
$WS = $WB.worksheets.add
$WS.name = "$clients"
}
$sheetcheck
$WB.Save
# Enter stat labels
$clientws.cells.item(1,1) = "CPU Count"
$clientws.cells.item(2,1) = "RAM"
$clientws.cells.item(3,1) = "Reserved CPU"
$clientws.cells.item(4,1) = "Reserved RAM"
### Put in Values in the next column ###
$clientws.cells.item(1,2) = [int]($cstats.cpuAllocationGHz/2)
$clientws.cells.item(2,2) = [decimal]$cstats.memoryLimitGB
$clientws.cells.item(3,2) = [int]($cstats.rescpuAllocationGHz/2)
$clientws.cells.item(4,2) = [decimal]$cstats.resmemoryLimitGB
$WB.save
$Exl.quit()
Stop-Process -processname EXCEL
Start-Sleep -Seconds 1
Echo "$clients excel sheet in monthly summary is done.."
}
and then I tried to make a Foreach thing for it
$clientxlmonthlywrite = Foreach ($client in $clientlist){
$cstats = $Combinedstats | Where {$_.Group -eq "$client"}
Excelclientstatstemplate -clients $client
}
The entire Process of the function goes
Take client name
Open a particular excel workbook
Check if there are any sheets with client name
If there are NO sheets with client name, make one with client name
Fill The first column Cells with labels
Fill the second column cells with data (data works I already write CSVs withem)
Save and exit
The Foreach variable just does the function for each of Clients names from a clientlist (nothing wrong with clientlist)
Am I messing something up?
Thanks for the help.
You are not calling the .Add() method correctly. You are missing the parenthesis at the end of it. To fix it you should be able to simply modify the line to this:
$WS = $WB.worksheets.add()
Also, the cells have properties that you should refer to, so I would also modify the part that sets your cell values to something like this:
# Enter stat labels
$clientws.cells.item(1,1).value2 = "CPU Count"
$clientws.cells.item(2,1).value2 = "RAM"
$clientws.cells.item(3,1).value2 = "Reserved CPU"
$clientws.cells.item(4,1).value2 = "Reserved RAM"
### Put in Values in the next column ###
$clientws.cells.item(1,2).value2 = [int]($cstats.cpuAllocationGHz/2)
$clientws.cells.item(2,2).value2 = [decimal]$cstats.memoryLimitGB
$clientws.cells.item(3,2).value2 = [int]($cstats.rescpuAllocationGHz/2)
$clientws.cells.item(4,2).value2 = [decimal]$cstats.resmemoryLimitGB
I'm fairly sure that defining the type is pointless, since to Excel they're all strings until you set the cell's formatting settings to something else. I could be wrong, but that is the behavior that I have observed.
Now, for other critiques that you didn't ask for... Don't launch Excel, open the book, save the book, and close Excel for each client. Open Excel once at the beginning, open the book, make your updates for each client, and then save, and close.
Test to see if the client has a sheet, and add it if needed, then select the client's sheet afterwords. Right now there's nothing there to set $clientws if you have to add one for that client.
Adding a worksheet by default places it before the active worksheet. This was a poor choice in design in my opinion, but it is what it is. If it were me I'd add new sheets specifying the last worksheet in the workbook, which will add the new worksheet before the last one, making it the second to the last worksheet. Then I'd move the last worksheet up in front of the new one, effectively adding the new worksheet as the last one listed. Is it possible to add the new worksheet as the last one when you make it? Yes, but it's was too complicated for my taste. See here if you are interested in doing that.
When testing for an existing client worksheet to make one if it is missing, do that, don't tell it to test for something, and do nothing, and put everything you want in an Else statement. That just complicates things. All that said, here's some of those suggestions put into practice:
function Excelclientstatstemplate ($clients) {
#### Check if Clients worksheet exists, if no then make one with client name ###
if (($clients -notin $($WB.worksheets).Name)){
#Find the current last sheet
$LastSheet = $WB.Worksheets|Select -Last 1
#Make a new sheet before the current last sheet so it's near the end
$WS = $WB.worksheets.add($LastSheet)
#Name it
$WS.name = "$clients"
#Move the last sheet up one spot, making the new sheet the new effective last sheet
$LastSheet.Move($WS)
}
#Find the current client sheet regardless of if it existed before or not
$clientws = $WB.worksheets | where {$_.name -like "*$clients*"}
# Enter stat labels
$clientws.cells.item(1,1).value2 = "CPU Count"
$clientws.cells.item(2,1).value2 = "RAM"
$clientws.cells.item(3,1).value2 = "Reserved CPU"
$clientws.cells.item(4,1).value2 = "Reserved RAM"
### Put in Values in the next column ###
$clientws.cells.item(1,2).value2 = [int]($cstats.cpuAllocationGHz/2)
$clientws.cells.item(2,2).value2 = [decimal]$cstats.memoryLimitGB
$clientws.cells.item(3,2).value2 = [int]($cstats.rescpuAllocationGHz/2)
$clientws.cells.item(4,2).value2 = [decimal]$cstats.resmemoryLimitGB
Start-Sleep -Seconds 1
Echo "$clients excel sheet in monthly summary is done.."
}
$Exl = New-Object -ComObject "Excel.Application"
$Exl.Visible = $false
$Exl.DisplayAlerts = $false
$WB = $Exl.Workbooks.Open($excelmonthlysummary)
$clientxlmonthlywrite = Foreach ($client in $clientlist){
$cstats = $Combinedstats | Where {$_.Group -eq "$client"}
Excelclientstatstemplate -clients $client
}
$WB.save
$Exl.quit()
Stop-Process -processname EXCEL
Related
PowerShell: Format second sheet within xlsx file
I am working with an xlsx file that has two sheets within it.
I am uploading data into these sheets and formatting it.
I am able to successfully format the first sheet but not the second sheet.
This is the code for how I format the first sheet:
# Format Data: Autofit Columns
$lgTime = "[{0:HH:mm:ss}]" -f (Get-Date)
Write-Host "$lgTime Autofitting Data Columns..."
$range2autofit = $worksheet.UsedRange
$rowCount = $range2autofit.Rows.Count
[void] $range2autofit.EntireColumn.Autofit()
$lgTime = "[{0:HH:mm:ss}]" -f (Get-Date)
write-host "$lgTime Creating Excel Table Format ..."
$tableStyle = "TableStyleMedium9"
$tableStyle = "TableStyleLight21"
$Worksheet.Columns.Item("A").NumberFormat = "MM/DD/YYYY"
$ListObject = $WorkBook.ActiveSheet.ListObjects.Add(1, $range2autofit, $null , 1, $null, $tableStyle)
I would like the same formatting to be applied to the second sheet within the file but am having trouble doing that. I have tried using that same code again but with small changes such as:
Workbook.Worksheet.Item(2).UsedRange
Workbook.Worksheet.Item("Sheet2Name").UsedRange
My thought process is that I should be able to use the same code but just access the second sheet in it, I think I am just not accessing the second sheet correctly. That could be completely wrong though.
Edit:
This is where I defined $workbook and added a sheet to the xlsx file which is followed by the renaming of each sheet
$dataFile = "FILE LOCATION.xlsx"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$workbook = $objExcel.Workbooks.Open($dataFile)
$worksheet = $workbook.Worksheets.Add()
$worksheetOne = $workbook.Worksheets.Item(1)
$worksheetOne.Name = "Sheet1Name"
$worksheetTwo = $workbook.Worksheets.Item(2)
$worksheetTwo.Name = "Sheet2Name"
From the code you added in your edit, it looks like you're getting tripped up when adding a sheet, then using the wrong index later? Worksheets.Add() creates a blank worksheet at index 2 by default, not at the end. For example:
# A good way to check and see what you're doing with your sheets:
$workbook.Worksheets | select Index,Name
Index Name
----- ----
1 Sheet1
2 Sheet2
# add a new sheet
$worksheet = $workbook.Worksheets.Add()
# check again
$workbook.Worksheets | select Index,Name
Index Name
----- ----
1 Sheet1
2 Sheet3 # whoops!
3 Sheet2
To add before/after a specific worksheet instead, you can specify like this:
# missing value is required for COM functions
$newSheet = $workbook.Worksheets.add(
[System.Reflection.Missing]::Value, ## before index n
$workbook.Worksheets.Item(2) ## after index n
)
Then just be careful when you select your sheets, and you should be good to go!
# Setting sheet properties by referring to variable
$ws1 = $workbook.Worksheets.Item(1)
$ws1.Name = "Sheet1Name"
$ws1.UsedRange.EntireColumn.AutoFit()
$ws2 = $workbook.Worksheets.Item(2)
$ws2.Name = "Sheet2Name"
$ws2.UsedRange.EntireColumn.AutoFit()
I am having multiple excel files having same no of sheets with same sheet names. The same sheets in all excel files having the same headers. So I want an idea how to do merging for all the matched sheets in multiple excel files and create a new excel file via scripting using Powershell.
Any sugguestion Helps.
Thanks.
If the number of columns is the same across the different Excel worksheets then you should be able to use the below code to merge the files together.
The code uses methods from the NamedRange interface from the Excel API.
Example Code: (Just remember to change the paths and file names to your environment)
# Create an instance of Excel
$Excel = New-Object -ComObject Excel.Application
# Find the files you want to process
$Files = Get-ChildItem -Path C:\Temp -Filter *.xlsx
# Create a target workbook and worksheet called 'Sheet1'
$TargetWorkbook = $Excel.Workbooks.add()
$TargetWorksheet = $TargetWorkbook.Sheets.Item("Sheet1")
# Loop through our Excel files
foreach($File in $Files) {
# Open the workbook and get the first sheet
$SourceWorkbook=$Excel.Workbooks.Open($File.FullName)
$SourceWorksheet=$SourceWorkbook.Sheets.Item(1)
# Calculate the end column letter
$EndColumn = [char]([int][char]'A' + $SourceWorksheet.UsedRange.Columns.Count - 1)
# Activate the source worksheet
$SourceWorksheet.activate()
# Get the total number of rows in the sheet
$SourceLastRow = $SourceWorksheet.UsedRange.Rows.Count + 1
# Calculate what our start row should be
# A1 for the first worksheet only to include the headers
$StartRow = (& { If ($TargetWorkSheet.UsedRange.rows.count -eq 1) { "A1" } Else { "A2" } } )
# Get the range of data and copy it to the clipboard
$SourceRange = $SourceWorksheet.Range("$($StartRow):$EndColumn$SourceLastRow")
$SourceRange.copy()
# Activate the target worksheet
$TargetWorksheet.activate()
# Get the total number of rows in the sheet
$TargetLastRow = $TargetWorkSheet.UsedRange.Rows.Count
if ($TargetWorkSheet.UsedRange.Rows.Count -ne 1) {
# If this isn't the first sheet we've processed, add one additional row
$TargetLastRow++
}
# Get the target range and paste the data
$TargetRange = $TargetWorksheet.Range("A$($TargetLastRow):$EndColumn$($SourceRange.Rows.Count)")
$TargetWorksheet.Paste($TargetRange)
# Disable showing alerts, otherwise a notification about
# large amounts of data on the clipboard will be shown
$Excel.DisplayAlerts = $false
# Close the source workbook
$SourceWorkbook.Close()
}
# Re-enable showing alerts
$Excel.DisplayAlerts = $true
# Save the workbook to the desired path
$TargetWorkbook.SaveAs("C:\Temp\Merged.xlsx")
# Quit Excel
$Excel.Quit()
If your sheets have different numbers of columns, you could still use the above code, however you'll need to make some changes to the $SourceRange, $TargetRange and $EndColumn variables to account for this.
This is a long winded one but I will try and shorten it.
I have a master spreadsheet exported from our MIS system each school term and it gives me all the classes and who is in them for a specific school year.
The only problem is that it comes as one massive sheet within one workbook.
It looks like this:
Redacted Master Sheet Image
My end goal is to have one master workbook with each class cut and pasted into a separate sheet, and then the name of that sheet is the 'class code' which is the {redacted} part at the top of each section.
This has been done manually until now but it takes hours.
Is there a way in powershell to do this? I need the data from Class List Report: ... to Males: X Females: X into their own sheet within the workbook and then name the sheet the class code. Unfortunately the classes are different lengths so I cant do it based on counting the number of cells.
Any help is greatly appreciated.
This will probably do what you need given the image you provided. Essentially it opens the Workbook gets the main sheet parses it based on the key phrase. Then creates new sheets with the data between the start of each key phrase named by the identifier within the key phrase.
$ExcelFile="SO61954371.xlsx"
$MainSheetName="Sheet1"
$splitOnLike="Class List Report*"
$newExcelFile="SO61954371AA.xlsx"
#create Excel Object
$xl = New-Object -ComObject Excel.Application
#open Workbook
$WorkBook = $xl.Workbooks.Open($ExcelFile)
#grab main sheet
$mainSheet=$WorkBook.sheets.Item("$MainSheetName")
$classStart=#()
#loop through used rows
for($i=1; $i -le $mainSheet.UsedRange.Rows.Count;$i++){
#get cell in row $i Column 1(A)
$cell=$mainSheet.Cells.Item($i,1)
#check if start of table
if($cell.Text -like "$splitOnLike")
{
#make object with class code and starting cell
$classStart+=[pscustomobject]#{
classCode=($cell.text.split(":")[1] -split "as")[0].trim()
Cell=$cell
}
}
}
#Loop though tables
for($c=0; $c -lt $classStart.Count;$c++){
#create new sheet after last sheet
$Nsheet=$WorkBook.Sheets.Add($($WorkBook.Worksheets|Select -Last 1))
#name sheet
$Nsheet.name=$classStart[$c].classCode
#get and copy from current class to start of next
if($c -ne ($classStart.Count-1)){
$range=$mainSheet.Range("A$($classStart[$c].Cell.Row):$([char](64 + $($mainSheet.UsedRange.Columns.count)))$($classStart[$c+1].Cell.Row-1)")
$range.Copy() | out-null
$nsheetrange=$Nsheet.Range("A1")
$Nsheet.paste($nsheetrange)
}
else{
#for last class get and copy from start of class to end of usable range
$range=$mainSheet.Range("A$($classStart[$c].Cell.Row):$([char](64 + $($mainSheet.UsedRange.Columns.count)))$($mainSheet.UsedRange.Rows.Count)")
$range.Copy() | out-null
$nsheetrange=$Nsheet.Range("A1")
$Nsheet.paste($nsheetrange)
}
}
#save the file
$workbook._SaveAs($newExcelFile)
#kill the Excel instance and cleanup
$xl.Quit()
Remove-Variable -Name xl
[gc]::collect()
[gc]::WaitForPendingFinalizers()
I'm working on a PS script to take a row of data from an Excel spreadsheet and populate that data in certain places in a Word document. To elaborate, we have a contract tracking MASTER worksheet that among other things contains data such as name of firm, address, services, contact name. Additionally, we have another TASK worksheet in the same workbook that tracks information such as project owner, project name, contract number, task agree number.
I'm writing a script that does the following:
Ask the user through a message box what kind of contract is being written ("Master", or "Task")
Opens the workbook with the appropriate worksheet opened ("Master" tab or "Task" tab)
Asks the user through a VB InputBox from which Excel row of data they want to use to populate the Word contract
Extracts that row of data from Excel
Outputs certain portions of that row of data to certain location in a Word document
Saves the Word document
Opens the Word document so the user can continue editing it
My question is this - using something like PSExcel, how do I extract that row of data out to variables that can be placed in a Word document. For reference, in case you're going to reply with a snippet of code, here are what the variables are defined as for the Excel portion my script:
$Filepath = "C:\temp\ContractScript\Subconsultant Information Spreadsheet.xlsx"
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($Filepath)
$Worksheet = $Workbook.sheets.item($AgreementType)
$Excel.Visible = $true
#Choosing which row of data
[int]$RowNumber = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the row of data from $AgreementType worksheet you wish to use", "Row")
Additionally, the first row of data in the excel worksheets are the column headings, in case it matters.
I've gotten this far so far:
import-module psexcel
$Consultant = new-object System.Collections.Arraylist
foreach ($data in (Import-XLSX -path $Filepath -Sheet $AgreementType -RowStart $RowNumber))
{
$Consultant.add($data)'
But I'm currently stuck because I can't figure out how to reference the data being added to $consultant.$data. Somehow I need to read in the column headings first so the $data variable can be defined in some way, so when I add the variable $consultant.Address in Word it finds it. Right now I think the variable name is going to end up "$Consultant.1402 S Broadway" which obviously won't work.
Thanks for any help. I'm fairly new to powershell scripting, so anything is much appreciated.
I have the same issue and searching online for solutions in a royal PITA.
I'd love to find a simple way to loop through all of the rows like you're doing.
$myData = Import-XLSX -Path "path to the file"
foreach ($row in $myData.Rows)
{
$row.ColumnName
}
But sadly something logical like that doesn't seem to work. I see examples online that use ForEach-Object and Where-Object which is cumbersome. So any good answers to the OP's question would be helpful for me too.
UPDATE:
Matthew, thanks for coming back and updating the OP with the solution you found. I appreciate it! That will help in the future.
For my current project, I went about this a different way since I ran into lack of good examples for Import-XLSX. It's just quick code to do a local task when needed, so it's not in a production environment. I changed var names, etc. to show an example:
$myDataField1 = New-Object Collections.Generic.List[String]
$myDataField2 = New-Object Collections.Generic.List[String]
# ...
$myDataField10 = New-Object Collections.Generic.List[String]
# PSExcel, the third party library, might want to install it first
Import-Module PSExcel
# Get spreadsheet, workbook, then sheet
try
{
$mySpreadsheet = New-Excel -Path "path to my spreadsheet file"
$myWorkbook = $mySpreadsheet | Get-Workbook
$myWorksheet = $myWorkbook | Get-Worksheet -Name Sheet1
}
catch { #whatever error handling code you want }
# calculate total number of records
$recordCount = $myWorksheet.Dimension.Rows
$itemCount = $recordCount - 1
# specify column positions
$r, $my1stColumn = 1, 1
$r, $my2ndColumn = 1, 2
# ...
$r, $my10thColumn = 1, 10
if ($recordCount -gt 1)
{
# loop through all rows and get data for each cell's value according to column
for ($i = 1; $i -le $recordCount - 1; $i++)
{
$myDataField1.Add($myWorksheet.Cells.Item($r + $i, $my1stColumn).text)
$myDataField2.Add($myWorksheet.Cells.Item($r + $i, $my2ndColumn).text)
# ...
$myDataField10.Add($myWorksheet.Cells.Item($r + $i, $my10thColumn).text)
}
}
#loop through all imported cell values
for ([int]$i = 0; $i -lt $itemCount; $i++)
{
# use the data
$myDataField1[$i]
$myDataField2[$i]
# ...
$myDataField10[$i]
}
I have a script that will create worksheets based on the number of files that it finds in a directory. From there it changes the name of the sheets to the file name. During that process, I am attempting to add two Column header values of "Hostname" and "IP Address" to every sheet. I can achieve this by activating each sheet individually but this becomes rather cumbersome as the amount of sheets goes past 20+ and thus I am trying to find a dynamic way of doing this regardless the amount of sheets that are present.
This is the code that I have to do everything up to the column header portion:
$WorksheetCount = (Get-ChildItem $Path\Info*.txt).count
$TabNames = Get-ChildItem $Path\Info*.txt
$NewTabNames = Foreach ($file IN $TabNames.Name){$file.Substring(0,$file.Length-4)}
$Break = 0
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$Workbook = $Excel.Workbooks.Add()
$null = $Excel.Worksheets.Add($MissingType, $Excel.Worksheets.Item($Excel.Worksheets.Count),
$WorksheetCount - $Excel.Worksheets.Count, $Excel.Worksheets.Item(1).Type)
1..$WorksheetCount
Start-Sleep -s 1
ForEach ($Name In $NewTabNames){
$Break++
$Excel.Worksheets.Item($Break).Name = $Name
}
I have attempted to insert my code as such:
ForEach ($Name In $NewTabNames){
$Break++
$Excel.Worksheets.Item($Break).Name = $Name
$cells=$Name.Cells
$cells.item(1,1)="Hostname"
$cells.item(1,2)="IP Address"
}
When I attempt to run the script, I get the following error..
You cannot call a method on a null-valued expression.
And then it proceeds to list each line of the code that I had put in. I thought that since I created a variable during the operation, that it was the issue:
$cells=$Name.Cells
I thought That perhaps if I moved it before the ForEach command that it would resolve it but I still receive the same issue. I have looked through various ways of trying to select ranges of sheets within excel via powershell but have not found anything helpful.
Would appreciate any assistance on this.
This is actually my first post in StackOverflow ever and I feel pretty excited to finally help out. I made some small modifications to your code and seems to work fine. I noticed some odd behavior when I removed the $null variable that was getting assigned because it seemed strange to me why it was being done, but after removing that assignment my outlook application open by itself automatically every time I ran the script. I found the site where you got the code from just to see if there were any changes to the original code.
I found this Microsoft documentation very helpful to figure this out.
This is what I modified
ForEach ($Name In $NewTabNames){
$Break++
$Excel.Worksheets($Break).Name = $Name
$Excel.Worksheets($Break).Cells(1,1).Font.Bold = $true
$Excel.Worksheets($Break).Cells(1,1) = "Hostname"
$Excel.Worksheets($Break).Cells(1,2).Font.Bold = $true
$Excel.Worksheets($Break).Cells(1,2) = "IP Address"
}