ひと工夫が必要。
成果物
sofficeには新規ファイル生成するコマンドが存在しない
ずっと昔から要求されてきたのに未だ実装されていないようだ。
 代わりに、--convert-toを用いて変換している。
function lo-create-spreadsheet () { # usage: # lo-create-spreadsheet test.xlsx # lo-create-spreadsheet test.xls # lo-create-spreadsheet test.ods baseName=${1%.*} ext=${1#*.} csvFile="${baseName}.csv" touch "$csvFile" loffice --convert-to "$ext" "$csvFile" rm "$csvFile" }
フィルタ一覧が知りたい
 --convert-toのヘルプを見てみる。出力ファイル形式を指定する方法のひとつにOutputFilterNameがある。この値が何か不明。その一覧を取得したい。
soffice --help
... --convert-to OutputFileExtension[:OutputFilterName] [--outdir output_dir] [--convert-images-to] Batch convert files (implies --headless). If --outdir isn't specified, then current working directory is used as output_dir. If --convert-images-to is given, its parameter is taken as the target MIME format for *all* images written to the output format. If --convert-to is used more than once, the last value of OutputFileExtension [:OutputFilterName] is effective. If --outdir is used more than once, only its last value is effective. For example: --convert-to pdf *.odt --convert-to epub *.doc --convert-to pdf:writer_pdf_Export --outdir /home/user *.doc --convert-to "html:XHTML Writer File:UTF8" *.doc --convert-to "txt:Text (encoded):UTF8" *.doc
改良
run.sh
Run() { THIS="$(realpath "${BASH_SOURCE:-0}")"; HERE="$(dirname "$THIS")"; PARENT="$(dirname "$HERE")"; THIS_NAME="$(basename "$THIS")"; APP_ROOT="$PARENT"; [ 0 -lt $# ] && { NAME=${1%.*}; EXT=${1#*.}; } || { NAME=$(date +%Y%m%d%H%M%S); EXT=ods; } PATH_IN="/tmp/$NAME.csv" touch "$PATH_IN" FILTER='calc8' [ 'fods' = "${EXT,,}" ] && FILTER='OpenDocument Spreadsheet Flat XML' [ 'tsv' = "${EXT,,}" ] && FILTER='Text - txt - csv (StarCalc)' soffice --convert-to "$EXT:$FILTER" --outdir "$(pwd)" "$PATH_IN" rm "$PATH_IN" } Run "$@"
- 引数がないなら%Y%m%d%H%M%S.odsファイルを出力する
- 引数にファイル名を受け取る。拡張子ods,fods,tsvに対応する
残念
一時ファイルの作成は避けられない。一時ファイルはどうせ空なのだからプロセス置換でもいいのでは? と思ったがダメだった。
soffice --convert-to ods <(echo '')
Error: source file could not be loaded
所感
 soffice --calc 'new.odc'とかで新規作成できるのが普通じゃない? それができないってダサすぎ。
対象環境
- Raspbierry pi 4 Model B
- Raspbian buster 10.0 2019-09-26 ※
- bash 5.0.3(1)-release 学習まとめ
- LibreOffice 6.1.5.2 ※
$ uname -a Linux raspberrypi 4.19.97-v7l+ #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armv7l GNU/Linux