最近在使用pd过程中,遇到一个问题,就是类的字段,方法,类型在excel中整理好了,想导入到pd直接生成类图。网上有很多生成实体表的方法,于是自己模仿写了一个生成类图的,在pd中的工具--扩展--脚本,或者直接快捷键shift + ctrl + X 打开脚本窗口,执行以下代码即可
Option Explicit
Dim mdl ‘
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
End If
Dim HaveExcel
Dim RQ
RQ = vbYes ‘MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")
If RQ = vbYes Then
HaveExcel = True
‘ Open & Create Excel Document
Dim x1 ‘
Set x1 = CreateObject("Excel.Application")
x1.Workbooks.Open "C:\Users\lenovo\Desktop\新建 Microsoft Excel 工作表.xlsx" ‘指定excel文档路径
x1.Workbooks(1).Worksheets("Sheet1").Activate ‘指定要打开的sheet名称
Else
HaveExcel = False
End If
a x1, mdl
sub a(x1, mdl)
dim rwIndex
dim tableName
dim colname
dim table
dim col
dim count
on error Resume Next
set table = mdl.classes.CreateNew ‘创建一个类图集合
For rwIndex = 1 To 1000
With x1.Workbooks(1).Worksheets("Sheet1")
if rwIndex=1 then
table.Name = .Cells(rwIndex, 2) ‘指定类名
table.Code = .Cells(rwIndex, 1) ‘指定类代码名
else
If .Cells(rwIndex, 1).Value = "" Then
Exit For
End If
set col = table.Attributes.CreateNew ‘创建一列/字段
col.Name=.Cells(rwIndex, 2).Value ‘指定列名
col.Code = .Cells(rwIndex, 1).Value ‘指定列名
col.DataType = .Cells(rwIndex, 3).Value ‘指定列数据类型,还可以指定其他属性
end if
End With
Next
count = count + 1
Exit Sub
End sub