2012年3月15日木曜日

次は千葉沖か?!!...

なんか怖いですね、最近二日千葉沖の地震が多すぎるでしょう!ドッカンはいつ来るでしょうか!

2012年3月8日木曜日

なぜ"-f"(--force)オプションが効かないだろう

普通にCPコマンドを叩いたら

$ cp -rf from/dir_a to/
cp: `........................' を上書きしてもよろしいですか(yes/no)?

あれ、forceオプション追加したじゃないですか、なぜいちいち聞くの?と思ったらaliasで見たら

$ alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

"-i"か!なるほど。ちなみに解決は

$ unalias cp
または
$ \cp -rf from/dir_a to/

2012年3月1日木曜日

railsコンソールでモデルのfindメソッドで関節的に関係を持つデーブルへのアクセス

3つのテーブルそれそれ下記の関係

tmp_fund_index ○----● fund ●----○ fund_ranking

#models/fund.rb
class Fund < ActiveRecord::Base
  has_many :fund_rankings
end

#models/fund_ranking.rb
class FundRanking < ActiveRecord::Base
  belongs_to :fund
end

#models/tmp_fund_index.rb
class TmpFundIndex < ActiveRecord::Base
  belongs_to :fund
end
まずコンソールで確認すると、出ました $ ruby script/console Loading development environment.
>> FundRanking.find(:all, :limit => 1, :include => [:fund => [:tmp_fund_index]])
コントローラでは
#controllers/fund/ranking_controller.rb
  @rank_dtl = FundRanking.find :all,
                               :include => [{:fund => :tmp_fund_index}],

ビューアーでは、こうやって参照できる
#views/fund/ranking/detail.rhtml
<%- if @rank_dtl -%>
  <%- @rank_dtl.each_with_index do |rank, idx| -%>
    <%= rank.fund.tmp_fund_index.standard_price %>
  <%- end -%>
<%- end -%>

2012年2月29日水曜日

vbaでエンコードがeucのファイルの処理

ADODB.Streamを利用して、vbaでエンコードがeucのファイルを処理する方法
Dim ADS As Object
Set ADS = CreateObject("ADODB.Stream")
Dim strText As String

'対象ファイルの状況によって、下記を使い分け
Const adTypeText = 2 'テキストファイル
Const adTypeBinary = 1 'バイナリファイル
Const adReadLine = -2 '行ごと読む
Const adReadAll = -1 'ファイル全体を読む
Const adLF = 10 '改行LF
Const adCR = 13 '改行CR
Const adCRLF = -1 '改行CRLF

With ADS
    .Open
    .Type = adTypeText
    .Charset = "EUC-JP" ' EUCコード
    .LineSeparator = adLF
    .LoadFromFile ファイル名 ' 読み込むファイルを指定
    .Position = 0 ' 読み込む位置は先頭から
    strText= .ReadText(adReadAll )
    .Close
End With
Debug.Print strText

2012年2月27日月曜日

特殊ディレクトリ一覧、XPとVISTA便利

便利ですよ、ありがとうございました。
引用元はhttp://pasofaq.jp/windows/mycomputer/folderlist.htm

名称 XP でのフォルダ Vista でのフォルダ
%USERPROFILE% C:\Documents and Settings\(ユーザー名) C:\Users\(ユーザー名)
%ALLUSERSPROFILE% C:\Documents and Settings\All Users C:\ProgramData
Application Data アプリケーションデータ %USERPROFILE%\Application Data %USERPROFILE%\AppData\Roaming
Cache キャッシュ %USERPROFILE%\Local Settings\ %USERPROFILE%\AppData\Local\
Temporary Internet Files Microsoft\Windows\
  Temporary Internet Files
Cookies クッキー %USERPROFILE%\Cookies %USERPROFILE%\AppData\Roaming\
Microsoft\Windows\Cookies
Desktop デスクトップ %USERPROFILE%\デスクトップ %USERPROFILE%\Desktop
Favorites お気に入り %USERPROFILE%\Favorites
History インターネット履歴 %USERPROFILE%\Local Settings\History %USERPROFILE%\AppData\Local\
Microsoft\Windows\History
My Music [XP] マイ ミュージック %USERPROFILE%\My Documents\My Music %USERPROFILE%\Music
[Vista] ミュージック
My Pictures [XP] マイ ピクチャ %USERPROFILE%\My Documents\My Pictures %USERPROFILE%\Pictures
[Vista] ピクチャ
My Video [XP] マイ ビデオ %USERPROFILE%\My Documents\My Videos %USERPROFILE%\Videos
[Vista] ビデオ
NetHood 共有フォルダ履歴 %USERPROFILE%\NetHood %USERPROFILE%\AppData\Roaming\
Microsoft\Windows\Network Shortcuts
Personal [XP] マイ ドキュメント %USERPROFILE%\My Documents %USERPROFILE%\Documents
[Vista] ドキュメント
PrintHood プリンタ フォルダ %USERPROFILE%\PrintHood %USERPROFILE%\AppData\Roaming\
Microsoft\Windows\Printer Shortcuts
Programs プログラムメニュー %USERPROFILE%\スタート メニュー\プログラム %USERPROFILE%\AppData\Roaming\
(個人) Microsoft\Windows\Start Menu\Programs
プログラムメニュー %ALLUSERSPROFILE%\スタート メニュー\プログラム %ALLUSERSPROFILE%\Microsoft\
(全ユーザー) Windows\Start Menu\Programs
Recent [XP] 最近使ったファイル %USERPROFILE%\Recent %USERPROFILE%\AppData\Roaming\
[Vista] 最近使った項目 Microsoft\Windows\Recent
SendTo 送る %USERPROFILE%\SendTo %USERPROFILE%\AppData\Roaming\
Microsoft\Windows\SendTo
Start Menu スタートメニュー %USERPROFILE%\スタート メニュー %USERPROFILE%\AppData\Roaming\
Microsoft\Windows\Start Menu
Startup スタートアップ %USERPROFILE%\スタート メニュー\プログラム\ %USERPROFILE%\AppData\Roaming\
スタートアップ Microsoft\Windows\Start Menu\
  Programs\Startup
Templates テンプレート %USERPROFILE%\Templates %USERPROFILE%\AppData\Roaming\
Microsoft\Windows\Templates

2012年2月15日水曜日

vbaでExcelチャートを弄るサンプル

vbaでExcelチャートを弄る簡単サンプル
Dim chartObj As ChartObject
Dim myChart As Chart
        For Each chartObj In someSheet.ChartObjects
            Select Case chartObj.Chart.ChartTitle.Caption
                Case "株価の推移"
                    'なぜか直接chartObj.Chart.SeriesCollectionを参照するとエラー
                    '一旦Chartオブジェクトに参照を移す
                    Set myChart = chartObj.Chart
                    
                    With myChart.SeriesCollection(1) 
                        .XValues = "='共通データ'!$B$2:$B$10"
                        .Values = "='共通データ'!$C$2:$C$10"
                    End With
                
                Case "そのた"
                
            End Select
        Next
上の例はチャートのタイトルで判別する場合、チャートの名前で判断する場合は
Dim chartObj As ChartObject
    Select Case chartObj.Name

ちなみにchartObj.Nameはreadonlyの属性みたい。chartObj.Nameを変更したい場合はExcelのメニュー
レイアウト>グラフ名で変更できます

2012年2月5日日曜日

自作VBAモジュール、特定のRangeをHashオブジェクトに格納するメソッドです。グループ分けのマスター/サブキーの設定も可能

自作モジュール、知る人ぞ知る、商用禁止
Option Explicit
'useage example:
'************************************************************************************
'load Microsoft scripting runtime
'dim result as Scripting.Dictionary
'set result = rg2hash(myWorksheet.Range("A2:CD255"), Array("term", "id") , True)
'************************************************************************************

'######################################################################################################################################################################################################
'rg2hash:               -- return an key formated hash that read from a certain range or Nothing if anything goes wrong
'author:                -- kagen.job$gmail.com write on 2102/2/5
'para1:rg               -- (must) an rectangle Range object to be read
'para2:keys             -- (opti - default Empty) an array of keys from main to sub such as array("Class","student","term","score"...),remember that first element of the array is the "master" key and others is the "sub" keys from big to small, and all the keys union should point to a unique record,if Empty will use the row number(string) as the master key and no sub keys
'para3:isFirstRowTitle  -- (opti - default True) falg if the first row of the rang is the title row,if True will use the title as item key (title should be unique), if False,will use the colume number(string) as the item key

'I made this to make it easy to get table into a easy "find" easy "reference" scripting dictionary object for vba developers
'######################################################################################################################################################################################################
Public Function rg2hash(ByVal rg As Range, Optional ByVal keys As Variant = Empty, Optional ByVal isFirstRowTitle As Boolean = True) As Scripting.Dictionary
    Dim dicHeader As New Scripting.Dictionary
    Dim dicHeaderRev As New Scripting.Dictionary
    Dim csvrow As Range
    Dim csvfield As Range
    Dim dicResult As New Scripting.Dictionary
    Dim key As Variant
    Dim keyVals As Variant
    Dim lngIndex As Long
    Dim refDic As Scripting.Dictionary
    
    
    'first removeall
    dicResult.RemoveAll
    
    If isFirstRowTitle Then
        'if only title row exit
        If Not rg.Rows.Count > 1 Then
            Set rg2hash = Nothing
            Exit Function
        End If
        For Each csvfield In rg.Rows(1).Columns
            'place keys
            If Not dicHeader.Exists(csvfield.Value) And Not IsEmpty(csvfield.Value) Then
                dicHeader.Add CStr(csvfield.Value), csvfield.Column
                dicHeaderRev.Add CStr(csvfield.Column), csvfield.Value
            Else
                'key should not be duplicated
                Set rg2hash = Nothing
                Exit Function
            End If
        Next
    Else
        For Each csvfield In rg.Rows(1).Columns
            'if no title row, column number will be the item keys
            dicHeader.Add CStr(csvfield.Column), csvfield.Column
            dicHeaderRev.Add CStr(csvfield.Column), csvfield.Column
        Next
    End If
    
    lngIndex = 1
    'begin to scan the range and read recorders
    For Each csvrow In rg.Rows
        'if first row is title skip it
        If lngIndex = 1 And isFirstRowTitle Then
        
        Else
            'make the {master key -> sub key1 -> sub key2 -> ... -> new dictionary object} strcture
            keyVals = Array()
            If Not IsEmpty(keys) Then
                If UBound(keys) >= 0 Then
                    For Each key In keys
                        push_ref CStr(Cells(csvrow.Row, dicHeader.Item(CStr(key))).Value), keyVals
                    Next
                End If
            Else
                'if no master/sub keys set, row index (start from 1) will be the master key and no sub keys
                push_ref CStr(IIf(isFirstRowTitle, lngIndex - 1, lngIndex)), keyVals
            End If
            Set refDic = buildDic(dicResult, keyVals)
            
            'at the last sub key position write the detail(item) records
            For Each csvfield In csvrow.Columns
                refDic.Add CStr(dicHeaderRev.Item(CStr(csvfield.Column))), csvfield.Value
            Next
            
        End If
        lngIndex = lngIndex + 1
    Next
    Set rg2hash = dicResult
    Set refDic = Nothing
    Set dicHeader = Nothing
    Set dicHeaderRev = Nothing
End Function
'this is a function to make a master-sub key strctured dictionary object and refer to the last sub key ready to put the detial record
Public Function buildDic(ByRef dic As Scripting.Dictionary, ByVal keys As Variant) As Scripting.Dictionary
    Dim key As Variant
    If UBound(keys) > 0 Then
        If Not dic.Exists(keys(LBound(keys))) Then
            dic.Add CStr(keys(LBound(keys))), New Scripting.Dictionary
        End If
        Set buildDic = buildDic(dic.Item(CStr(keys(LBound(keys)))), shift_ref(keys))
    ElseIf UBound(keys) = 0 Then
        dic.Add CStr(keys(0)), New Scripting.Dictionary
        Set buildDic = dic.Item(CStr(keys(0)))
    End If
End Function
'this is a function to remove the first element of an array
Public Function shift_ref(ByRef arr As Variant) As Variant
    Dim u As Long
    u = UBound(arr)
    Dim l As Long
    If u < 1 Then
        shift_ref = Array()
        Exit Function
    End If
    Dim v() As Variant
    For l = LBound(arr) To u - 1
        ReDim Preserve v(l)
        v(l) = arr(l + 1)
    Next
    shift_ref = v
End Function

'this is a function to push an element into an array
Public Function push_ref(ByVal val As Variant, ByRef arr As Variant) As Integer
    If IsArray(arr) Then
        ReDim Preserve arr(UBound(arr) + 1)
    Else
        ReDim arr(0)
    End If
    arr(UBound(arr)) = val
    push_ref = UBound(arr)
End Function