通过 Excel VBA 调用 javascript 文件 (.js)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/341112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:05:12  来源:igfitidea点击:

Calling a javascript file (.js) via Excel VBA?

javascriptexcelvbaexcel-vba

提问by Cheeso

How to call a javascript file (.js) via Excel VBA?

如何通过 Excel VBA 调用 javascript 文件 (.js)?



So as i am opposed to the same kind of problem i'll try to submit you guys my case.

因此,由于我反对同类问题,因此我将尝试向你们提交我的案例。

I am trying to automate datas extraction from valeo's catalogue using excel vba macro.

我正在尝试使用 excel vba 宏从 valeo 的目录中自动提取数据。

I have a list of références attached to valeo's automotive products (huge list, as more than 3000 thousands items). And i would like to import directly informations from the catalogue wich seems to run under javascript.

我有一份附在 valeo 汽车产品上的 références 清单(清单很大,超过 300 万件)。我想直接从似乎在 javascript 下运行的目录中导入信息。

The datas i need is the list of every vehicules attached to a reference.

我需要的数据是附加到参考的每辆车的列表。

Here is the url: http://outcat-cs.tecdoc.net/ows/en/7FA2A0C501BC34CA4BECB04095663CF1.ows_cs2.srv?view=VIndexFramesetJsp

这是网址:http: //outcat-cs.tecdoc.net/ows/en/7FA2A0C501BC34CA4BECB04095663CF1.ows_cs2.srv?view=VIndexFramesetJsp

I'd like to access to the "Direct Article Search" tab, in order to copy a reference directly from an excel tab's cell and then simulate a clic on the reference in order to display the "linked vehicules section" and then to copy them in a new excel sheet.

我想访问“直接文章搜索”选项卡,以便直接从 Excel 选项卡的单元格复制引用,然后模拟对引用的点击以显示“链接车辆部分”,然后复制它们在新的 Excel 表格中。

I already succeede in doing this with html pure programmed webpage (oscaro.com) using the following code :

我已经使用以下代码通过 html 纯编程网页(oscaro.com)成功地做到了这一点:

Set maPageHtml = IE.document
Set Helem = maPageHtml.getElementsByTagName("input")

For i = 0 To Helem.Length - 1 
    If Helem(i).getAttribute("name") = "toFind" Then Helem(i).Value = "819971" '819971 is the valeo reference searched
    If Helem(i).getAttribute("name") = "submit" Then Set Monbouton = Helem(i)
Next

Monbouton.Click 'this does the click on my button Monbouton

But this technique can't be used with valeo website since I am not able (or at least I don't know yet how to do it) to select/click a button when the page is made on javascript, since it doesn't have a name, value or id for the button.

但是这种技术不能与 valeo 网站一起使用,因为我无法(或者至少我不知道如何去做)当页面是在 javascript 上创建时选择/单击一个按钮,因为它没有具有按钮的名称、值或 id。

Also it seems that the url in the address field is the same before clicking on the "Direct Article Search" button and after having clicked....

此外,在单击“直接文章搜索”按钮之前和单击之后,地址字段中的 url 似乎相同。

Hope i am clear enought in spite of my english...

希望我足够清楚,尽管我的英语...

Greetings

你好

回答by Cheeso

All the previously suggested approaches sound hacky to me.

以前建议的所有方法对我来说都听起来很笨拙。

For a more reliable solution, embed the Javascript in a COM component via Windows Script Components, and call the Javascript-based COM component as you would any other COM component.

要获得更可靠的解决方案,请通过Windows 脚本组件将 Javascript 嵌入 COM组件中,并像调用任何其他 COM 组件一样调用基于 Javascript 的 COM 组件。

回答by Dirk Vollmar

I don't think that there is a direct way to run JavaScript code in VBA.

我认为在 VBA 中没有直接运行 JavaScript 代码的方法。

What you could try to do is to embed the JavaScript code in an HTML form which you could open in a (hidden) browser control. Then fill the form controls via the DOM of the browser control and submit the form. The submit triggers the JavaScript function that you want to call.

您可以尝试做的是将 JavaScript 代码嵌入到您可以在(隐藏的)浏览器控件中打开的 HTML 表单中。然后通过浏览器控件的DOM填充表单控件并提交表单。提交会触发您要调用的 JavaScript 函数。

Sample (not tested):

样品(未测试):

VBA:

VBA:

oIE.Document.frmMain.param1.Value = 5
oIE.Document.frmMain.param2.Value = "6"
oIE.Document.frmMain.submit.click ' this line will call the JavaScript function

HTML:

HTML:

<div id="submit" > <a href="javascript:doAction();">Do Action</a></div>

<script>
function doAction()
{
    // do whatever the code should do
}
</script>

回答by Will Rickards

You mean through windows scripting host? You can shell (use the shell command) out to wscript.exe with the name of the .js file. But this javascript object model won't be like the one you get in the browser. It would be helpful if you told us what the javascript was supposed to do.

你的意思是通过windows脚本主机?您可以使用 .js 文件的名称将外壳(使用 shell 命令)输出到 wscript.exe。但是这个 javascript 对象模型不会像你在浏览器中得到的那样。如果您告诉我们 javascript 应该做什么,那将会很有帮助。