Javascript 使用VBA在网页中的脚本函数之间传递数组

Javascript 使用VBA在网页中的脚本函数之间传递数组,javascript,html,css,excel,vba,Javascript,Html,Css,Excel,Vba,嗨,我正在尝试从网页导入数据 我想做的是一个宏,导航到此网页并从日历中选择不同的日期范围 我不明白如何在日历中设置日期。我可以打开它,但我无法“复制”鼠标点击来选择日期 到目前为止,我开发的代码是: Sub Problem() Dim IE As Object Set IE = CreateObject("InternetExplorer.application") IE.Visible = True IE.navigate "http://www.investing.com/indices/u

嗨,我正在尝试从网页导入数据 我想做的是一个宏,导航到此网页并从日历中选择不同的日期范围

我不明白如何在日历中设置日期。我可以打开它,但我无法“复制”鼠标点击来选择日期 到目前为止,我开发的代码是:

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
代码没有给出错误消息,但没有任何更改,因此我不确定代码是否真的在执行某些操作。如果我正确理解了代码,要设置新日期,我必须使用2个参数调用DatePickerSetDate

DatePickerSetDate(date, shifTo)
其中date是由2个元素组成的数组,shifTO是布尔值。我不知道如何使用vba将数组传递给脚本

另外,当我调用函数DatePickerGetDate时,我希望得到结果并保存在vba数组中


有人能帮忙吗?

下面是一个关于我的上述建议的可自定义代码示例:

<!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>

播放日期范围内的数据
可变的;
函数applyChangesAction(发送方)
{
//TODO:从服务器检索数据并将其呈现到表中
//使用数据表有很多方法
//关于它的讨论:http://datatables.net/forums/discussion/comment/26734
//完成此操作后,您只需调用以下命令,即可刷新整个表:
//fnReloadAjax();
}
$(文档).ready(函数()
{

//>>>BEGIN DATEPICKER LOGIC>END DATEPICKER LOGIC BEGIN DATATABLES LOGIC>END DATATABLES LOGIC下面是一个关于我的上述建议的可自定义代码示例:

<!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>

播放日期范围内的数据
可变的;
函数applyChangesAction(发送方)
{
//TODO:从服务器检索数据并将其呈现到表中
//使用数据表有很多方法
//关于它的讨论:http://datatables.net/forums/discussion/comment/26734
//完成此操作后,您只需调用以下命令,即可刷新整个表:
//fnReloadAjax();
}
$(文档).ready(函数()
{
//>>>BEGIN DATEPICKER LOGIC>END DATEPICKER LOGIC BEGIN DATATABLES LOGIC>END DATATABLES LOGIC这对我很有效(从页面完全加载时开始拾取…)

这对我来说很有效(从页面完全加载时开始拾取…)


如果我理解的没错,你想用VBA开发一个IE插件吗?好吧,在你的问题上,点击很简单:只需在当前的焦点选项卡(应该是你刚刚打开的页面)中插入JavaScript代码即可。我认为使用IE.Document.parentWindow.execScript可以将JS代码插入当前选项卡。您还可以将JS代码分为内容脚本和背景脚本(=在本例中为VBA应用程序)。我想你应该了解更多关于如何开发浏览器加载项的知识。对不起,我不太清楚。我想做的是使用excel中的宏更改该页面日历中的日期。我不想开发加载项。至于在execScript中使用JS代码,你能举一个简单的例子说明如何做类似的事情吗?哦,是这样的rry,我误解了你。只有在浏览器扩展上下文中,你才能在JS中使用execScript函数。过去我也使用VBA为MS Office开发了宏。我从来没有听说过在VBA代码中使用JavaScript的方法。我认为你混淆了两个不同的东西。如果你在这一页上找到JS代码,那就不会与Excel VBA宏代码有关…你指的是什么日历?这类似于从传递的输入数据绘制图表,在Excel中抛出向导吗???如果打开网页,表格上方会有一个日期范围框:如果单击该框,可以选择不同的日期范围,而不是2013年12月16日-2014年1月15日您需要2013年11月16日-2014年1月15日。一旦您选择了日期的新范围,您可以使用“应用”按钮重新加载表格。我需要做的是使此过程自动化。我需要找到一些方法来选择新的日期范围。好的,现在。因此,您的逻辑目前无法正确运行。我知道我的建议违反规则,但是:有你有没有想过不使用VBA而只在jQuery和jQuery UI框架中使用JavaScript?有了它(以及jQuery datatables插件),你就可以做所有你想做的事情。日期范围有两个日期选择器,datatables插件(www.datatables.net)对于表中的数据,将显示一个带有onclick事件的应用按钮。使用datatables,您可以在周期或用户触发的事件中使用POST参数重新加载数据内容,以获得正确的输出。如果我理解您的意思是对的,您希望使用VBA开发IE附加组件吗?好吧,对于您的问题,单击也很简单:只需在当前的焦点选项卡(应该是您刚刚打开的页面)中插入JavaScript代码。我认为使用IE.Document.parentWindow.execScript,您可以将JS代码插入当前选项卡。您还可以分为内容脚本和背景脚本(=在本例中为您的VBA应用程序)。我想你应该了解更多关于如何开发浏览器加载项的知识。对不起,我不太清楚。我想做的是使用excel中的宏更改该页面日历中的日期。我不想开发加载项。至于在execScript中使用JS代码,你能举一个简单的例子说明如何做类似的事情吗?哦,是这样的rry,我误解了你的意思。只有在浏览器扩展上下文中,你才能在JS中使用execScript函数。过去我也使用VBA为MS Office开发了宏。我从来没有听说过任何方法
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"