LoginSignup
1
0

More than 5 years have passed since last update.

LibreOffice Pythonマクロ 「InputBoxの表示」

Last updated at Posted at 2018-02-20

注意)
このページのマクロは、Libreofficeの「マクロの実行」で動作させます。

LibreOfficeでPythonマクロを実行するには、
UbuntuのLibreOffice(MS Office互換性あり)でPythonマクロを使用する方法(超初級者向け)
https://qiita.com/ty21ky/items/2c8b001363c8293198e2
を参照ください。

実行した環境

  Ubuntu Studio 17.10
  LibreOffice6.0 Writer,Calc

Linux Mint 18.2 + LibureOffice 5.4.3.2では、from InputMessageBox import inputboxでエラーになりました。???(2018/3/11)

確認のためにLibreOffice 6.0.2.1をMintにインストールしましたが、やっぱりエラーがでます。???
Ubuntu Stdioと Mintの違い?MintもUbuntuの派生だけど???

解決策

(Ubuntu Stdioでは確認しましたが動作します。多分Ubuntuも。(未確認)この作業は不要です。)
importできないので、考えついたのがまずfrom InputMessageBox import inputboxの前に「#」を追加してコメントにします。
このページの一番下にinputboxメソッドのコードがあるので、それを挿入しました。これで動きました。
何が異なるのか?不思議だ!OSの違いによるものか???

inputbox.py
from InputMessageBox import inputbox

def InputBoxWriter():
    InputData = inputbox("Please input some value", "Input", "Default value", 200, 200)
    XSCRIPTCONTEXT.getDocument().getText().getEnd().setString(InputData)

def InputBoxCalc():
    InputData = inputbox("Please input some value", "Input", "Default value", 200, 200)
    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.getSheets().getByName('Sheet1')
    A1 = sheet.getCellRangeByName('A1')
    A1.String = InputData

InputBox.png

Writer

InputBoxWriter.png

Calc

InputBoxCalc.png

このInputBoxのコードを発見しました。
https://wiki.openoffice.org/wiki/Python/Transfer_from_Basic_to_Python
Apache OpenOffice wiki
Transfer from Basic to Python

Input Box
Pythonには関数は用意されていません。独自のものを作る必要があります。次に例を示します。 (古いVerでは用意されてなかったのでしょうか)

inputbox
def inputbox(message, title="", default="", x=None, y=None):
    """ Shows dialog with input box.
        @param message message to show on the dialog
        @param title window title
        @param default default value
        @param x dialog positio in twips, pass y also
        @param y dialog position in twips, pass y also
        @return string if OK button pushed, otherwise zero length string
    """
    WIDTH = 600
    HORI_MARGIN = VERT_MARGIN = 8
    BUTTON_WIDTH = 100
    BUTTON_HEIGHT = 26
    HORI_SEP = VERT_SEP = 8
    LABEL_HEIGHT = BUTTON_HEIGHT * 2 + 5
    EDIT_HEIGHT = 24
    HEIGHT = VERT_MARGIN * 2 + LABEL_HEIGHT + VERT_SEP + EDIT_HEIGHT
    import uno
    from com.sun.star.awt.PosSize import POS, SIZE, POSSIZE
    from com.sun.star.awt.PushButtonType import OK, CANCEL
    from com.sun.star.util.MeasureUnit import TWIP
    ctx = uno.getComponentContext()
    def create(name):
        return ctx.getServiceManager().createInstanceWithContext(name, ctx)
    dialog = create("com.sun.star.awt.UnoControlDialog")
    dialog_model = create("com.sun.star.awt.UnoControlDialogModel")
    dialog.setModel(dialog_model)
    dialog.setVisible(False)
    dialog.setTitle(title)
    dialog.setPosSize(0, 0, WIDTH, HEIGHT, SIZE)
    def add(name, type, x_, y_, width_, height_, props):
        model = dialog_model.createInstance("com.sun.star.awt.UnoControl" + type + "Model")
        dialog_model.insertByName(name, model)
        control = dialog.getControl(name)
        control.setPosSize(x_, y_, width_, height_, POSSIZE)
        for key, value in props.items():
            setattr(model, key, value)
    label_width = WIDTH - BUTTON_WIDTH - HORI_SEP - HORI_MARGIN * 2
    add("label", "FixedText", HORI_MARGIN, VERT_MARGIN, label_width, LABEL_HEIGHT, 
        {"Label": str(message), "NoLabel": True})
    add("btn_ok", "Button", HORI_MARGIN + label_width + HORI_SEP, VERT_MARGIN, 
            BUTTON_WIDTH, BUTTON_HEIGHT, {"PushButtonType": OK, "DefaultButton": True})
    add("btn_cancel", "Button", HORI_MARGIN + label_width + HORI_SEP, VERT_MARGIN + BUTTON_HEIGHT + 5, 
            BUTTON_WIDTH, BUTTON_HEIGHT, {"PushButtonType": CANCEL})
    add("edit", "Edit", HORI_MARGIN, LABEL_HEIGHT + VERT_MARGIN + VERT_SEP, 
            WIDTH - HORI_MARGIN * 2, EDIT_HEIGHT, {"Text": str(default)})
    frame = create("com.sun.star.frame.Desktop").getCurrentFrame()
    window = frame.getContainerWindow() if frame else None
    dialog.createPeer(create("com.sun.star.awt.Toolkit"), window)
    if not x is None and not y is None:
        ps = dialog.convertSizeToPixel(uno.createUnoStruct("com.sun.star.awt.Size", x, y), TWIP)
        _x, _y = ps.Width, ps.Height
    elif window:
        ps = window.getPosSize()
        _x = ps.Width / 2 - WIDTH / 2
        _y = ps.Height / 2 - HEIGHT / 2
    dialog.setPosSize(_x, _y, 0, 0, POS)
    edit = dialog.getControl("edit")
    edit.setSelection(uno.createUnoStruct("com.sun.star.awt.Selection", 0, len(str(default))))
    edit.setFocus()
    ret = edit.getModel().Text if dialog.execute() else ""
    dialog.dispose()
    return ret

この関数は、次のように呼び出すことができます。

inputbox
inputbox("Please input some value", "Input", "Default value")
1
0
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
1
0