getTableData

構文
getTableData( table, Var arr )
引数
table
tableエレメント
arr
取得したデータを格納する配列(参照引数)
戻値

プログラム

////////////////////////////////////////////////// // 【引数】 // table : tableエレメント // arr : 取得したデータを格納する配列(参照引数) // 【戻値】 // ////////////////////////////////////////////////// PROCEDURE getTableData(table, Var arr[][]) // 何行何列のテーブルか求める rowMax = table.rows.length - 1 colMax = 0 FOR row = 0 TO table.rows.length - 1 IFB table.rows(row).cells.length - 1 > colMax THEN colMax = table.rows(row).cells.length - 1 ENDIF NEXT // テーブルの値を格納するための2次元配列を作成 DIM arr[rowMax][colMax] // テーブルの値を格納 FOR row = 0 TO table.rows.length - 1 FOR col = 0 TO table.rows(row).cells.length - 1 n = 0 WHILE arr[row][col + n] <> "" n = n + 1 WEND arr[row][col + n] = table.rows(row).cells(col).innerText // rowSpan(行結合)とcolSpan(列結合)の両方があれば IFB table.rows(row).cells(col).rowSpan > 1 AND table.rows(row).cells(col).colSpan > 1 THEN rmax = table.rows(row).cells(col).rowSpan - 1 cmax = table.rows(row).cells(col).colSpan - 1 FOR r = 1 TO rmax FOR c = 1 TO cmax arr[row + r][col + c] = "←" NEXT NEXT ENDIF // rowSpan(行結合)があれば結合セルに「↑」を代入 IFB table.rows(row).cells(col).rowSpan > 1 THEN n = table.rows(row).cells(col).rowSpan - 1 WHILE n arr[row + n][col] = "↑" n = n - 1 WEND ENDIF // colSpan(列結合)があれば結合セルに「←」を代入 IFB table.rows(row).cells(col).colSpan > 1 THEN n = table.rows(row).cells(col).colSpan - 1 WHILE n arr[row][col + n] = "←" n = n - 1 WEND ENDIF NEXT NEXT FEND

プログラム実行例

楽天銀行定期預金の金利をExcelに保存

DIM IE = CREATEOLEOBJ("InternetExplorer.Application") IE.Visible = TRUE IE.Navigate("https://rbweb.rakuten-bank.co.jp/REF/main/fis/BasicTimeDepositInterestRate.html?PageID=BasicTimeDepositInterestRatePresentationLogicBean") BusyWait(IE) DIM elements = IE.document.getElementsByTagName("table") DIM element = elements.Item(0) DIM arr[-1][-1] getTableData(element, arr) IE.Quit DIM Excel = XLOPEN() XLSETDATA(Excel, arr, "A1") DIM FSO = CREATEOLEOBJ("Scripting.FileSystemObject") DIM path = uniqueFilename(FSO.BuildPath(GET_CUR_DIR, "楽天銀行定期預金金利.xlsx")) XLCLOSE(Excel, path) ////////////////////////////////////////////////// // 【引数】 // IE : IEオブジェクト // 【戻値】 // ////////////////////////////////////////////////// PROCEDURE BusyWait(Var IE) SLEEP(0.500) DIM t = GETTIME() TRY REPEAT DIM tm = GETTIME() - t FUKIDASI("BusyWait:" + tm) SLEEP(0.010) IF tm >= 60 THEN BREAK UNTIL !IE.Busy AND IE.readyState = 4 EXCEPT IE = getIEObj(-1) PRINT IE.document.URL + " のIEオブジェクトを取得しました。" BusyWait(IE) ENDTRY FUKIDASI() FEND ////////////////////////////////////////////////// // 【引数】 // 文字列 or 数値 : 取得したいIEオブジェクトのタイトル・URLもしくは数値を指定 // 完全一致フラグ : (TRUE : 文字列が完全一致したものを取得、FALSE : 文字列の一部を含むものを取得) // 【戻値】 // Internet Explorerオブジェクト ////////////////////////////////////////////////// FUNCTION getIEObj(str, flg = FALSE) DIM Shell = CREATEOLEOBJ("Shell.Application") SELECT CHKNUM(str) CASE TRUE DIM cnt = 0 SELECT TRUE CASE str > 0 FOR n = 0 TO Shell.Windows.Count - 1 TRY IFB Shell.Windows.Item(n).Name = "Internet Explorer" THEN cnt = cnt + 1 IFB str = cnt THEN RESULT = Shell.Windows.Item(n) EXIT ENDIF ENDIF EXCEPT ENDTRY NEXT CASE str < 0 FOR n = Shell.Windows.Count - 1 TO 0 STEP -1 TRY IFB Shell.Windows.Item(n).Name = "Internet Explorer" THEN cnt = cnt + 1 IFB ABS(str) = cnt THEN RESULT = Shell.Windows.Item(n) EXIT ENDIF ENDIF EXCEPT ENDTRY NEXT CASE str = 0 FOR n = 0 TO Shell.Windows.Count - 1 TRY IF Shell.Windows.Item(n).Name = "Internet Explorer" THEN cnt = cnt + 1 EXCEPT ENDTRY NEXT RESULT = cnt EXIT SELEND CASE FALSE DIM t = GETTIME() REPEAT FOR n = 0 TO Shell.Windows.Count - 1 TRY DIM targetObj = Shell.Windows.Item(n) IFB targetObj.Name = "Internet Explorer" THEN SELECT flg CASE TRUE IFB targetObj.document.title = str OR targetObj.LocationURL = str THEN RESULT = Shell.Windows.Item(n) EXIT ENDIF CASE FALSE IFB POS(str, targetObj.document.title) OR POS(str, targetObj.LocationURL) THEN RESULT = Shell.Windows.Item(n) EXIT ENDIF SELEND ENDIF EXCEPT ENDTRY NEXT UNTIL GETTIME() - t >= 5 SELEND RESULT = ERR_VALUE FEND ////////////////////////////////////////////////// // 【引数】 // table : tableエレメント // arr : 取得したデータを格納する配列(参照引数) // 【戻値】 // ////////////////////////////////////////////////// PROCEDURE getTableData(table, Var arr[][]) // 何行何列のテーブルか求める rowMax = table.rows.length - 1 colMax = 0 FOR row = 0 TO table.rows.length - 1 IFB table.rows(row).cells.length - 1 > colMax THEN colMax = table.rows(row).cells.length - 1 ENDIF NEXT // テーブルの値を格納するための2次元配列を作成 DIM arr[rowMax][colMax] // テーブルの値を格納 FOR row = 0 TO table.rows.length - 1 FOR col = 0 TO table.rows(row).cells.length - 1 n = 0 WHILE arr[row][col + n] <> "" n = n + 1 WEND arr[row][col + n] = table.rows(row).cells(col).innerText // rowSpan(行結合)とcolSpan(列結合)の両方があれば IFB table.rows(row).cells(col).rowSpan > 1 AND table.rows(row).cells(col).colSpan > 1 THEN rmax = table.rows(row).cells(col).rowSpan - 1 cmax = table.rows(row).cells(col).colSpan - 1 FOR r = 1 TO rmax FOR c = 1 TO cmax arr[row + r][col + c] = "←" NEXT NEXT ENDIF // rowSpan(行結合)があれば結合セルに「↑」を代入 IFB table.rows(row).cells(col).rowSpan > 1 THEN n = table.rows(row).cells(col).rowSpan - 1 WHILE n arr[row + n][col] = "↑" n = n - 1 WEND ENDIF // colSpan(列結合)があれば結合セルに「←」を代入 IFB table.rows(row).cells(col).colSpan > 1 THEN n = table.rows(row).cells(col).colSpan - 1 WHILE n arr[row][col + n] = "←" n = n - 1 WEND ENDIF NEXT NEXT FEND ////////////////////////////////////////////////// // 【引数】 // path : ファイルのパス // 【戻値】 // 重複しないファイル名 ////////////////////////////////////////////////// FUNCTION uniqueFilename(path) DIM FSO = CREATEOLEOBJ("Scripting.FileSystemObject") IFB FSO.FileExists(path) THEN DIM fol = FSO.GetParentFolderName(path) DIM filename = FSO.GetBaseName(path) DIM extension = FSO.GetExtensionName(path) i = 2 WHILE FSO.FileExists(FSO.BuildPath(fol, filename + " (" + i + ")." + extension)) i = i + 1 WEND RESULT = FSO.BuildPath(fol, filename + " (" + i + ")." + extension) ELSE RESULT = path ENDIF FEND