LoginSignup
4
9

More than 5 years have passed since last update.

powershellでexcel図形描画

Last updated at Posted at 2018-03-01

はじめに

vbaの記事はあるんだが、powershellから扱うのがなかなかないのでアップします。

サンプル

内容は、座標から任意点のshapeを作って色を設定して、そのあと、グループ化するということをやってます。

e.ps1
$exc = new-object -Com Excel.Application
$exc.visible = $true
$bk = $exc.WorkBooks.Add()
$sh = $bk.ActiveSheet
$sh.Cells.Item(2,2)='jj'

$xx=@((10,10),(300,10),
(300,40),(20,50))

# 0 auto
$p=$sh.Shapes.BuildFreeform(0, $xx[0][0],$xx[0][1])
for($i=1;$i -lt $xx.Length;$i++){
    # segmentLine(0),editingTypeAuto(0)
    $b=$p.AddNodes(0,0,$xx[$i][0],$xx[$i][1])
}
$b=$p.AddNodes(0,0,$xx[0][0],$xx[0][1])
$b=$p.ConvertToShape()
$b.Line.ForeColor.RGB = 0x0000FF #このデータは0xBBGGRRの順ね
$b.Fill.Visible=$false
$b.select($true) #最初に選択するときはtrue

$yy=@((20,30),(320,20),
(320,70),(50,60))

# 0 auto
$p=$sh.Shapes.BuildFreeform(0, $yy[0][0],$yy[0][1])
for($i=1;$i -lt $xx.Length;$i++){
    # segmentLine(0),editingTypeAuto(0)
    $b=$p.AddNodes(0,0,$yy[$i][0],$yy[$i][1])
}
$b=$p.AddNodes(0,0,$yy[0][0],$yy[0][1])
$b=$p.ConvertToShape()
$b.Line.ForeColor.RGB = 0x00FF00
$b.Fill.Visible=$false
$b.name
$b.select($false) #追加で選択するときはfalse
$exc.selection.group() #selectionはアプリのプロパティでした
#$exc.Quit()
#$exc = $null

わかったこと

まあ、だいたいは分かるんだが、vbaの場合withで書いてしまっているので、それをなんとかしないといけない。$p.ConvertToShape()での返り値がshapeなのでその色を設定すれば、そこは変えられる。

また、selectionというのがアプリケーションのプロパティというのもなかなかググってもなかったりする。

今、よくわからんのが、ShapeRengeやShapes(n)というようなアクセスをどんなふうにするべきなのか試行錯誤してたりする。

4
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9