目次

趣旨

VBAをOpenOffice.org Basicに移植する際の留意点というか、ルールをまとめる。

修正量を最小限にするための工夫を中心に書く。

OpenOffice.org Basicマクロリファレンス

http://itref.fc2web.com/openoffice/basic/index.html

現在表示中のシートオブジェクトを取得する

   Dim ash As Worksheet
   Set ash = ActiveSheet

を下記のように書く

   Dim ash As Object
   Set ash = ThisComponent.CurrentController.ActiveSheet

セルへの値

OpenOfficeは0始まりで、行と列番号が逆という、センスゼロな実装のため さらに、文字列の取得と数値の取得のメソッドが異なり、 valueメソッドでは数値しか取得できない。文字列を取得しようとすると0になるのだ。 文字列で取得したい場合はvalueメソッドではなくStringメソッドを使う。

なるべくVBA側もセルにアクセスするgetter,setterを下記のようにメソッドを用意して実装しておけば、OpenOffice用のメソッドに置き換えるだけで移植が可能になる。

VBAでのセル値取得メソッド

Function cel(sh As Worksheet, row, col) As String
   cel = sh.Cells(row, col).Value
End Function

OpenOffice側でのセル値取得メソッド 数値版

Function cel(sh As Worksheet, row, col) As String
   cel = sh.Cells(row, col).Value
End Function

OpenOffice側でのセル値取得メソッド 文字列版

Function getString(sh As Object, row, col) As String
   getString = sh.getCellByPosition(col -1, row-1).String
End Function

Debug.printのかわりにどーするか

Printメソッドを使うと、一回一回ダイアログが出てくるのでめんどくさい。

なので、下記の実装を行うことを自分のルールとする

Debug.Print代替コード

Debug.printのスコープってグローバル変数扱いでよいと思う

Mainメソッドから実行がスタートする使用っぽいのでそこに、Debug.printのいろいろをまとめておく。

'グローバル変数として宣言
Dim DebugPrint as String
 
Sub MAIN
	'初期化
	DebugPrint = ""
	
	'任意のメソッドを呼び出す。
	 GeneGene
	 
	 'デバック出力
	'ExportData "c:\OpenOfficeDebugPring.txt",DebugPrint
       p
END Sub

もともとあった、Debug.printをDebug_printに置き換えて移植する

Sub Debug_Print(buf as string)
	If DebugPrint <> "" then
		DebugPrint = 	DebugPrint & vbCrLf
	End if
	DebugPrint = 	DebugPrint & buf
End Sub 
'ファイルに文字列を出力します。
Sub ExportData (finename, s)
 Dim FA      As Object
 Dim O       As Object
 Dim OS      As Object
 
 FA = createUnoService("com.sun.star.ucb.SimpleFileAccess") 
 O  = createUnoService("com.sun.star.io.TextOutputStream")
 OS = FA.openFileWrite(finename)
 O.setOutputStream(OS) 
 O.writeString(s)
 O.closeOutput()
End Sub

デバック出力するからには、確認するために出力するわけで、単にファイルに吐き出すだけだと、 いちいちファイルを開く作業が発生するし、確認するためのコードは簡単に呼び出したい。

というわけでデバックに関してはp一文字のメソッドを用意することにする。

Sub p()
	 'デバック出力
	ExportData "c:\OpenOfficeDebugPring.txt",DebugPrint
	
	'//Shell参考
	'//http://wiki.services.openoffice.org/wiki/JA/Documentation/BASIC_Guide/Other_Functions_(Runtime_Library)
	Shell("C:\Program Files\sakura\sakura.exe", 1,"c:\OpenOfficeDebugPring.txt" )
End sub

Macの場合

コンソールで試行錯誤してみたところ次のコードでいけそうだ。

/Developer/Applications/Dashcode.app/Contents/MacOS% ./Dashcode      /Users/hayashikuniyuki/Documents/01TODO/ebook/OpenOfficeDebugPring.txt 

なので次のようなコードにする。

Sub p()
	 'デバック出力
	ExportData "/Users/hayashikuniyuki/Documents/01TODO/ebook/OpenOfficeDebugPring.txt",DebugPrint
	print DebugPrint
	
	'//Shell参考
	'//http://wiki.services.openoffice.org/wiki/JA/Documentation/BASIC_Guide/Other_Functions_(Runtime_Library)
	Shell("/Developer/Applications/Dashcode.app/Contents/MacOS/Dashcode" ,1,"/Users/hayashikuniyuki/Documents/01TODO/ebook/OpenOfficeDebugPring.txt 

")

End sub

参考

http://programamemo2.blogspot.com/2007/12/openoffice.html

改行コード

関数で用意しておく

Function vbCrLf() as String
	vbCrLf = CHR(10)
End Function
トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-09-06 (月) 02:04:55 (4974d)