【问题标题】:Run javascript function on cells of CSV / excel file在 CSV / excel 文件的单元格上运行 javascript 函数
【发布时间】:2015-03-28 05:44:25
【问题描述】:

我有a javascript function here(查看页面源代码),它按照字符替换算法将文本框的所有简单文本内容转换为 Unicode Devnagri 脚本,然后输出到另一个文本框。我想把它带到 Excel / CSV 文件中,对于所选的单元格或单元格范围,每个单元格的内容应该由这个函数处理。

有没有办法将 javascript 函数导入 excel 并在那里运行?像宏或其他东西:如果我必须将其转换为 VBA 代码,那么该怎么做?或者有没有办法将 CSV 文件导入 javascript,在每个单元格上运行该函数并生成输出 CSV?

附加信息:该用例用于将使用传统印地语字体的文档(ASCII 字符的字体是印地语字符..如果您没有安装字体,那么您会看到乱码)转换为无需特殊字体文件即可看到的 Unicode。由于特殊的字符连接,字符替换有点复杂,Devnagri 脚本的元音(maatras)可以为辅音加前缀或后缀,因此需要 Javascript 函数。简单的查找-替换操作不起作用。

编辑:在尝试提出的解决方案后,我在 64 位 Office2010 安装中点击了this roadblock:“scriptcontrol 仅是 32 位组件,不会在 64 位进程中运行”。

您能说明如何在 OpenOffice 中执行此操作吗?还是谷歌文档/电子表格?或者这可以通过在网页中导入和处理 CSV/XLS 来完成吗?

【问题讨论】:

    标签: javascript excel vba csv


    【解决方案1】:

    您可以像这样在 VBa 中创建 javascript 函数。

    ** 复制自:How can I URL encode a string in Excel VBA? ** 解决方案:ozmike ** 添加 Microsoft Script Control 作为参考。

    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
    

    编辑:17.04.15

    这是与上述类似的解决方案,只是它使用了一个 Web 浏览器控件,该控件运行 javascript 并为您提供结果。

    基本思路是这样的:

    1) 我们将本地 html 文件上传到 webbrowser 控件。 html 文件中包含 javascript 函数。

    html文件也有两个元素:

    <p id='data'>_DATA_</p>
    <p id='result'></p>
    

    第一个将保存您想要编码(或采取行动)的数据。 DATA 是一个占位符。使用 vba 代码,我们将替换那个刺 用你想要的数据。

    之后,html文件中的javascript会读取第一个元素中写入的内容,并将结果输出到第二个元素。 发生这种情况后,使用 VBA 代码将从第二个元素读取结果。

    说明:

    1) 创建一个名为 c:\temp.html 的空文本文件并将以下 html 代码粘贴到其中:

    <!-- saved from url=(0014)about:internet -->
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function encode1()
    {
      var sData;
    
      // put the "input" to a variable.
      sData = document.getElementById('data').innerHTML;
    
      // do stuff with the data variable, for example
      sData = encodeURIComponent(sData);
    
      // write the manipulated data to the "output"
      document.getElementById('result').innerHTML = sData;
    }
    
    </script>
    </head>
    <body onload='encode1();'>
    <p id='data'>_DATA_</p>
    <p id='result'></p>
    </body>
    </html>
    

    在 vba 编辑器中:

    2) 插入用户表单

    3) 向表单添加一个 WebBrowser 控件。

    在用户表单中添加以下代码:

    Option Explicit
    
    Public Sub MyEncode(str As String)
        Const TEMPLATE_HTML As String = "c:\temp.html"
    
        Const OUTPUT_HTML As String = "c:\tempWithData.html"
    
        Dim s As String
    
        s = ReadAllFile(TEMPLATE_HTML)
    
        s = Replace(s, "_DATA_", str)
    
        Call PrintToFile(OUTPUT_HTML, s)
    
    
        ' Load the HTML file to the WebBrowser control
        UserForm1.WebBrowser1.Navigate OUTPUT_HTML
        ' wait for it to finish
        While UserForm1.WebBrowser1.ReadyState <> READYSTATE_COMPLETE
            DoEvents
        Wend
        ' wait some more
        While UserForm1.WebBrowser1.Busy
            DoEvents
        Wend
    
        ' find the html element that holds the result data and put the value
        ' to the global variable.
        g_result = (WebBrowser1.Document.getElementById("result").innerHTML)
    
        ' unload the form
        Unload Me
    End Sub
    

    最后,添加一个模块并将以下代码放入其中:

    Option Explicit
    
    ' this global variable will hold the returning result from the html file
    Public g_result As String
    
    ' !!!! RUN THIS PROCEDURE !!!
    ' This should work fast enough so that you will not see the 
    ' form opening and closing.
    Public Sub TestEncode()
        Dim sResult As String
    
        Load UserForm1
    
        ' This runs the web browser control
        Call UserForm1.MyEncode("https://stackoverflow.com/")
    
        ' The result will be: http%3A%2F%2Fstackoverflow.com%2F
        MsgBox g_result
    
        ' Last remark: you can of course run the above using data from
        ' the sheet, for example:
        ' Call UserForm1.MyEncode(Range("A1").Text)
    End Sub
    
    ' a helping method- read contents from a file.
    Public Function ReadAllFile(fname As String) As String
        Dim sFileData As String
        Dim FileLength As Long
        Dim iFree As Integer
    
        iFree = FreeFile
    
        Open fname For Input As iFree    ' Open file for input.
    
        sFileData = Input(LOF(iFree), iFree) ' read all the file
    
        Close iFree    ' Close file.
    
        ReadAllFile = sFileData
    End Function
    
    ' a helping method- write contents to a file.
    Public Sub PrintToFile(fname As String, sFileData As String)
        Dim iFree As Integer
    
        iFree = FreeFile
    
        Open fname For Output As iFree    ' Open file for input.
    
        Print #iFree, sFileData
    
        Close iFree    ' Close file.
    End Sub
    

    【讨论】:

    • 嗨!试过了。我被困在“Dim ScriptEngine As ScriptControl”行。弹出提示“编译错误:未定义用户定义类型”
    • 我认为这是问题所在:stackoverflow.com/questions/9725882/…:“遗憾的是,scriptcontrol 只是一个 32 位组件,不会在 64 位进程中运行”>> 这太疯狂了!
    • 这真是个坏消息。你的需要是什么?意思是,你想用 ScriptControl 达到什么目的?
    • 我已经编辑了我的答案。让我知道这是否能让你更接近你的目标。
    猜你喜欢
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多