gpt4 book ai didi

javascript - VBA for Excel,如何加载 .js 文件作为 Jscript Scriptcontrol 对象

转载 作者:行者123 更新时间:2023-12-01 03:34:05 24 4
gpt4 key购买 nike

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

我尝试改编以下借自 here 的代码,但是编译后的库太大,无法插入到Vba代码中。

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

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

更新。

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

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

有什么想法吗?

最佳答案

将库从文件系统读取到字符串中:

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

关于javascript - VBA for Excel,如何加载 .js 文件作为 Jscript Scriptcontrol 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44376533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com