Takenoff Labs

Lotus Notes/Domino に関する Tips や、クラシックの名曲などを紹介します

[Notes/Domino] CSVの1行を配列にする関数

CSVの1行をデリミタで分割して配列にする処理はよくやると思いますが、CSVの書式に則ってちゃんと分割しようとすると、ちょっと面倒です。

ということで、今回はCSV分割用の関数を書いてみました。

※ この関数は自作関数の AryPush、AryIsUninit を使用しています。こちらのページから取得してください。

説明 CSVの1行をデリミタで配列に分割する
構文 SplitCSV(Byval strCsv As String, Byval strDelim As String, Byval strQuote As String) As Variant
引数 [in,---]strCsv ... CSVの1レコード
[in,---]strDelim ... デリミタ
[in,---]strQuote ... 引用符
戻り値 デリミタで分割された配列

ソースコード

 
Public Function SplitCSV(Byval strCsv As String, Byval strDelim As String, _
Byval strQuote As String) As Variant
	Dim strRet() As String
	Dim strBuf   As String
	Dim bln1st   As Boolean
	Dim blnQuote As Boolean
	
	strBuf = ""
	bln1st = True
	Do Until strCsv = ""
		If Left(strCsv, 1) = strQuote Then
			If bln1st = True Then
				blnQuote = True
				bln1st   = False
			Else
				If blnQuote = True Then
					If Left(strCsv, 2) = strQuote & strQuote Then
						strBuf = strBuf & strQuote
						strCsv = Mid(strCsv, 2)
					Else
						blnQuote = False
					End If
				Else
					strBuf = strBuf & strQuote
				End If
			End If
		Elseif Left(strCsv, 1) = strDelim Then
			If blnQuote = True Then
				strBuf = strBuf & strDelim
			Else
				Call AryPush(strRet, strBuf)
				strBuf = ""
				bln1st = True
			End If
		Else
			strBuf = strBuf & Left(strCsv, 1)
			bln1st = False
		End If
		strCsv = Mid(strCsv, 2)
	Loop
	
	If strBuf <> "" Then
		Call AryPush(strRet, strBuf)
	End If
	
	If AryIsUninit(strRet) = True Then
		Call AryPush(strRet, "")
	End If
	
	SplitCSV = strRet
End Function

例文

 
v = SplitCSV({"aaa","bbb,ccc","dd""ddd"}, {,}, {"})
↓
'vの値
aaa
bbb,ccc
dd"ddd

※ 書き方がスマートじゃないのは百も承知ですが、書き直すの面倒くさかったので(爆

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
読み込み中...

トラックバック

トラックバックはありません

コメント

コメントはありません

※コメントは承認制となっております。管理者が承認するまで表示されません。申し訳ありませんが、投稿が表示されるまでしばらくお待ちください。





(以下のタグが使えます)
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

For spam filtering purposes, please copy the number 6316 to the field below:

^
×