Why can't I create this chart in excel (using powershell) - excel

Trying to add a chart in excel using powershell. I used the following link for guidance:
http://theolddogscriptingblog.wordpress.com/2010/07/03/how-do-i-change-the-size-or-position-of-my-chart-with-powershell/
Here is the code:
# <---- Start Code --------------------------------------->
$xl = New-Object -comobject Excel.Application
# Show Excel
$xl.visible = $true
$xl.DisplayAlerts = $False
# Open a workbook
$wb = $xl.workbooks.add()
#Create Worksheets
$ws = $wb.Worksheets.Item(1) # Opens Excel and 3 empty Worksheets
1..8 | % { $ws.Cells.Item(1,$_) = $_ } # adds some data
1..8 | % { $ws.Cells.Item(2,$_) = 9-$_ } # adds some data
$range = $ws.range("a${xrow}:h$yrow") # sets the Data range we want to chart
# create and assign the chart to a variable
#$ch = $xl.charts.add() # This will open a new sheet
$ch = $ws.shapes.addChart().chart # This will put the Chart in the selected WorkSheet
$ch.chartType = 58
$ch.setSourceData($range)
$RngToCover = $ws.Range("D5:J19") # This is where we want the chart
$ChtOb = $ch.Parent # This selects the current Chart
$ChtOb.Top = $RngToCover.Top # This moves it up to row 5
$ChtOb.Left = $RngToCover.Left # and to column D
$ChtOb.Height = $RngToCover.Height # resize This sets the height of your chart to Rows 5 - 19
$ChtOb.Width = $RngToCover.Width # resize This sets the width to Columns D - J
<------------- End Code -------------------------------------->
This works on one machine I have which uses Excel 2010, but on another machine using Excel 2003 it fails with the following message:
"Method invocation failed because [System.__ComObject] doesn't contain a method named 'addchart'."
Is this a limitation of the version of Excel I'm using? Using powershell, how can I add a chart within a worksheet in Excel 2003?

According to the documentation the method was added with Excel 2007, so it's not available in Excel 2003. Try recording chart creation as a macro in Excel 2003 and translate the result into PowerShell. You'll probably get something like this:
$chart = $xl.Charts.Add
$chart.ChartType = ...
$chart.SetSourceData $xl.Sheets(1).Range(...), ...
$chart.Location ...

Related

PowerShell: Format second sheet within xlsx file

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()

Merge multiple excel files containing multiple sheets using Powershell

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.

Powershell - Doing a SaveAs with an Excel file I receive a Cannot access read-only document error

Good Morning,
I am new to Power Shell and I cobbled together a script to open, copy , and paste data from one excel sheet to another on a network drive. I can run it once and it works , but if I try to run it again I get the "Cannot access read-only document". I assume I am not opening and closing the excel application and workbooks properly. Any advice is appreciated.
#Start Excel
$excel = New-Object -ComObject Excel.Application
#$excel.Visible = $true
#$excel.DisplayAlerts = $false
$wb1= $excel.workbooks.open($file1)
$wb2 = $excel.workbooks.open($file2)
$ws1 = $wb1.WorkSheets.item(1)
$ws1exemptions = $wb1.WorkSheets.item(4)
$ws1totals = $wb1.WorkSheets.item(3)
$ws2 = $wb2.Worksheets.item(1)
$ws1.activate()
#Get number of rows to delete on Sheet1
$mydelrowcount = $ws1.UsedRange.rows.count
#Assign variable to make range A2:H(number of rows) $range = $mysheet.range("A2:H" + $myrowcount)
$mydelrange = "A2:H" + $mydelrowcount
#Select the rows
$deleteme = $ws1.range("A2:H" + $mydelrange)
#Delete rows fromm $file1!Sheet1
$deleteme.clear()
#Go to Sheet 1 of APOTHER (file2)
$ws2.activate()
#Get number of rows to copy from Sheet1
$mynewrowcount = $ws2.UsedRange.rows.count
#Assign variable to make range A2:H(number of rows)
$mynewrange = "A2:H" + $mynewrowcount
#Select the rows
$selectme = $ws2.range("A2:H" + $mynewrowcount)
#Copy the rows
$selectme.Copy()
#Go to Sheet1 of TestFiletoSend (file1)
$ws1.activate()
#Select the same number of rows in Sheet1 (variable in $mynewrange)
$ws1.Range($mynewrange).Select()
#Paste the rows into A2:H(number of rows)
$ws1.Paste()
$ws1.Cells.Range("A1").Select()
$ws1totals.activate()
$wb1.SaveAs($file1)#
$wb1.Close()
$wb2.Close()
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
# no $ needed on variable name in Remove-Variable call
Remove-Variable excel

Powershell Excel Automating adding worksheet

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

Powershell Excel Chart - obtain data series from existing chart

I have a Powershell script that copies a worksheet (with a custom dual axis chart) from one workbook to another workbook and then populates the new copy with data. That portion of the script works fine but I am trying to change the data series in the existing chart and I do not know the fields to change the data series.
I can change the chart title and the legend labels of the existing chart with no issues. I have tried the $ChartTemplate.SeriesCollection(1).Values field and the $ChartTemplate.SeriesCollection(1).XValues with and with out the $ChartTemplate.SeriesCollection().NewSeries.Invoke() command and I have had no success.
Does anyone know the Powershell syntax to edit an existing data series of a custom dual axis line chart (=SERIES(Template!$G$1,Template!$A$2:$A$112,Template!$G$2:$G$112,4) ?
The following is my Powershell code which I have obtained from googling:
$file1 = $global:ChartTemplateXlsx # source's fullpath
$file2 = $Path # destination's fullpath
$xl = new-object -c excel.application
$xl.Visible = $False # dont display the spreadsheet
$xl.displayAlerts = $false # don't prompt the user
$wb1 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
$wb = $xl.workbooks.open($file2) # open target workbook/worksheet
$sh1_wb = $wb.sheets.item(1) # 1st sheet in destination workbook
$sheetToCopy = $wb1.sheets.item('Template') # source sheet to copy
$sheetToCopy.copy($sh1_wb) # copy source sheet to destination workbook
$ws = $wb.ActiveSheet # set the worksheet
$ChartTemplate = $ws.chartobjects(1).chart # obtain the existing chart
$ChartTemplate.HasTitle = $true # turn on chart title
$ChartTemplate.ChartTitle.Text = "Test Chart" # set a new chart title
$ChartTemplate.SeriesCollection(1).Name = "=""Test01"""
$ChartTemplate.SeriesCollection(2).Name = "=""Test02"""
$ChartTemplate.SeriesCollection(3).Name = "=""Test03"""
$ChartTemplate.SeriesCollection(4).Name = "=""Test04"""
$wb1.close($false) # close source workbook w/o saving
$wb.close($true) # close and save destination workbook
$xl.quit()
spps -n excel
By the way, I am using a separate workbook with the chart so that users can create their own chart templates and then I will populate it with data.
The property that you're looking for is either the Formula or FormulaLocal property. They appear to be duplicates of each other. If updating one doesn't work, try the other, or just set both.
$ChartTemplate.SeriesCollection(1).Formula = '=SERIES(Template!$G$1,Template!$A$2:$A$112,Template!$G$2:$G$112,4)'
$ChartTemplate.SeriesCollection(1).FormulaLocal = '=SERIES(Template!$G$1,Template!$A$2:$A$112,Template!$G$2:$G$112,4)'

Resources