从VBA调用JavaScript文件

时间:2014-05-10 11:27:35

标签: javascript excel vba excel-vba

如果我想将业务逻辑编写到javascript文件中并在将它们处理到Web作者之前从Excel VBA测试它们,则需要能够从VBA调用javascript。从其他Stack Overflow问题(比如解析JSON)我发现了Microsoft脚本控件,所以我一直在尝试这个但是我被卡住了。

我有一个javascript文件,我保存到'c:\ temp \ starthere.js',其中包含一个简单的javascript函数

     function DoubleMe(a) {
         return a * 2;
     }

然后我有一些VBA代码来创建Microsoft Script Control的实例,将文件加载到其中,然后尝试调用它。代码在下面。可以通过激活Excel VBA并粘贴代码来重新创建,但您需要引用Microsoft脚本控件(对于我C:\Windows\SysWOW64\msscript.ocx)。然后向下翻页到'Sub TestRunIncluded()',在那里您将看到我的中止尝试以使其正常工作。

目标是让代码调用javascript(虽然它存放在一个文件中)并返回传入的参数的两倍。

我真的希望这个工作,因为我有很多用Excel VBA编写的测试脚本(比复杂的东西更复杂!)。请帮忙。

    Option Explicit

    'code loosely based on 'http://stackoverflow.com/questions/5773683/excel-vba-parsed-json-object-loop

    'Tools->References->
    'MSScriptControl; Microsoft Script Control 1.0;  {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}; C:\Windows\SysWOW64\msscript.ocx

    'other libraries are late bound

    Private moScriptEngine As ScriptControl

    Public Property Get ScriptEngine()
        'factory
        If moScriptEngine Is Nothing Then
            Set moScriptEngine = New ScriptControl
            moScriptEngine.Language = "JScript"
            'moScriptEngine.AllowUI = True
        End If
        Set ScriptEngine = moScriptEngine
    End Property

    Public Sub AddJSFile(ByVal sJSFilename As String)
        Debug.Assert LCase$(Right$(sJSFilename, 3)) = ".js"
        Dim fso As Object 'Scripting.FileSystemObject
        Set fso = VBA.CreateObject("Scripting.FileSystemObject")

        Debug.Assert fso.FileExists(sJSFilename)
        Dim fil As Object 'Scripting.File
        Set fil = fso.GetFile(sJSFilename)

        Dim ins As Object 'Scripting.TextStream
        Set ins = fil.OpenAsTextStream(1)  '1=ForReading

        Dim sCode As String
        sCode = ins.ReadAll

        ins.Close
        Set ins = Nothing
        Set fil = Nothing
        Set fso = Nothing

        Debug.Assert Len(sCode) > 0
        ScriptEngine.AddCode sCode


    End Sub

    Sub TestAddJSFile()
        AddJSFile "c:\temp\starthere.js"
        '* c:\temp\starthere.js contains the following four lines, last line is just a newline (from Notepad)
        'function DoubleMe(a) {
        '    return a * 2;
        '}
        '<newline>

    End Sub

    '!Fellow StackOverflow users, press F5 on this Sub as this is entry point!
    Sub TestRunIncluded()
        TestAddJSFile
        Dim res
        Debug.Assert Not IsObject(ScriptEngine.Eval("var b=DoubleMe(3)"))
        'Debug.Print ScriptEngine.Eval("return DoubleMe(3) ")  'Errors 1018: "'return' statement outside of function"
        Debug.Print ScriptEngine.Eval("(function { DoubleMe(3) })")  'Errors 1005: "expected '('"
        'Debug.Assert IsObject(ScriptEngine.Run("(DoubleMe(3))"))
        'Debug.Assert IsObject(ScriptEngine.Eval("(DoubleMe(3))"))
        'Set res = ScriptEngine.Eval("DoubleMe(3);")
    End Sub

1 个答案:

答案 0 :(得分:1)

看起来答案很简单...... doh!

Debug.Assert ScriptEngine.Eval("DoubleMe(3)") = 6

这里资源良好http://support.microsoft.com/kb/184740