Powershell to create line chart from EXCEL

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

My powershell script inputs data into EXCEL worksheet

\"enter

and I am trying to c

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    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.

提交回复
热议问题