crossframe » VBA http://crossframe.iiv.jp Windows Dev. Site Tue, 07 Nov 2023 06:31:52 +0000 ja hourly 1 https://wordpress.org/?v=3.8.41 SQLite for Excel ../../../201603051181/ ../../../201603051181/#comments Sat, 05 Mar 2016 10:19:27 +0000 ../../../?p=1181 一つのファイルでデータベースを扱える手軽さから、さまざまなプラットホームで使われているSQLite。Excelから直接扱えることを知り、テストしてみました。

http://sqliteforexcel.codeplex.com/

このサイトからダウンロードしたファイルだけで、特に何かインストールすることなくDBファイルに読み書きできました。
Distributionフォルダにある、SQLiteForExcel_64.xlsmを使用。
sqlite3.dllはx64フォルダのものに変更します。

環境 : Excel 2013 / Windows 10
このファイルには、SQlite3とSQlite3DemoというVBAモジュールがすでに組み込まれており、SQlite3Demoを参考に以下のプログラムを作成しました。
ボタン1でDBの作成、データ書き込み、ボタン2でDB読み込みセルに表示をしています。

Sub ボタン1_Click()
    Dim InitReturn As Long
    Dim testFile As String
    Dim RetVal As Long
    Dim myDbHandle As LongPtr
    Dim myStmtHandle As LongPtr
    
    InitReturn = SQLite3Initialize
    If InitReturn <> SQLITE_INIT_OK Then
        Debug.Print "Error Initializing SQLite. Error: " & Err.LastDllError
        Exit Sub
    End If
    
    testFile = "C:\temp\Test01.db3"
    RetVal = SQLite3Open(testFile, myDbHandle)
    Debug.Print "SQLite3Open returned " & RetVal

    RetVal = SQLite3PrepareV2(myDbHandle, "CREATE TABLE MySecondTable (TheId INTEGER, TheText TEXT, TheValue REAL)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal

    RetVal = SQLite3Finalize(myStmtHandle)
    Debug.Print "SQLite3Finalize returned " & RetVal
    
    RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MySecondTable Values (123, 'ABC', 42.1)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal

    RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MySecondTable Values (456, 'DEF', 12.3)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal
    
    RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MySecondTable Values (789, 'GHI', 45.6)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal
    
    RetVal = SQLite3Finalize(myStmtHandle)
    Debug.Print "SQLite3Finalize returned " & RetVal

    RetVal = SQLite3Close(myDbHandle)


End Sub

Sub ボタン2_Click()
    Dim testFile As String
    Dim RetVal As Long
    Dim myDbHandle As LongPtr
    Dim myStmtHandle As LongPtr
    
    testFile = "C:\temp\Test01.db3"
    
    RetVal = SQLite3Open(testFile, myDbHandle)
    Debug.Print "SQLite3Open returned " & RetVal
    
    RetVal = SQLite3PrepareV2(myDbHandle, "SELECT * FROM MySecondTable", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal

    r = 1
    Do While RetVal <> SQLITE_DONE
        Cells(r, 1).Value = SQLite3ColumnText(myStmtHandle, 0)
        Cells(r, 2).Value = SQLite3ColumnText(myStmtHandle, 1)
        Cells(r, 3).Value = SQLite3ColumnText(myStmtHandle, 2)
        RetVal = SQLite3Step(myStmtHandle)
        r = r + 1
    Loop
    
    RetVal = SQLite3Finalize(myStmtHandle)
    Debug.Print "SQLite3Finalize returned " & RetVal
    
    RetVal = SQLite3Close(myDbHandle)

End Sub

実行結果。
sqliteExcel

DB Browser for SQLiteでDBファイルの内容を確認。
sqliteExcel02

データベースのデフォルトデータを作成する用途に便利だと思いました。

]]>
../../../201603051181/feed/ 0
D3.js from Excel ../../../20131214667/ ../../../20131214667/#comments Sat, 14 Dec 2013 07:37:39 +0000 http://xfra.me/?p=667 前回、Excelデータのビジュアライズに、WebViewのバージョンの関係によりD3.jsでなくjqplotを使用しましたが、Selenium IDEを使うと、WebViewでなくWebブラウザを使用できるので、これならD3.jsでもと思い、トライしてみました。

このツールはWebアプリのテスト等に使うもので、ブラウザ操作を自動的にできるなどとても強力なツールです。また各種プログラムコードも出力してくれます。

selenium-vba

https://code.google.com/p/selenium-vba/

ここではVBAからseleniumアドインを組み込んだFirefoxをコントロールする方法でテストしました。これならVBAからFirefoxで表示されているページのJavaScriptを呼び出すことができます。

環境 : SeleniumWrapperSetup-1.0.16.0.exe, Firefox 23.0.1, Excel 2010 / Windows 7
Excel Visual Basic for Application -> ツール -> 参照設定で、SeleniumWrapper Type Library を追加

Private Sub CommandButton1_Click()
   Dim selenium As New SeleniumWrapper.WebDriver
   selenium.Start "firefox", "file:///C:\d3.html"
   selenium.Open "file:///C:\d3.html"
   For i = 1 To 20
       v1 = Sheets(1).Cells(i, 1).Value
       v2 = Sheets(1).Cells(i, 2).Value
       v3 = Sheets(1).Cells(i, 3).Value

       selenium.getEval "setData({'clm1':" + Str(v1) + ", 'clm2':" + Str(v2) + ", 'clm3':" + Str(v3) + "});"
   Next i
   selenium.getEval "plot();"
End Sub

d3_2

D3.jsは以下のサンプルを使いました。

http://bl.ocks.org/mbostock/raw/7586334/

d3_1

サンプルでは、csvからファイルを読み込むようになっていますが、これを以下のように変更しました。
d3.html抜粋

var cars = [];
function setData(d)
{
	cars.push(d);
}
function plot(){
  x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
    return d != "name" && (y[d] = d3.scale.linear()
        .domain(d3.extent(cars, function(p) { return +p[d]; }))
        .range([h, 0]));
  }));
 .....
}
.....

Firefoxにもアドインを入れておく必要があります。
SeleniumIDE
これはなかなかいいかも。

]]>
../../../20131214667/feed/ 0