Created
November 3, 2016 13:56
-
-
Save linkin-park/0c6be72585679628817949be7112a835 to your computer and use it in GitHub Desktop.
Update 1 - VBA , update the logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function generateObject() As RowCell | |
Set rfirst = Selection.Rows.Item(1) ' initialized to first row | |
Dim cell As Variant | |
Dim count As Integer | |
Dim dict As Variant | |
Set dict = CreateObject("Scripting.Dictionary") ': dict.CompareMode = vbTextCompare | |
count = 0 | |
Debug.Print "total Rows in Selection " & Selection.Rows.count | |
Dim tempRowCell As RowCell, theParentRowCell As RowCell | |
' for each cell in selection selected | |
For Each cell In Selection | |
' LBound(Selection.Rows) | |
Debug.Print cell.Row & "," & cell.Column | |
' no parent for the first row and a single cell , added to dictionary | |
If cell.Row = rfirst.Row And cell.Value <> "" Then | |
Set tempRowCell = New RowCell: Set theParentRowCell = tempRowCell | |
tempRowCell.name = cell.Value | |
Set tempRowCell.childRowCell = New Collection | |
tempRowCell.rowcol = cell.Row & "," & cell.Column | |
tempRowCell.cnt = tempRowCell.cnt + 1 | |
dict.Add cell.Row & "," & cell.Column, tempRowCell | |
Set temp = dict(cell.Row & "," & cell.Column) | |
''MsgBox temp.name | |
' If not first row in selection and not empty go to Else | |
ElseIf cell.Value <> "" Then | |
'MsgBox "total" & dict.count | |
''' if not first row come here | |
Set tempRowCell = Nothing | |
Set tempRowCell = New RowCell | |
With tempRowCell | |
.name = cell.Value | |
.cnt = tempRowCell.cnt + 1 | |
.rowcol = cell.Row & "," & cell.Column | |
Set tempRowCell.childRowCell = New Collection | |
End With | |
Dim parentTemp As RowCell, parentStr As String | |
parentStr = cell.Row - 1 & "," & cell.Column '': MsgBox parentStr | |
If dict.Exists(parentStr) Then | |
Set parentTemp = dict(parentStr) '': MsgBox parentTemp.name | |
parentTemp.addCollection tempRowCell | |
'DictParentCellRetrieved.Add tempRowCell | |
Else | |
''MsgBox parentStr & " does nt exist" | |
End If | |
dict.Add cell.Row & "," & cell.Column, tempRowCell | |
' outer else , when no value in any row common task | |
Else | |
tempRowCell.cnt = tempRowCell.cnt + 1 | |
dict.Add cell.Row & "," & cell.Column, tempRowCell | |
End If | |
Next | |
'' release the resources | |
Set rfirst = Nothing | |
Set dict = Nothing | |
Set tempRowCell = Nothing | |
Set temp = Nothing | |
Set parentTemp = Nothing | |
parentStr = "" | |
'' end of releasing the resources | |
Set generateObject = theParentRowCell | |
'MsgBox tempRowCell.cnt & " item(s) selected" | |
End Function | |
Function getXSD() | |
With Application | |
.ScreenUpdating = False | |
.DisplayStatusBar = False | |
.EnableEvents = False | |
End With | |
ActiveSheet.DisplayPageBreaks = False | |
Dim parentRowCell As RowCell, str As String | |
Set parentRowCell = generateObject() | |
str = generateXSD(parentRowCell) | |
str = str & "</xs:schema>" & vbNewLine | |
''Debug.Print " EveryThing , " & parentRowCell.name | |
''Debug.Print "": Debug.Print "": Debug.Print "": Debug.Print "": Debug.Print "": Debug.Print "": Debug.Print "" | |
Debug.Print str | |
End Function | |
Function generateXSD(ByVal Rcell As RowCell, Optional str As String) As String | |
'' Dim strXSD As String | |
'' if Rcell.childRowCell.Count | |
If Len(str) >= 0 Then | |
str = Trim(str) | |
If Len(str) = 0 Then | |
str = vbNewLine & vbNewLine & "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>" & vbNewLine _ | |
& "<xs:schema xmlns:xsd=" & Chr(34) & "http://www.w3.org/2001/XMLSchema" & Chr(34) & ">" & vbNewLine | |
End If | |
End If | |
Dim tempStr As String | |
If Rcell.childRowCell.count = 0 Then ' leaf | |
tempStr = "<xsd:element " & "name=" & Chr(34) & Rcell.name & Chr(34) & " type=" _ | |
& Chr(34) & "xsd:text" & Chr(34) & " minOccurs=" & Chr(34) & "0" & Chr(34) & " maxOccurs=" & Chr(34) & "unbounded" & Chr(34) & " />" & vbNewLine | |
'generateXSD = "" ,for debug purpose set | |
generateXSD = tempStr | |
tempStr = vbEmpty | |
Exit Function | |
End If | |
str = str & "<xsd:element " & "name=" & Chr(34) & Rcell.name & Chr(34) & ">" & vbNewLine _ | |
& "<xsd:complexType>" & vbNewLine _ | |
& "<xsd:sequence>" & vbNewLine | |
For Each rce In Rcell.childRowCell | |
Debug.Print str | |
str = str & generateXSD(rce, str) | |
Next | |
str = str & " </xsd:sequence>" & vbNewLine _ | |
& "</xsd:complexType>" & vbNewLine _ | |
& "</xsd:element>" & vbNewLine | |
generateXSD = str | |
End Function | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment