やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

LibreOffice CalcのBASICマクロを端末でバックグラウンド実行する`soffice`コマンド

 BASICなら簡単に外部実行できる。

成果物

コード

soffice macro:///Standard.Module1.Main
Sub Main
    MsgBox("マクロのテストです。")
End Sub

BASICファイル作成

 BASICのコードは以下の手順で作成した。

  1. LibreOffice Calc を起動する
  2. メニュー→マクロマクロの編集
  3. マイマクロ&ダイアログStandardModule1
  4. 画面右のテキストエディタにコードを書く
  5. 保存する(Ctrl+Sキー押下)

BASICファイルパス確認

 問題はこの手順で作成したBASICファイルのパスがどこなのか不明なこと。以下のようにパスを確認しようとしたが、出力されない。

Sub Main
    If ThisComponent.HasLocation() Then
      Print "The Document URL is " & ThisComponent.getURL()
    Else
      Print "The document has not yet been stored"
    End If

    MsgBox(StarDesktop.CurrentComponent.getURL & "\n" & ThisComponent.getURL & "\nマクロのテストです。\n")
End Sub

 自力で探したらあった。以下のパス。

~/.config/libreoffice/4/user/basic/Standard/Module1.xba

 ただ、なぜかXML形式の中にBASICコードが入っている。しかもそのせいでサニタイズまでされてる。これじゃ直接編集できないじゃないか……。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">Sub Main
    If ThisComponent.HasLocation() Then
      Print &quot;The Document URL is &quot; &amp; ThisComponent.getURL()
    Else
      Print &quot;The document has not yet been stored&quot;
    End If

    MsgBox(StarDesktop.CurrentComponent.getURL &amp; &quot;\n&quot; &amp; ThisComponent.getURL &amp; &quot;\nマクロのテストです。\n&quot;)
End Sub
</script:module>

所感

 Pythonもこれで実行できたら楽でよかったのに……。

 BASICなんて書きたくない。End Subとかクソすぎて発狂しそう。終わってる。でもMsgBoxは圧倒的にスマート。

対象環境

$ uname -a
Linux raspberrypi 4.19.97-v7l+ #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armv7l GNU/Linux