2014-01-15 56 views
0

嗨我正在尝试从网页导入数据http://www.investing.com/indices/us-spx-500-futures-historical-data 我想要做的是导航到此网页并从中选择不同日期范围的宏日历。使用VBA将数组传递给网页中的脚本函数以及从脚本函数传递数组

我不明白如何在日历中设置日期。我可以打开它,但我不能够“复制”鼠标点击选择日期 我公司开发了代码,现在是:

Sub Problem() 
Dim IE As Object 
Set IE = CreateObject("InternetExplorer.application") 
IE.Visible = True 
IE.navigate "http://www.investing.com/indices/us-30-historical-data" 
' 
PauseTime = 4 ' Set duration to wait in seconds. 
Start = Timer ' Set start time. 
Do ' While Timer < Start + PauseTime 
DoEvents ' allow other processes to work (the browser to have time to load the webpage) 
Loop Until IE.ReadyState = READYSTATE_COMPLETE Or Timer > Start + PauseTime 
IE.Document.getElementByID("datePickerIconWrap").Click 
' 
End Sub 

我看了这个页面,我的源代码发现两个有趣的功能,在DatePickerGetDateDatePickerSetDate 我试图同时运行脚本,以使用

IE.Document.parentWindow.execScript "widgetHolCalendar.DatePickerGetDate", "javascript" 
IE.Document.parentWindow.execScript "widgetHolCalendar.DatePickerSetDate", "javascript" 

没有错误消息是由代码中给出,但没有被改变,我不知道该代码是真执行某事。如果我理解正确的密码,设置新的日期我要叫DatePickerSetDate以2个参数

DatePickerSetDate(date, shifTo) 

其中日期是2个元素的数组和shifTO是一个布尔值。我不知道如何使用vba将数组传递给这个脚本。

此外,当我调用函数DatePickerGetDate我希望得到的结果,并保存在VBA阵列

谁能帮助?

+0

如果时许了解你的权利,你要不要建立一个IE插件使用VBA?那么,在你的问题钩过点击很简单:只需在当前重点标签(这应该是你刚刚打开的页面)注入JavaScript代码。我想用IE.Document.parentWindow.execScript你可以插入JS代码到当前标签。您还划分为内容脚本和后台脚本(在这种情况下为您的VBA应用程序)。我认为你应该更多地了解如何开发浏览器附加组件。 – alpham8

+0

抱歉alpham8,我不确定明白。我想要做的是在Excel中使用宏来更改该页面日历中的日期。我不想开发一个附加组件。至于在execScript中使用JS代码,你能举一个简单的例子来说明如何做这样的事情吗? – MeSS83

+0

哦,对不起,误解了你。如果您处于浏览器扩展上下文中,则只能在JS中使用execScript函数。在过去,我也使用MS Office的VBA开发宏。我从来没有听说过使用VBA代码中隐含的JavaScript的方法。我认为你在混合两个不同的东西。如果你在这个页面上找到了JS代码,这与Excel VBA宏代码没有关系......你的意思是什么日历?就像从传入的输入数据中绘制图表一样,在Excel中抛出一个向导? – alpham8

回答

3

这为我工作(从当页面完全加载捡......)

Dim f As String 

'set the new dates (you just need to plug the required strings in here... 
f = "$('#widgetHolCalendar').DatePickerSetDate(['11/05/2013', '11/12/2013'], true);" 
IE.document.parentWindow.execScript f, "jscript" 

'trigger the page function which refreshes the table 
f = "historical_submit();" 
IE.document.parentWindow.execScript f, "jscript" 
+0

谢谢蒂姆,它对我很好,我花了几个星期的时间来解决这个问题! – MeSS83

1

这是一个可定制的代码示例我的建议上面:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Playing with Data in a Date range</title> 
    <!-- load the jQuery CSS first. The order is important here. Css first, then jQuery, then ui and at last plugins 
    remove the ".min" if you are on development environment (this let gets you human readable JS Lib Code) --> 
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script> 
    <!-- load the datables plugin --> 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     var oTable; 

     function applyChangesAction(sender) 
     { 
      // TODO: Retrieve the data from the server and render it into the table 
      // they are many ways to do it with datatables. Here is a good 
      // discussion about it: http://datatables.net/forums/discussion/comment/26734 

      // after doing that you should be able to refresh the whole table by simply calling this below: 
      //oTable.fnReloadAjax(); 
     } 

     $(document).ready(function() 
         { 
          // >>> BEGIN DATEPICKER LOGIC <<< 
          var iOneDayInMs = 60 * 1000 * 60 * 24; 

          $("#dtPickTo").datepicker(
                { 
                 "setDate": new Date(), 
                 "maxDate": new Date() 
                } 
              ); 
          $("#dtPickFrom").datepicker(
                 { 
                  "maxDate": new Date($("#dtPickTo").datepicker("getDate").getTime() - iOneDayInMs) 
                 } 
              ); 
          // >>> END DATEPICKER LOGIC <<< 

          // >> BEGIN DATATABLES LOGIC << 
          oTable = $("#dataTabPrices").dataTable(
                    { 
                     "bSortClasses": false, 
                     "bProcessing": true, 
                     "bServerSide": true, 
                     "sAjaxSource": "http://path/to/your/json/return_script.php", 
                     "fnServerData": function(sSource, aoData, fnCallback) 
                         { 
                          // put your filter criteria data in this object below to get the requested results: 
                          var oaData = { "dtPickFrom": $("#dtPickFrom"), 
                              "dtPickTo": $("#dtPickTo") 
                             }; 
                          $.ajax(
                            { 
                            "dataType": "json", 
                            "type": "POST", 
                            "url": sSource, 
                            "data": JSON.stringify(aoData), // your data filtering criteria for the server side script to return only requested data 
                            "success": fnCallback // closure callback if all data is received and ready to be rendered into the table (refresh logic for button click) 
                            } 
                          ); 
                         } 
                    } 
               ); 
          // >> END DATATABLES LOGIC << 
         } 
        ); 
    </script> 
</head> 

<body> 
    <p> 
     <label for="dtPickFrom">From</label><input type="text" id="dtPickFrom" name="dtPickFrom" /><br /> 
     <label for="dtPickTo">Until</label><input type="text" id="dtPickTo" name="dtPickTo" /> <br /> 
     <input type="button" id="btnApplyChanges" name="btnApplyChanges" value="Apply" onClick="applyChangesAction(this);" /> 
    </p> 
    <hr /> 

    <table id="dataTabPrices"> 
     <thead> 
      <tr> 
       <th>Date</th> 
       <th>Last</th> 
       <th>Open</th> 
       <th>High</th> 
       <th>Low</th> 
       <th>Change &#37;</th> 
      </tr> 
     </thead> 
     <tbody> 
     </tbody> 
    </table> 
</body> 
</html> 

这还没完,还没有!这只是你应该怎么做的一个基本例子。阅读我的评论,看看到底是什么东西,让它充分发挥作用。我的代码已经过测试,可以工作到目前为止...

我希望这有助于!

+0

感谢您为此代码所花时间的alpham8我将尽力完成您的代码,我会让您知道,这非常有趣,再次感谢! – MeSS83

+0

:-)没问题。这让我适应JS编码;-) 如果这些库的许可证与您的使用兼容,您还应该看看。 我也忘了提及,这个解决方案只适用于胖客户端,而不适用于瘦客户端。在瘦客户端中,我会将数据工作逻辑外包给服务器。所以这段代码可能会在智能手机和其他移动设备上造成很长的执行时间。我其实并不知道你的终端设备的范围。如果你的示波器是一台普通的电脑或平板电脑,那么这个代码就可以。但对于瘦客户端,您应该将一些代码从JS外包给PHP。 – alpham8