Powershell to create line chart from EXCEL

后端 未结 3 1837
清歌不尽
清歌不尽 2021-01-23 10:23

My powershell script inputs data into EXCEL worksheet

\"enter

and I am trying to c

相关标签:
3条回答
  • 2021-01-23 10:54

    Fancy stuff to avoid seeing unnecessary red lines (Exception setting "ChartType": "Unspecified error) all over:

    $chart.chartType = 4
    

    Detailed list of chart types and more information available here: http://learn-powershell.net/2012/12/24/powershell-and-excel-adding-a-chart-and-header-filter-to-a-report/

    0 讨论(0)
  • 2021-01-23 11:01

    Based on my experience with VBA, it appears that the first thing to try is changing your chart type. You have $chart.chartType=$xlChart::xlBarClustered. Based on the similar VBA commands, I would try changing this to $chart.chartType=$xlChart::xlLine. That should make a big difference, and let you see what else needs to be tweaked.

    0 讨论(0)
  • 2021-01-23 11:07

    Degustaf is 100% correct, his is the correct answer, but you have a lot of extra stuff in there that isn't needed. I can replicate your desired outcome including populating the spreadsheet with your test data, and do it in fewer lines than what you have. Here, check this out, you may come away with a few pointers for your future endeavors.

    #Test Data
    $Data=("8/15/2014",3091),("8/14/2014",240),("8/13/2014",519),("8/12/2014",622),("8/11/2014",2132),("8/10/2014",1255),("8/9/2014",3240)|ForEach{[PSCustomObject][Ordered]@{'Date_to_Display'=$_[0];'Number_of_Computers'=$_[1]}}
    
    $xlConditionValues=[Microsoft.Office.Interop.Excel.XLConditionValueTypes]
    $xlTheme=[Microsoft.Office.Interop.Excel.XLThemeColor]
    $xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
    $xlIconSet=[Microsoft.Office.Interop.Excel.XLIconSet]
    $xlDirection=[Microsoft.Office.Interop.Excel.XLDirection]
    
    $xl = new-object -ComObject Excel.Application
    $wb = $xl.workbooks.add()
    $ws = $wb.activesheet
    $xl.Visible = $true
    
    #Populate test data onto worksheet
    $Data |ConvertTo-CSV -NoTypeInformation -Delimiter "`t"| c:\windows\system32\clip.exe
    $ws.Range("A1").Select | Out-Null
    $ws.paste()
    $ws.UsedRange.Columns.item(1).numberformat = "ffffdd, mmm dd, yyyy"
    $ws.UsedRange.Columns.AutoFit() |Out-Null
    
    #Create Chart
    $chart=$ws.Shapes.AddChart().Chart
    $chart.chartType=$xlChart::xlLine
    
    #modify the chart title
    $chart.ChartTitle.Text = "Number of Computers"
    
    $ws.shapes.item("Chart 1").top=40
    

    If you work with Powershell and Excel much you'll probably find the line up there $Data|ConvertTo-CSV... extremely useful.

    0 讨论(0)
提交回复
热议问题