Format Chart DataLabel in Powershell generated Excel File - excel

Try to format datalabel font size with a powershell generated barchart, but does not work
Read the "whole" API for Chart.SeriesCollection for VBA and .NET. But it does not help. Is it a bug or have I a brain bug? Anyone who can help?
https://learn.microsoft.com/de-de/office/vba/api/excel.chart.seriescollection
My try (with different iterations about this)
$chart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange2.Font.Size = 18
Powershell Error Message: The property 'Size' cannot be found on this object. Verify that the property exists and can be set.
The whole short powershell script:
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $excel.Workbooks.Add()
$sheet = $excel.Worksheets.Item(1)
$sheet.Activate() | Out-NULL
$sheet.Cells.Item(1,1).Value2 = "City"
$sheet.Cells.Item(1,2).Value2 = "Citizens"
$sheet.Cells.Item(2,1) = "Offenbach"
$sheet.Cells.Item(2,2) = 111020
$sheet.Cells.Item(3,1) = "Heusenstamm"
$sheet.Cells.Item(3,2) = 18200
$sheet.Cells.Item(4,1) = "Rembruecken"
$sheet.Cells.Item(4,2) = 1850
$range = "A1:B4"
$chartSelect = $sheet.range($range)
$ch = $sheet.shapes.addChart().chart
$ch.chartType = 51
$ch.ApplyDataLabels(2)
$ch.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange2.Font.Size = 18
$ch.setSourceData($chartSelect)

Try this ?
1..3| %{$ch.SeriesCollection(1).DataLabels($_).Font.Size = 18}

There are a couple syntaxes that ought to work.
The old one which you need to select 'Show hidden members' in the VBIDE's Object Browser to see (as if it's been deprecated, but it works fine):
ActiveChart.SeriesCollection(2).DataLabels.Font.Size = 18
The new and improved and way more complex one:
ActiveChart.SeriesCollection(2).DataLabels.Format.TextFrame2.TextRange.Font.Size = 18
I'll let you convert from VBA to PowerShell.

Related

Using Powershell to convert text to number in Excel

I have an excel file that has three columns that are set to Number. However, when I open the file I have the this :
I found a helpful link here: stackoverflow link
I have tried this method but I am getting the following error. Is there something I am doing wrong:
$wb = 'C:\Users\user1\Documents\Working Folder\239\239_uploadFile.xlsx'
$excel = new-object -ComObject excel.application
$excel.visible = $false
$excel.DisplayAlerts = $false
$wb = $excel.workbooks.open($wb)
$ws1 = $wb.worksheets.item(3)
$ws1.columns.item(1).numberformat = 0
$ws1.Columns.item(14).Value = $ws1.Columns.item(14).Value
$wb.Save()
$wb.close()
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel`
[
In recent versions of Excel (as of at least Excel 2019), the .Value property is now a parameterized property and therefore requires an argument in order to be used.
A simple workaround is to use the .Value2 property instead - shown below.
However, .Value2 differs from .Value in that "it doesn’t use the Currency and Date data types" - if those types are required, use .Value(10) (sic) instead (10 is the value of the xlRangeValueDefault constant).
While using the .Item() method explicitly used to be required in earlier PowerShell (Core) versions, this no longer the case as of (at least) PowerShell (Core) 7.2.1.
Therefore, try the following:
& {
$wb = 'C:\Users\user1\Documents\Working Folder\239\239_uploadFile.xlsx'
$excel = new-object -ComObject excel.application
$wb = $excel.workbooks.open($wb)
$ws1 = $wb.worksheets(3)
$ws1.columns(1).numberformat = 0
# Note the use of .Value2, not .Value
$ws1.Columns(14).Value2 = $ws1.Columns(14).Value2
$wb.Save()
$wb.close()
$excel.Quit()
}
Note the use of & { ... }, i.e. the execution of the Excel-related code in a child scope. This makes the calls to and Remove-Variable excel unnecessary - see this answer for more information.

How to export a DataTable into an Excel file via Powershell?

I was searching for a simple and fast option to export an existing DataTable-object via Powershell into an Excel file.
At the end I came up with this code. I hope it helps others with same challenge:
# get XML-schema and XML-data from the table:
$schema = [System.IO.StringWriter]::new()
$myTable.WriteXmlSchema($schema)
$data = [System.IO.StringWriter]::new()
$myTable.WriteXml($data)
# start Excel and prepare some objects:
$xls = New-Object -Comobject Excel.Application
$xls.DisplayAlerts = $false
$xls.Visible = $false
$book = $xls.Workbooks.Add()
$sheet = $book.Worksheets[1]
$range = $sheet.Range("A1")
# import the data and save the file:
$map = $book.XmlMaps.Add($schema)
[void]$book.XmlImportXml($data, $map, $true, $range)
$book.SaveAs("c:\temp\test.xlsx", 51)
$xls.Quit()

How to zoom the excel window?

I am doing a powershell script that creates a new excel file and I would like that when the file opens, the zoom of the page is at 85%.
Someone has an idea of ​​how?
$NewExcel = New-Object -ComObject excel.application
$NewExcel.visible = $True
$NewExcel.WindowState = "xlMaximized"
$NewWorkbook = $NewExcel.Workbooks.Add()
$NewWorksheet= $NewWorkbook.Worksheets.Item(1)
This is my script to create the worksheet
I found !
Function Set-XlColumns {
$Window = $NewExcel.ActiveWindow
$Window.Zoom = 80
}
Set-XlColumns
Just use this function and call it later

Copying graph from Excel to Word with Powershell

I am trying to copy a graph from excel to word. The source file in excel has two sheets, 'data' & 'graph' on the 'graph' sheet there are 4 graphs, arranged 2x2.
The Word document is empty.
I am fairly new to powershell, but i want to automate a weekly report i have to make. This might seem a little steep, but i like a challenge. Plus, the report eats my time.
This is my code:
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.workbooks.open("H:\Reporting\ULTRAgraphTest.xlsx")
$ws = $wb.worksheets.item(1)
$charts = $ws.ChartObjects()
$chart = $charts.Item(1)
$a = $chart.copy
$wd = new-object -comobject Word.application
$wd.visible = $true
$path = "H:\Reporting\insertest.docx"
$doc = $wd.documents.open($path)
$wd.selection.Paste()
When i run this, the files get opened, but it pastes the clipboard content in the word document. It doesn't seem to copy the graph. What am i not seeing here?
Forgot the partheses
$a = $chart.copy
Should be
$a = $chart.copy()

Change from single target to last used cell in column

I am sure this is a common question. I am trying to just paste instead of to the target cell in the paste range, to go to the last used cell in the column + 1, so it just appends the target spread sheet. What do I need to change here?
I have tried some of the suggestions on here but keep getting errors, probably because I am using $lastRow = $ExcelWorkSheet.UsedRange.rows.count + 1 in the wrong place (haven't been using powershell that long).
If anyone can show easiest way to do this would be great. Thanks!
$Excel = New-Object -ComObject "Excel.Application"
$Excel.Visible=$true
$Workbook = $Excel.Workbooks.Open($pathxlsx)
$TempWorkbook = $Excel.Workbooks.Opentext($pathcsv)
$temp = $excel.Workbooks.Item(2)
$temp = $temp.Worksheets.Item(1)
$CopyRange = $temp.Range("C15")
$CopyRange.Copy()
$workbooksheet = $Workbook.Worksheets.Item(1)
$Workbooksheet.activate()
$PasteRange = $workbooksheet.Range("C10")
$workbooksheet.Paste($PasteRange)
EDIT - Ok, got it to do something different, but still not what I want.
$Excel = New-Object -ComObject "Excel.Application"
$Excel.Visible=$true
$Workbook = $Excel.Workbooks.Open($pathxlsx)
$TempWorkbook = $Excel.Workbooks.Opentext($pathcsv)
$temp = $excel.Workbooks.Item(2)
$temp = $temp.Worksheets.Item(1)
$CopyRange = $temp.Range("C15")
$CopyRange.Copy()
$workbooksheet = $Workbook.Worksheets.Item(1)
$ExcelWorkSheet.UsedRange.rows.count
$lastRow = $workbooksheet.UsedRange.rows.count + 1
$Workbooksheet.activate()
$PasteRange = $workbooksheet.Range("C" + $lastrow)
$workbooksheet.Paste($PasteRange)
I ended up just creating an excel template with a power query for this issue. This plus a formula to transpose the data allowed for a clean copy and paste with the data I needed in the form i needed it.

Resources