A. FilterNames

ファイルを開いたり保存したりする時に直接フィルター名を指定して開くことができます。自動的に選択させると期待した形式として認識されないことがあるため決まったファイル形式として認識させたい時には指定します。

フィルターは拡張機能から追加することもできるため利用できるフィルターは環境によって違います。次のようなマクロで自分の環境で利用できるフィルターを調べることができます。

#!
# -*- coding: utf-8 -*-

def flags2str(flags):
    i = u'○' if flags & 1 else u'×'
    e = u'○' if flags & 2 else u'×'
    return i, e


def writeFilterNames():
    ctx = XSCRIPTCONTEXT.getComponentContext()
    desktop = XSCRIPTCONTEXT.getDesktop()
    
    ff = ctx.getServiceManager().createInstanceWithContext(
                         "com.sun.star.document.FilterFactory", ctx)
    names = ff.getElementNames()
    
    docTypes = {"com.sun.star.text.TextDocument": [], 
                "com.sun.star.sheet.SpreadsheetDocument": [], 
                "com.sun.star.drawing.DrawingDocument": [], 
                "com.sun.star.text.WebDocument": [], 
                "com.sun.star.presentation.PresentationDocument": [], 
                "com.sun.star.formula.FormulaProperties": [], 
                "com.sun.star.text.GlobalDocument": [], 
                "com.sun.star.sdb.OfficeDatabaseDocument": [], 
                "com.sun.star.chart2.ChartDocument": []}
    ids = {"UIName": 0, "Name": 1, "Type": 2, 
           "FilterService": 5, "UIComponent": 6}
    
    f2s = flags2str
    for name in names:
        docType = ""
        d = ["", "", "", "", "", "", "", ""]
        ps = ff.getByName(name)
        for p in ps:
            n = p.Name
            if n == "DocumentService":
                docType = p.Value#docServices.add(p.Value)
            pos = ids.get(n, None)
            if pos is None:
                if n == "Flags":
                    d[3], d[4] = f2s(p.Value)
                elif n == "UserData":
                    d[7] = "".join(p.Value)
            else:
                d[pos] = p.Value
        if docType is not "":
            docTypes[docType].append(tuple(d))

    # write in new document
    doc = desktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, ())
    
    doc.disableSetModified()
    doc.lockControllers()
    sheets = doc.getSheets()
    n = sheets.getCount()
    
    h = ("UIName", "Name", "Type", "Import", "Export", "FilterService", "UIComponent", "UserData")
    for k, v in docTypes.iteritems():
        v.insert(0, h)
        sheetName = k[k.rfind(".") + 1:].replace("Document", "").replace("Properties", "")
        sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
        sheets.insertByName(sheetName, sheet)
        r = sheet.getCellRangeByPosition(0, 0, 7, len(v) -1)
        r.setDataArray(tuple(v))
        r.getColumns().OptimalWidth = True
    
    for i in range(n):
        sheets.removeByName(sheets.getByIndex(0).getName())
    doc.unlockControllers()
    doc.enableSetModified()
    
    print("done.")

g_exportedScripts = writeFilterNames, 

実行すると Calc のシートに情報が書き出されます。各項目は次のようなものです。

  • UIName: ファイルを開く/保存するダイアログで表示される名称
  • Name: API などで FilterName の値として使用する名称
  • Type: ファイル形式
  • Import: インポートできるかどうか
  • Export: エクスポートできるかどうか
  • FilterService: 直接インスタンス化するときのサービス名
  • UIComponent: フィルターオプションダイアログのサービス名
  • UserData: コンフィグデータ