VBA for Excel,如何将.js文件作为Jscript Scriptcontrol对象加载 [英] VBA for Excel, how to load a .js file as Jscript Scriptcontrol object

查看:653
本文介绍了VBA for Excel,如何将.js文件作为Jscript Scriptcontrol对象加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道将js加载到VBA有点傻,但我需要加载Google的libphonenumber库来对大量电话号码进行分析。

I know it's a bit silly to load a js into VBA, but I need to load the libphonenumber library by Google to perform an analysis of a big bunch of phone numbers.

我尝试调整从此处借来的以下代码,但是编译好的库要插入到Vba代码。

I tried to adapt the following code borrowed from here, but the compiled library is to big to be inserted into the Vba code.

有没有办法从文件中加载.js库?
谢谢!

Is there any way to load the .js library from a file instead? Thanks!

Function encodeURL(str As String)
   Dim ScriptEngine As ScriptControl
   Set ScriptEngine = New ScriptControl

   ScriptEngine.Language = "JScript"
   ScriptEngine.AddCode "function encode(str) {return encodeURIComponent(str);}"
   Dim encoded As String

   encoded = ScriptEngine.Run("encode", str)
   encodeURL = encoded
End Function



UPDATE。



这应该是一个有效的代码,但由于某种原因不起作用:

UPDATE.

This should be a working code, but for some reason doesn't works:

Function loabdjs(x As String)

    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject

    Dim script As String
    Dim fs As Scripting.TextStream

    ''' CODE : "function encode(str) {return encodeURIComponent(str);}"
    Set fs = fso.OpenTextFile("test.js", ForReading, False)
    MsgBox ("Never reached this point")


    script = fs.ReadAll

    fs.Close

    Dim ScriptEngine As ScriptControl
    Set ScriptEngine = New ScriptControl

    Dim output As String

    ScriptEngine.Language = "JScript"
    ScriptEngine.AddCode script

    output = ScriptEngine.Run("encode", x)
    loadjs = output
End Function

任何想法?

推荐答案

将文件系统中的库读入字符串:

Read the library from the filesystem into a string:

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject

Dim fs As Scripting.TextStream
Set fs = fso.OpenTextFile( "libphonenumber.js", ForReading, False )

Dim script As String
script = fs.ReadAll

fs.Close()

scriptEngine.AddCode script

这篇关于VBA for Excel,如何将.js文件作为Jscript Scriptcontrol对象加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆