「Excel VBA ワークシートをHTMLテーブル」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「Excel VBA ==Excel VBA ワークシートをHTMLテーブル== <pre> Option Explicit Public Function MakeHtmlTable(table As Range) As String Dim buf As String…」) |
|||
(同じ利用者による、間の2版が非表示) | |||
1行目: | 1行目: | ||
[[Excel VBA]] | [[Excel VBA]] | ||
==Excel VBA ワークシートをHTMLテーブル== | ==Excel VBA ワークシートをHTMLテーブル== | ||
− | <pre> | + | <pre>Option Explicit |
− | Option Explicit | + | |
− | + | ||
+ | Public Function MakeHtmlTable(table As Range, Optional headerType As String = "r", Optional headerSize As Integer = 1, Optional cls As String = "") As String | ||
− | |||
Dim buf As String | Dim buf As String | ||
Dim cl As Range | Dim cl As Range | ||
11行目: | 11行目: | ||
Dim isFirstRow As Boolean | Dim isFirstRow As Boolean | ||
Dim celVal As String | Dim celVal As String | ||
+ | Dim colPos As Integer | ||
+ | Dim isColHeader As Boolean | ||
+ | Dim isRowHeader As Boolean | ||
+ | Dim celTag As String | ||
+ | |||
+ | If Trim(cls) <> "" Then | ||
+ | cls = " class=""" & cls & """ " | ||
+ | Else | ||
+ | cls = " border=""1"" " | ||
+ | End If | ||
+ | isColHeader = InStr(headerType, "c") > 0 | ||
+ | isRowHeader = InStr(headerType, "r") > 0 | ||
isFirstRow = True | isFirstRow = True | ||
rowPos = -1 | rowPos = -1 | ||
− | buf = "<table | + | buf = "<table " & cls & ">" |
For Each cl In table | For Each cl In table | ||
− | If rowPos <> cl. | + | |
+ | If rowPos <> cl.Row Then | ||
If Not isFirstRow Then | If Not isFirstRow Then | ||
buf = buf & "</tr>" | buf = buf & "</tr>" | ||
23行目: | 36行目: | ||
buf = buf & "<tr>" | buf = buf & "<tr>" | ||
isFirstRow = False | isFirstRow = False | ||
+ | colPos = 0 | ||
End If | End If | ||
celVal = EscapeHtmlSpecial(cl.text) | celVal = EscapeHtmlSpecial(cl.text) | ||
If Trim(celVal) = "" Then | If Trim(celVal) = "" Then | ||
− | celVal = " | + | celVal = " " |
End If | End If | ||
− | rowPos = cl. | + | rowPos = cl.Row |
− | + | ||
+ | If (isColHeader And headerSize > colPos) Or _ | ||
+ | (isRowHeader And headerSize > rowPos) Then | ||
+ | celTag = "th" | ||
+ | Else | ||
+ | celTag = "td" | ||
+ | |||
+ | End If | ||
+ | buf = buf & EncloseTag(celVal, celTag) | ||
+ | colPos = colPos + 1 | ||
Next | Next | ||
buf = buf & "</table>" | buf = buf & "</table>" | ||
− | + | ||
MakeHtmlTable = buf | MakeHtmlTable = buf | ||
− | + | ||
+ | End Function | ||
+ | Public Function EncloseTag(value As String, tag As String) | ||
+ | EncloseTag = "<" & tag & ">" & value & "</" & tag & ">" | ||
End Function | End Function | ||
− | + | ||
− | |||
Public Function EscapeHtmlSpecial(text As String) As String | Public Function EscapeHtmlSpecial(text As String) As String | ||
Dim buf As String | Dim buf As String | ||
50行目: | 75行目: | ||
Select Case c | Select Case c | ||
Case "&" | Case "&" | ||
− | c = "& | + | c = "&" |
Case "<" | Case "<" | ||
− | c = " | + | c = "<" |
Case ">" | Case ">" | ||
− | c = " | + | c = ">" |
Case """" | Case """" | ||
− | c = "' | + | c = "'""" |
End Select | End Select | ||
66行目: | 91行目: | ||
</pre> | </pre> | ||
+ | |||
+ | *使用例 | ||
+ | <pre> | ||
+ | =MakeHtmlTable(B2:C7, "r", 1, "wikitable") | ||
+ | </pre> | ||
+ | *結果 | ||
+ | <table class="wikitable" ><tr><td>プロパティ</td><td>内容</td></tr><tr><td>axis</td><td>(UIStackViewのみ) スタックビューの方向を定義し、垂直または水平のどちらかを指定します</td></tr><tr><td>orientation</td><td>スタックビューの方向を指定します。(NSStackView のみ) スタックビューの方向を、垂直または水平に定義</td></tr><tr><td>distribution</td><td>軸に沿ったビューのレイアウトを定義</td></tr><tr><td>alignment</td><td> スタックビューの軸に垂直なビューのレイアウトを定義</td></tr><tr><td>spacing</td><td> 隣接するビューの間のスペースを定義します。</td></table> |
2022年3月26日 (土) 00:36時点における最新版
Excel VBA ワークシートをHTMLテーブル
Option Explicit Public Function MakeHtmlTable(table As Range, Optional headerType As String = "r", Optional headerSize As Integer = 1, Optional cls As String = "") As String Dim buf As String Dim cl As Range Dim rowPos As Integer Dim isFirstRow As Boolean Dim celVal As String Dim colPos As Integer Dim isColHeader As Boolean Dim isRowHeader As Boolean Dim celTag As String If Trim(cls) <> "" Then cls = " class=""" & cls & """ " Else cls = " border=""1"" " End If isColHeader = InStr(headerType, "c") > 0 isRowHeader = InStr(headerType, "r") > 0 isFirstRow = True rowPos = -1 buf = "<table " & cls & ">" For Each cl In table If rowPos <> cl.Row Then If Not isFirstRow Then buf = buf & "</tr>" End If buf = buf & "<tr>" isFirstRow = False colPos = 0 End If celVal = EscapeHtmlSpecial(cl.text) If Trim(celVal) = "" Then celVal = " " End If rowPos = cl.Row If (isColHeader And headerSize > colPos) Or _ (isRowHeader And headerSize > rowPos) Then celTag = "th" Else celTag = "td" End If buf = buf & EncloseTag(celVal, celTag) colPos = colPos + 1 Next buf = buf & "</table>" MakeHtmlTable = buf End Function Public Function EncloseTag(value As String, tag As String) EncloseTag = "<" & tag & ">" & value & "</" & tag & ">" End Function Public Function EscapeHtmlSpecial(text As String) As String Dim buf As String Dim c As String Dim i As Integer For i = 1 To Len(text) c = Mid(text, i, 1) Select Case c Case "&" c = "&" Case "<" c = "<" Case ">" c = ">" Case """" c = "'""" End Select buf = buf & c Next EscapeHtmlSpecial = buf End Function
- 使用例
=MakeHtmlTable(B2:C7, "r", 1, "wikitable")
- 結果
プロパティ | 内容 |
axis | (UIStackViewのみ) スタックビューの方向を定義し、垂直または水平のどちらかを指定します |
orientation | スタックビューの方向を指定します。(NSStackView のみ) スタックビューの方向を、垂直または水平に定義 |
distribution | 軸に沿ったビューのレイアウトを定義 |
alignment | スタックビューの軸に垂直なビューのレイアウトを定義 |
spacing | 隣接するビューの間のスペースを定義します。 |
© 2006 矢木浩人