LoginSignup
5
7

More than 3 years have passed since last update.

Mac環境でExcelマクロを自動実行したい件

Posted at

前置き

  • お仕事のお話
  • 既存業務でExcelマクロを使って hoge.json を生成して、fugafugaするーみたいなつらみなものがあった
  • 既存業務を全自動する際に、Mac環境でExcelマクロを自動実行をとりあえずしたい状況

環境

  • Excel for Mac 16.16.12
  • macOS High Sierra(10.13.6)

プロジェクトの階層

./
├── 1_xxxxx
│   ├── 1_excel_project
│   │   ├── Main.xlsm ・・・どうにかしたいExcelファイル
└── tools
    └── exec_macro.scpt ・・・今回作成するスクリプト

対処法

  • AppleScriptというものを使う。
  • 作成したAppleScriptをshellから叩けるようにする。

AppleScriptを作成する

exec_macro.scpt
# 親ディレクトリのパスを取得
tell application "Finder"
    set ParentPath to parent of parent of (path to me) as text
end tell

# マクロの実行(Excelをactivateして、対象のファイル開いて、マクロ実行して、閉じる)
tell application "Microsoft Excel"
    activate
    open (ParentPath & "1_xxxxx:1_excel_project:Main.xlsm")
    run XLM macro "JikkousitaiMacroName"
    quit
end tell

マクロのセキュリティレベルを下げる

  • セキュリティ警告のダイアログの対処が難航したので、あんましたくないけど一旦対応
    • テキトーにExcelファイルを新規で開く
    • メニューで、Excel > 環境設定 > セキュリティとプライバシー を開く
    • マクロセキュリティで すべてのマクロを有効にする(~) を選択

shellで実行する

exec.sh
osascript tools/exec_macro.scpt

一応実行できた。
セキュリティレベルの部分だけどうにかしたい……。

参照

5
7
0

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