javascript - Excel VBA 单击由 javascript 控制的表格中的单元格

标签 javascript excel vba click

我正在尝试为 Excel 创建 Web 查询,但遇到了一些障碍。我弄清楚了如何登录网页并单击链接转到正确的 PortalID。然而,我在获取具体报告时遇到了困难。

要获取特定报告,我需要单击表格中的链接,但它似乎使用了 JavaScript。该表的 HTML 如下。此表中大约有 10 个单元格,但为了保持代码简洁,我只包含前 2 个单元格。正如您所看到的,每个单元格都有自己唯一的 ID,因此我可以使用 VBA 轻松筛选到正确的单元格元素。但是我似乎无法弄清楚如何实际单击该单元格。

<table submenu='1' border='0' cellpadding='6' cellspacing='0'
 id='ctl02n4c73594f1bf740b982340da2a792ff62_MainM' 
 class="ctl02n4c73594f1bf740b982340da2a792ff62Island"  
 style=' background:#141311; border-width:0px; width:100%; 
 cursor:Default;' onselectstart="javascript:igmenu_selectStart();" 
 onmouseover="javascript:igmenu_mouseover(this, event);" 
 onmouseout="javascript:igmenu_mouseout(this, event);" 
 onmousedown="javascript:igmenu_mousedown(this, event);" 
 onmouseup="javascript:igmenu_mouseup(this, event);"igLevel='0'>
  <tr>
    <td align='center' id='ctl02n4c73594f1bf740b982340da2a792ff62_1' 
     igTag='4eddf201-f6ed-4e11-b2ff-6a742128909c'  
     class="n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Class"style=" 
     white-space: nowrap; ;" igHov='n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Hover' 
     igTop='1'>Welcome</td>
    <td align='center' id='ctl02n4c73594f1bf740b982340da2a792ff62_2' 
     igTag='e0d4474e-87f8-42cc-ab47-38751029052d'  
     class="n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Class"style=" 
     white-space: nowrap; ;" igHov='n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Hover' 
     igTop='1'>Dashboard</td>
  </tr>
</table>

这是我的 vba,用于定向到我需要单击的单元格。看来我需要调用表的“onmouseup”事件。我如何将我需要的单元格与表格的 onmouseup 事件“链接”?

Sub clickTable(toFind As String, ie As Object)
'"toFind" is the ID of the cell to find, ie is the IE application
Dim ieDoc As Object 'ieDocDocument
Dim tdCollection As Object 'table that has the javascript events and contains
                            the cells I want to click
Dim cell As Object 'specific "clickable" cell in the table to "click"

    Set ieDoc = ie.document
    Set tdCollection = ieDoc.getelementbyID("ctl02n4c73594f1bf740b982340da2a792ff62_MainM").getElementsByTagName("td")
    For Each cell In tdCollection
        If cell.ID = toFind Then
            cell.Click
            Exit Sub
        End If
    Next

End Sub

最佳答案

嗯,在搜索其他 vba/javascript 问题时,我实际上自己解决了!

我浏览了所有的 javascript 代码,看看 HTML 事件如何与 javascript 绑定(bind)在一起,果然,“onmouseup”需要“onmousedown”,而“onmousedown”又需要“onmouseover”。在这里,我试图执行最终的交互,甚至没有想到分解 JavaScript 代码。

因此,如果其他人在尝试单击 javascript 控制的表格菜单中的单元格时遇到问题,您可能需要根据 javascript 的编写方式来执行鼠标与页面交互的所有方式。

这是我最终的有效代码。

Sub clickTable(toFind As String, ie As Object)
'toFind is the ID of the element to find, ie is the IE application
Dim ieDoc As Object 'ieDocDocument
Dim tdCollection As Object 'table that has the javascript attributes and contains the element I want to click
Dim cell As Object 'specific "clickable" cell in the table to test

        Set ieDoc = ie.document
        Set tdCollection = ieDoc.getelementbyID("ctl02n4c73594f1bf740b982340da2a792ff62_MainM").getElementsByTagName("td")
        For Each cell In tdCollection
            If cell.ID = toFind Then
                cell.FireEvent ("onmouseover")
                cell.FireEvent ("onmousedown")
                cell.FireEvent ("onmouseup")
                Exit Sub
            End If
        Next

    End Sub

关于javascript - Excel VBA 单击由 javascript 控制的表格中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23728887/

相关文章:

javascript - 从 fetch() 返回 html 并显示给用户

javascript - 使用样式按钮时,jQuery 返回选中单选按钮的错误值

excel - 通过excel将unix时间转换为日期时间

arrays - 将逗号分隔的数组作为函数参数传递

excel - VBA 用户窗体问题 - 无法设置列表属性。无效的属性数组索引

vba - 将当前工作表设置为变量,以便更轻松地使用公式

javascript - Angular $routeParams 强制变量类型

javascript - 如何使用 Jquery 触发 onload 事件?

excel - 转义双引号 Visual Basic excel

vba - Excel VBA : Loop through sheets/transfer data/create new workbook for each