Posts: 858
Threads: 196
Joined: Apr 2005
Can you add an example of how to use this function in tables with rowspan?
Posts: 12,097
Threads: 142
Joined: Dec 2002
Macro Macro2765
out
str html=
;<!DOCTYPE html>
;<html>
;<head>
;<style>
;table, th, td {
;;;;;border: 1px solid black;
;}
;</style>
;</head>
;<body>
;
;<table>
;;;<tr>
;;;;;<th>Month</th>
;;;;;<th>Savings</th>
;;;;;<th>Savings for holiday!</th>
;;;</tr>
;;;<tr>
;;;;;<td>January</td>
;;;;;<td>$100</td>
;;;;;<td rowspan="2">$50</td>
;;;</tr>
;;;<tr>
;;;;;<td>February</td>
;;;;;<td>$80</td>
;;;</tr>
;</table>
;
;</body>
;</html>
HtmlDoc d.InitFromText(html)
ARRAY(MSHTML.IHTMLElement) a
d.GetTable(0 0 a)
int i
for i 0 a.len
,int rowspan=a[i].getAttribute("rowspan" 0)
,out rowspan
,
Posts: 858
Threads: 196
Joined: Apr 2005
if span is in the table in multiple cells in column 2: how can I list the all items in column 2?
Posts: 12,097
Threads: 142
Joined: Dec 2002
GetTable does not give row and column numbers. Need to find the table element, get its rows etc, it's not very easy.
Partial example:
Macro Macro2765
out
str html=
;<!DOCTYPE html>
;<html>
;<head>
;<style>
;table, th, td {
;;;;;border: 1px solid black;
;}
;</style>
;</head>
;<body>
;
;<table>
;;;<tr>
;;;;;<th>Month</th>
;;;;;<th>Savings</th>
;;;;;<th>Savings for holiday!</th>
;;;</tr>
;;;<tr>
;;;;;<td>January</td>
;;;;;<td>$100</td>
;;;;;<td rowspan="2">$50</td>
;;;</tr>
;;;<tr>
;;;;;<td>February</td>
;;;;;<td>$80</td>
;;;</tr>
;;;<tr>
;;;;;<td>March</td>
;;;;;<td>$90</td>
;;;;;<td>$70</td>
;;;</tr>
;</table>
;
;</body>
;</html>
HtmlDoc d.InitFromText(html)
MSHTML.IHTMLElement2 e=+d.GetHtmlElement("table" 0)
if(e=0) end "not found"
;out e.innerHTML
MSHTML.IHTMLElement2 ee
foreach ee e.getElementsByTagName("tr")
,out ee.getElementsByTagName("td").length
Posts: 858
Threads: 196
Joined: Apr 2005
Thanks.
It would be possible to transform table in ICsv?
Posts: 12,097
Threads: 142
Joined: Dec 2002
I don't know a function for it. Can try with IHTMLElement2 etc like in the example, or regular expressions, or XML. rowspan etc make it difficult.
Posts: 858
Threads: 196
Joined: Apr 2005
Posts: 12,097
Threads: 142
Joined: Dec 2002
Need more testing with all possible colSpan/rowSpan.
Member function HtmlDoc.GetTable2D
function `table [ARRAY(str)&a] [ARRAY(MSHTML.IHTMLElement)&a2] [flags] ;;flags: 1 get HTML
;Gets cells of a HTML table to a 2-dim array.
;Error if something fails, eg the specified table does not exist.
;table - table name or 0-based index. See <help>HtmlDoc.GetHtmlElement</help>. Also can be table element COM interface, eg MSHTML.IHTMLElement or IHTMLTable.
;a - array variable for results. The function creates 2-dimension array where each element is cell text. Can be 0 if not needed.
;a2 - array variable for results. The function creates 2-dimension array where each element is cell object that can be used to get html elements within the cell. Can be 0 if not needed.
;REMARKS
;If some cells have colspan or/and rowspan attribute, adds empty array elements, so that all array element positions match the visual positions of cells.
MSHTML.IHTMLTable t
if(table.vt=VT_DISPATCH) t=+table
else t=+GetHtmlElement("TABLE" table)
if(!t) end F"{ERR_OBJECT} (table)"
MSHTML.IHTMLTableRow tr
MSHTML.IHTMLTableCell td
MSHTML.IHTMLElement el
int i nc nr=t.rows.length
;calc n columns; t.cols gives 0
foreach tr t.rows
,i=0
,foreach(td tr.cells) i+td.colSpan
,if(i>nc) nc=i
if(&a) a.create(nc nr)
if(&a2) a2.create(nc nr)
ARRAY(int) ars.create(nc) ;;all rowSpan of prevous row
int r c rowSpan colSpan
foreach tr t.rows
,c=0
,foreach td tr.cells
,,;colSpan/rowSpan
,,if(r) rep() if(ars[c]) ars[c]-1; c+1; else break
,,
,,el=+td
,,if(&a) if(flags&1) a[c r]=el.innerHTML; else a[c r]=el.innerText
,,if(&a2) a2[c r]=el
,,
,,;colSpan/rowSpan
,,rowSpan=td.rowSpan; colSpan=td.colSpan
,,for(i 0 colSpan) ars[c+i]=rowSpan-1
,,c+colSpan
,r+1
err+ end ERR_FAILED
Macro test HtmlDoc.GetTable2D
out
str html=
;<!DOCTYPE html>
;<html>
;<head>
;<style>
;table, th, td {
;;;;;border: 1px solid black;
;}
;</style>
;</head>
;<body>
;
;<table>
;;;<tr>
;;;;;<th colspan=2>colspan 2</th>
;;;;;<th>Savings</th>
;;;</tr>
;;;<tr>
;;;;;<td>January</td>
;;;;;<td rowspan="2">rowspan 2</td>
;;;;;<td>2</td>
;;;</tr>
;;;<tr>
;;;;;<td>February</td>
;;;;;<td>3</td>
;;;</tr>
;;;<tr>
;;;;;<td>March</td>
;;;;;<td>4</td>
;;;;;<td>5</td>
;;;</tr>
;;;<tr>
;;;;;<td colspan="2" rowspan="2">colspan+rowspan</td>
;;;;;<td>6</td>
;;;</tr>
;;;<tr>
;;;;;<td>7</td>
;;;</tr>
;</table>
;
;</body>
;</html>
HtmlDoc d.InitFromText(html)
ARRAY(str) a
;ARRAY(MSHTML.IHTMLElement) a2
d.GetTable2D(0 a)
;d.GetTable2D(d.GetHtmlElement("TABLE" 0) a)
int r c
for r 0 a.len
,out F"---- row {r} ----"
,for c 0 a.len(1)
,,out a[c r]
Posts: 858
Threads: 196
Joined: Apr 2005
|