Tại sao tôi không thể tạo biểu đồ này trong excel (sử dụng powershell) (Why can't I create this chart in excel (using powershell))


問題描述

Tại sao tôi không thể tạo biểu đồ này trong excel (sử dụng powershell) (Why can't I create this chart in excel (using powershell))

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?


參考解法

方法 1:

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 ...

(by user2230237Ansgar Wiechers)

參考文件

  1. Why can't I create this chart in excel (using powershell) (CC BY‑SA 3.0/4.0)

#excel-2003 #powershell #excel






相關問題

使用 VBA 根據 B 列的值重置 A 列中的值 (Reset values in column A based on the value of column B using VBA)

Пераўтварэнне формулы ячэйкі ў тэкст з дапамогай excel vba (Converting a cell's formula to text using excel vba)

刪除行並維護輸入範圍 (Delete Rows & Maintain Input Range)

從 VBA 自動排序中排除文本值 (Exclude Text Value From VBA Autosort)

Tại sao tôi không thể tạo biểu đồ này trong excel (sử dụng powershell) (Why can't I create this chart in excel (using powershell))

如何使用 VBA 從 Excel 中的公式中獲取單元格值? (How do I get the cell value from a formula in Excel using VBA?)

讓 Excel 2003 在 Word 文檔中進行查找並返回出現次數 (Have Excel 2003 do a Find in a Word document and return the number of occurences)

驗證下拉條件 (Validation Drop down on a condition)

在位於兩個不同工作表的兩個範圍內添加單元格 (Adding cells in two ranges which are located at two different sheets)

如何在excel中獲得所需的輸出? (How to get the required output in excel?)

從復雜查詢中獲取數據到 Excel (Getting data from a complex query to excel)

使用文本格式的用戶定義輸入從 excel 中檢索數據 (Retrieving data from excel with user defined input that is in text format)







留言討論