Skip to content

Instantly share code, notes, and snippets.

@mtmtcode
Created July 2, 2012 16:27
Show Gist options
  • Save mtmtcode/3034098 to your computer and use it in GitHub Desktop.
Save mtmtcode/3034098 to your computer and use it in GitHub Desktop.
OpenOffice selection to markdown table
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.table.XCellRange);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.beans.XIntrospection);
importClass(Packages.com.sun.star.frame.XModel);
importClass(Packages.com.sun.star.sheet.XCellRangeAddressable);
importClass(Packages.com.sun.star.sheet.XSpreadsheetView);
importClass(Packages.com.sun.star.datatransfer.clipboard.XClipboard);
importClass(Packages.com.sun.star.datatransfer.DataFlavor);
var oDoc = XSCRIPTCONTEXT.getDocument();
var xModel = qi(XModel, oDoc);
// アクティブシートを取得
var oController = xModel.getCurrentController();
var oSheet = qi(XSpreadsheetView, oController).getActiveSheet();
var xCellRange = qi(XCellRange, oSheet); // アクティブシート全体のCellRange
// 選択範囲取得
var xSelection = xModel.getCurrentSelection();
var array = qi(XCellRangeAddressable, xSelection).getRangeAddress();
var sCol = array['StartColumn'];
var sRow = array['StartRow'];
var eCol = array['EndColumn'];
var eRow = array['EndRow'];
// 選択範囲をMarkdown形式の表に変換
var str = "";
for (var i = sRow; i <= eRow; i++) {
var cols = new Array(eCol - sCol + 1);
for (var j = sCol; j <= eCol; j++) {
var cell = xCellRange.getCellByPosition(j, i);
var xTextRange = qi(XTextRange, cell);
cols[j - sCol] = xTextRange.getString();
}
str += toMarkdownRow(cols);
if (i == sRow) {
str += createBorderRow(cols.length);
}
}
toClipBoard(str);
/**
* ヘッダ行と内容を区切る行を生成する
*/
function createBorderRow(length) {
var str = "";
for (var i = 0; i < length; i++) {
str += "|----";
}
str += "\n";
return str;
}
/**
* Stringの配列をMarkdown書式の表1行文に変換する
*/
function toMarkdownRow(cols) {
var str = "";
for (var i = 0; i < cols.length; i++) {
str += ("|" + cols[i]);
}
str += "\n";
return str;
}
/**
* UnoRuntime.queryInterfaceのラッパー(メソッド名が長いので)
*/
function qi(interface, obj) {
return UnoRuntime.queryInterface(interface, obj);
}
/**
* MRIのインスペクタのラッパー(デバッグ用)
*/
function inspect(obj) {
var xContext = XSCRIPTCONTEXT.getComponentContext();
var xMCF = xContext.getServiceManager();
var oMRI = xMCF.createInstanceWithContext("mytools.Mri", xContext);
var xIntrospection = qi(XIntrospection, oMRI);
xIntrospection.inspect(obj);
}
/**
* 文字列をクリップボードにコピー
*/
function toClipBoard(str) {
var xContext = XSCRIPTCONTEXT.getComponentContext();
var xServiceManager = xContext.getServiceManager();
var oClip = xServiceManager.createInstanceWithContext("com.sun.star.datatransfer.clipboard.SystemClipboard", xContext);
var xClip = qi(XClipboard, oClip);
// クリップボードにコピーするために必要なXTransferableの実装オブジェクト
var xTr = new com.sun.star.datatransfer.XTransferable({
string: str,
getTransferData : function(flavor){
return this.string;
},
getTransferDataFlavors : function() {
var flavor = new DataFlavor();
flavor.MimeType = "text/plain;charset=utf-16";
flavor.HumanPresentableName = "Unicode-Text";
return new Array(flavor);
}
});
// クリップボードにコピー
xClip.setContents(xTr, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment