如何从正则表达式生成随机文本以填充Excel单元格

时间:2018-02-28 17:24:51

标签: excel excel-vba excel-2016 vba

我在很多列中创建了大量具有不同值的Excel测试文件。

(Windows 7 - 64位系统(如果相关))。

稍后将使用其他未知工具将这些文件导入数据库,因此我只需准备这些填充有效数据的文件。

每个文件都有不同的列,因此每列需要不同的有效字符范围。

我想做什么?

  

使用公式女巫接收正则表达式并生成/并填充单元格   使用基于该正则表达式的随机字符串,如果可能,指定如何   字符串应该是多个字符。

像这个网站的内容:https://www.browserling.com/tools/text-from-regex

例如:对于下一列,我想用随机文本填充单元格,如下所示:

> 
> -------------------------------------------------------------------
> | Name      | Email          | Date  | URL               | Price  |
> -------------------------------------------------------------------
> | AHygsyub  | xyz@uygsh.hyu  | 1956  | http://iadif.jyf  | 245.75 |
> -------------------------------------------------------------------

在公式栏中使用类似的内容:

> =fillCellWith('([a-z]{3,10} ){1,10}')              //For Name column
> =fillCellWith('\d{4}')                             //For Date column
> =fillCellWith(RegexPattern)                        //Etc etc

有人可以指导我创建VBA函数来实现此目的吗?

我没有VB编程的经验,但我可以想象:

Public Function fillCellWith(MyRegex As String) As String
Dim regEx As New RegExp
Dim Rand As String

'Analize the pattern and create random string satisfying the pattern needs to be valid
'Merge all parts and return as a result in Rand


End Function

1 个答案:

答案 0 :(得分:1)

知道了!使用Java语言+ VBA Excel 并基于以下链接:

Microsoft Excel Macro to run Java program

https://www.ozgrid.com/forum/forum/help-forums/excel-general/142069-calling-and-executing-a-java-program-from-excel-vba-macro

Here我发现很多库已根据正则表达式生成值。

我选择this进行测试。

为什么?,我使用Javascript库进行测试,但我发现与VBA一起使用非常复杂,Phyton库不那么复杂,但是当我看到Java的一个例子时很有意思。

我做的是:

1 - 从here

下载带有依赖关系的generex.jar版本1.0.2

2 - 创建一个带有Main类的Java项目,在我的情况下 RegexToValue ,使用以下代码:

import com.mifmif.common.regex.Generex;

//args[0] receive regex to init Generex Object
public class RegexToValue {
    public static Generex generex;
    public static int attempts = 0;

    public static void main(String[] args) {
        if (args.length > 0) {
            generex = new Generex(args[0]);
        } else {
            generex = new Generex("[0-3]([a-c]|[e-g]{1,2})"); //default init
        }
        generateValue();
    }

    public static void generateValue() {
        try {
            System.out.print(generex.random());
        } catch(Error e) {
            if (attempts <= 3) {
                attempts++;
                generateValue();
            } else {
                System.out.print("Error");
            }
        }
    }
}

3 - 将从步骤1下载的两个.jars导入项目(Libraries - &gt; Add JAR file)

4 - 使用步骤3中导入的依赖项生成项目的JAR文件,所有这些都在一起

5 - 为简单起见,我将生成的JAR和Lib文件夹放入C:\ Generex \

6 - 创建为宏启用的Excel文件

7 - 创建模块 (Alt + F11) - &gt;菜单插入 - &gt;模块 ,代码如下:

#If VBA7 Then
    '64 Bits
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    '32 Bits
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Public Function fillCellWith(regxPattern As String)
    Dim prog As Object

    '// pass the program name to java on command line, and pass any arguments after.
    Set prog = CreateObject("WScript.Shell").exec("javaw -jar ""C:\Generex\RegexToValue.jar"" " & regxPattern)
    While prog.Status = 0
        Sleep 1000 '// put thread to sleep for 1 sec.
    Wend
    fillCellWith = prog.StdOut.ReadAll
End Function

8 - 在单元格中使用这样的公式:

=fillCellWith("(blue|red|yellow|green)\.(oak|cedar|willow)\@(yahoo\.co\.uk|google\.com|example\.org)")

9 - 查看结果 =)