shohei blog

月: 2010年4月

HTMLのテーブルタグを縦横入れ替える

某大手ECサイトというか楽天の一括更新はCSVなので必然的にExcelでやる羽目になる。
商品情報をなぜかテーブルタグで書いてたのでそれを縦横入れ替えたいときは下記のようなスクリプトを書けば一括ですね、という話。
Option Explicit
Sub main()
Dim SelectCell As Range
Set SelectCell = Range("g3")
SelectCell.Activate
While SelectCell.Value <> ""
If InStr(SelectCell.Value, "table") <> 0 Then
SelectCell.Value = ReplaceHTML(SelectCell.Value)
End If
Set SelectCell = SelectCell.Cells(2, 1)
SelectCell.Activate
Wend
End Sub
Function ReplaceHTML(str As String) As String
Dim pos_header As Integer
Dim pos_footer As Integer
Dim pos_tbl_header As Integer
Dim pos_tbl_footer As Integer
Dim header As String
Dim footer As String
Dim table_data As String
Dim table_body As String
Dim table_header As String
Dim table_footer As String
Dim table_body_th As String
Dim table_body_td As String
Dim temp As String
Dim title As Variant
Dim data As Variant
Dim i As Integer
'テーブルタグまでヘッダを検出する
pos_header = InStr(str, "<table")
header = Left(str, pos_header - 1)
'テーブルタグが終わった後のフッタを検出する
pos_footer = InStr(str, "</table>")
footer = Right(str, Len(str) - pos_footer - 7)
table_data = Mid(str, pos_header, Len(str) - (Len(str) - pos_footer) - pos_header + 8)
'Debug.Print "header------" & header
'Debug.Print "table-------" & table_data
'Debug.Print "footer------" & footer
pos_tbl_header = InStr(table_data, "<tr")
table_header = Left(table_data, pos_tbl_header - 1)
pos_tbl_footer = InStr(table_data, "</table>")
table_footer = Right(table_data, Len(table_data) - pos_tbl_footer)
'テーブルのTR的な部分だけを検出。
table_body = Mid(table_data, pos_tbl_header, Len(table_data) - pos_tbl_header - (Len(table_data) - pos_tbl_footer - 1))
'Debug.Print "テーブルのヘッダ部分" & table_header
'Debug.Print "テーブルのフッタ部分" & table_footer
'Debug.Print "テーブルの本体部分" & table_body
'<TH>部分を抜き出す
table_body_th = Left(table_body, InStr(table_body, "</tr>") + 4)
table_body_td = Right(table_body, Len(table_body) - Len(table_body_th))
'Debug.Print "Th部分" & table_body_th
'Debug.Print "Td部分" & table_body_td
title = Split(table_body_th, "</th>")
data = Split(table_body_td, "</td>")
For i = 0 To UBound(title)
title(i) = RemoveHTML(title(i))
Next i
For i = 0 To UBound(data)
data(i) = RemoveHTML(data(i))
Next i
For i = 0 To UBound(title) - 1
temp = temp & "<tr>"
temp = temp & "<th>" & title(i) & "</th>"
temp = temp & "<td>" & data(i) & "</td>"
temp = temp & "</tr>"
Next
ReplaceHTML = header & table_header & temp & "</table>" & footer
End Function
Function RemoveHTML(strHTML) As String
'HTMLタグを削除するファンクション
Dim Flg As Boolean
Dim i As Integer
For i = 1 To Len(strHTML)
If Mid(strHTML, i, 1) = "<" Then
Flg = True
Mid(strHTML, i, 1) = " "
ElseIf Mid(strHTML, i, 1) = ">" Then
Flg = False
Mid(strHTML, i, 1) = " "
ElseIf Flg Then
Mid(strHTML, i, 1) = " "
End If
Next
strHTML = Replace(strHTML, " ", "")
'Debug.Print strHTML
RemoveHTML = strHTML
End Function
Option Explicit
Sub main()Dim SelectCell As Range
Set SelectCell = Range("g3")SelectCell.Activate
While SelectCell.Value <> ""    If InStr(SelectCell.Value, "table") <> 0 Then        SelectCell.Value = ReplaceHTML(SelectCell.Value)    End If    Set SelectCell = SelectCell.Cells(2, 1)    SelectCell.ActivateWend
End Sub
Function ReplaceHTML(str As String) As String
Dim pos_header As IntegerDim pos_footer As IntegerDim pos_tbl_header As IntegerDim pos_tbl_footer As Integer
Dim header As StringDim footer As StringDim table_data As StringDim table_body As StringDim table_header As StringDim table_footer As StringDim table_body_th As StringDim table_body_td As StringDim temp As String
Dim title As VariantDim data As Variant
Dim i As Integer
'テーブルタグまでヘッダを検出するpos_header = InStr(str, "<table")header = Left(str, pos_header - 1)
'テーブルタグが終わった後のフッタを検出するpos_footer = InStr(str, "</table>")footer = Right(str, Len(str) - pos_footer - 7)
table_data = Mid(str, pos_header, Len(str) - (Len(str) - pos_footer) - pos_header + 8)
'Debug.Print "header------" & header'Debug.Print "table-------" & table_data'Debug.Print "footer------" & footer
pos_tbl_header = InStr(table_data, "<tr")table_header = Left(table_data, pos_tbl_header - 1)
pos_tbl_footer = InStr(table_data, "</table>")table_footer = Right(table_data, Len(table_data) - pos_tbl_footer)

'テーブルのTR的な部分だけを検出。table_body = Mid(table_data, pos_tbl_header, Len(table_data) - pos_tbl_header - (Len(table_data) - pos_tbl_footer - 1))
'Debug.Print "テーブルのヘッダ部分" & table_header'Debug.Print "テーブルのフッタ部分" & table_footer'Debug.Print "テーブルの本体部分" & table_body
'<TH>部分を抜き出す
table_body_th = Left(table_body, InStr(table_body, "</tr>") + 4)table_body_td = Right(table_body, Len(table_body) - Len(table_body_th))
'Debug.Print "Th部分" & table_body_th'Debug.Print "Td部分" & table_body_td
title = Split(table_body_th, "</th>")data = Split(table_body_td, "</td>")
For i = 0 To UBound(title)    title(i) = RemoveHTML(title(i))Next i
For i = 0 To UBound(data)    data(i) = RemoveHTML(data(i))Next i
For i = 0 To UBound(title) - 1    temp = temp & "<tr>"    temp = temp & "<th>" & title(i) & "</th>"    temp = temp & "<td>" & data(i) & "</td>"    temp = temp & "</tr>"Next
ReplaceHTML = header & table_header & temp & "</table>" & footer
End Function
Function RemoveHTML(strHTML) As String'HTMLタグを削除するファンクション
Dim Flg As Boolean
Dim i As Integer
For i = 1 To Len(strHTML)    If Mid(strHTML, i, 1) = "<" Then        Flg = True        Mid(strHTML, i, 1) = " "    ElseIf Mid(strHTML, i, 1) = ">" Then        Flg = False        Mid(strHTML, i, 1) = " "    ElseIf Flg Then        Mid(strHTML, i, 1) = " "    End If Next strHTML = Replace(strHTML, " ", "")'Debug.Print strHTMLRemoveHTML = strHTMLEnd Function

久々に

VBA書いたので残しておくw
Set ~思い出すのに3分かかったw

Sub main()
Dim SelectCell As Range

Set SelectCell = Range(“i2”)
SelectCell.Activate

While SelectCell.Value <> “”

SelectCell.Value = ReplaceHTML(SelectCell.Value)

Set SelectCell = SelectCell.Cells(2, 1)
SelectCell.Activate

Wend

End Sub

Function ReplaceHTML(str As String) As String

Dim pos As Integer
Dim str1 As String
Dim str2 As String

‘最初のTRまで検出する
pos = InStr(str, “</tr”)

str1 = Left(str, pos)
str2 = Right(str, Len(str) – pos)

str1 = Replace(str1, “<tr”, “<tr id=””st_head”” “, 1, -1, vbTextCompare)
str2 = Replace(str2, “<tr”, “<tr id=””st_date”” “, 1, -1, vbTextCompare)

ReplaceHTML = str1 & str2

End Function