サブロウ丸

Sabrou-mal サブロウ丸

主にプログラミングと数学

AppleScript で Excelでグラフ描写と保存を自動化

どーしても、Excelでグラフを大量に作成したいときには
AppleScriptで書かせましょう。

(サンプルコード)

tell application "Finder"
    --theListにファイルパスのリストを代入
    set theList to {"ファイルパス,ファイルパス,....."}
    --for文
    repeat with file_name in theList
        --Excelを開く
        tell application "Microsoft Excel"
            activate
            --file_nameをexcelで開く(前半は適当なパス)
            open "/Users/???/sample/" & file_name
            --セルD1の値をgraph_name1に代入
            set graph_name1 to string value of range "D1"
            set graph_name2 to string value of range "C1"
            --chartの保存名を取得(パス/ファイル名)
            set a_chartname to (path to desktop folder) & "Chart:" & graph_name1 & "_" & graph_name2 & ".png" as text
            --chartobjectを作成(大きさも)
            set chartObject to make new chart object at worksheet 1 ¬
                with properties {left position:10, top:10, width:1200, height:500}
            --グラフデータを設定
            set source data chart of chartObject source range ("A:B")
            tell chart of chartObject
                --グラフの種類を変更
                set chart type to line stacked
                --グラフタイトルを設定
                set has title to true
                tell its chart title
                    set caption to graph_name1
                end tell
            end tell
            --chartobjectを図として保存
            save as picture chartObject ¬
                picture type save as PNG file ¬
                file name a_chartname
            close active workbook saving no
        end tell
    end repeat
end tell