-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCellFunctions.vb
596 lines (542 loc) · 28.5 KB
/
CellFunctions.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
Imports System.Text
Imports System.Runtime.CompilerServices
''' <summary>
''' Functions and Methods used to process data related to
''' <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> objects.
''' </summary>
Public Module CellFunctions
''' <summary>
''' Copies the cell from one shape to another.
''' Optionally copies the entire row's contents.
''' </summary>
''' <param name="vsoSourceCell">The source cell.</param>
''' <param name="vsoDestShape">The destination shape.</param>
''' <param name="copyRow">Indicates whether or not to also copy the contents of the cell's parent row.</param>
''' <returns>
''' A reference to the newly copied cell.
''' </returns>
''' <remarks>
''' Copies by Formula, not by Result. Maintains locale settings.
''' </remarks>
''' <exception cref="Exceptions.UnsupportedSectionException">
''' Thrown if <paramref name="vsoDestShape" >vsoDestShape</paramref> does not support the section to which <paramref name="vsoSourceCell">vsoSourceCell</paramref> belongs.
''' </exception>
<Extension()> _
Public Function CopyCellToShape(ByVal vsoSourceCell As Visio.Cell, _
ByVal vsoDestShape As Visio.Shape, _
ByVal copyRow As Boolean) As Visio.Cell
Try
'Get all needed variables.
'Dim vsoDoc As Visio.Document = vsoSourceCell.Document
Dim vsoSourceShape As Visio.Shape = vsoSourceCell.Shape
Dim vsoSectionIndex As Visio.VisSectionIndices = vsoSourceCell.Section
Dim vsoSourceRowIndex As Short = vsoSourceCell.Row
Dim vsoRowTag As Visio.VisRowTags = vsoSourceCell.Shape.RowType(vsoSectionIndex, vsoSourceRowIndex)
Dim vsoRowName As String = Nothing
Dim vsoRowNameU As String = Nothing
Dim vsoColumnIndex As Short = vsoSourceCell.Column
Dim vsoCellName As String = vsoSourceCell.Name
Dim vsoDestCell As Visio.Cell
Dim vsoDestRowIndex As Short
Dim vsoRowPrefix As String = VisioPowerDevTools.ShapeFunctions.GetSectionPrefix(vsoSectionIndex)
'Make sure you're using a valid section based on shape-type
If Not vsoDestShape.SectionIsValidToAdd(vsoSectionIndex) Then
'Section is not supported...Throw appropriate exception.
Dim ex As New UnsupportedSectionException( _
"You cannot create section " & vsoSectionIndex.ToString & _
" in " & vsoDestShape.Name & _
" because it is type: " & vsoDestShape.Type)
Throw ex
End If
'Create section if it doesn't already exist.
If Not vsoDestShape.SectionExists(vsoSectionIndex, Visio.VisExistsFlags.visExistsAnywhere) Then
vsoDestShape.AddSection(vsoSectionIndex)
End If
'Since we are moving to a shape, not a section, we already know that the type of row and cell are valid for the section.
'Find out if the row is named or not.
Dim sectionType As VisioPowerDevTools.visSectionTypes = VisioPowerDevTools.ShapeFunctions.GetSectionType(vsoSectionIndex)
If _
sectionType = visSectionTypes.NamedNonConstantRowsAndCells OrElse _
sectionType = visSectionTypes.NamedOrUnnamedNonConstantRowsAndCells OrElse _
sectionType = visSectionTypes.NamedRowsConstantCells Then
'Row is Named, set variables.
vsoRowName = vsoSourceCell.RowName
vsoRowNameU = vsoSourceCell.RowNameU
'Create named row if it doesn't already exist.
If Not vsoDestShape.CellExistsU(vsoRowPrefix & vsoRowNameU, Visio.VisExistsFlags.visExistsAnywhere) Then
vsoDestRowIndex = vsoDestShape.AddNamedRow(vsoSectionIndex, vsoRowNameU, vsoRowTag)
'Keep any changes to locale name intact by setting "Name" in addition to "NameU"
vsoDestShape.Section(vsoSectionIndex).Row(vsoDestRowIndex).Name = vsoRowName
Else
vsoDestRowIndex = vsoDestShape.Cells(vsoRowPrefix & vsoRowName).Row
End If
Else
'Row is not named, just index.
'Create row if it doesn't already exist.
If Not vsoDestShape.RowExists(vsoSectionIndex, vsoSourceRowIndex, Visio.VisExistsFlags.visExistsAnywhere) Then
vsoDestRowIndex = vsoDestShape.AddRow(vsoSectionIndex, vsoSourceRowIndex, vsoRowTag)
Else
vsoDestRowIndex = vsoSourceRowIndex
End If
End If
'Test if the user wants to copy all of the row information, or just the cell info.
If copyRow Then
'Copy all of the cell formulas in the row.
'Replace references to the source shape with references to the dest shape.
For i = 0 To vsoSourceCell.Shape.RowsCellCount(vsoSectionIndex, vsoSourceRowIndex) - 1
vsoDestShape.CellsSRC(vsoSectionIndex, vsoDestRowIndex, i).FormulaForceU = _
vsoSourceShape.CellsSRC(vsoSectionIndex, vsoSourceRowIndex, i).FormulaU.Replace(vsoSourceShape.Name, vsoDestShape.Name)
'Maintain locale formulas
vsoDestShape.CellsSRC(vsoSectionIndex, vsoDestRowIndex, i).FormulaForce = _
vsoSourceShape.CellsSRC(vsoSectionIndex, vsoSourceRowIndex, i).Formula.Replace(vsoSourceShape.Name, vsoDestShape.Name)
Next
Else
vsoDestShape.CellsSRC(vsoSectionIndex, vsoDestRowIndex, vsoColumnIndex).FormulaForceU = _
vsoSourceCell.FormulaU
'Maintain locale formulas
vsoDestShape.CellsSRC(vsoSectionIndex, vsoDestRowIndex, vsoColumnIndex).FormulaForce = _
vsoSourceCell.Formula
End If
vsoDestCell = vsoDestShape.CellsSRC(vsoSectionIndex, vsoDestRowIndex, vsoColumnIndex)
Return vsoDestCell
Catch ex As Exception
'Rethrow any other exceptions.
Throw
End Try
End Function
#Region "Sort List"
''' <overloads>
''' Alphabetizes a list in a <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> (<paramref name="vsoCell">vsoCell</paramref>),
''' and properly resets any cell formula that is referencing the list by indexing it.
''' </overloads>
''' <summary>
''' Alphabetizes a list in a <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> (<paramref name="vsoCell">vsoCell</paramref>),
''' and properly resets any cell formula that is referencing the list by indexing it.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="delimiter">The delimiting character of the list.</param>
''' <param name="alwaysOnTop">A string which should always be placed on top of the list.</param>
''' <param name="sortListArgs">Arguments used to determine various sort options. <seealso cref="SortListArgs">SortListArgs</seealso>.</param>
''' <remarks>
''' Assumes that it has been passed a valid <paramref name="vsoCell">cell</paramref> containing a delimited list.
''' If <paramref name="alwaysOnTop">alwaysOnTop</paramref> contains a valid string, any duplicates of that string will be replaced,
''' regardless of the value of <paramref name="removeDuplicates">removeDuplicates</paramref>.
''' </remarks>
Public Function SortList(ByVal vsoCell As Visio.Cell, _
ByVal delimiter As Char, _
ByVal alwaysOnTop As String, _
ByVal sortListArgs As Integer) As Boolean
Try
Dim reverseAlpha As Boolean = Not ((sortListArgs And CellFunctions.SortListArgs.reverseAlpha) = 0)
Dim removeDuplicates As Boolean = Not ((sortListArgs And CellFunctions.SortListArgs.removeDuplicates) = 0)
Dim removeUnused As Boolean = Not ((sortListArgs And CellFunctions.SortListArgs.removeUnused) = 0)
'Get the raw list as a string so we can parse it.
Dim listAsString As String = VisioFormulaToString(vsoCell.Formula)
'Get the array of cells dependent on the list cell of the shape.
Dim cellsWhichIndexTheList As List(Of Visio.Cell) = GetIndexedCellReferences(vsoCell)
'An array which will hold the index which the referencing-cell is using.
Dim indiciesUsedByReferences(cellsWhichIndexTheList.Count) As Integer
'The dependent Cell
Dim vsoDependentCell As Visio.Cell
'The extracted Index from a shape represented as a string.
Dim myIntsAsString As String
'The new index for the shape to use in the lookup cell.
Dim newIndex As Integer
'Regex which only returns numbers
Dim numbersRegex As RegularExpressions.Regex = New RegularExpressions.Regex( _
"[0-9][0-9]? # All nums", _
RegularExpressions.RegexOptions.IgnoreCase _
Or RegularExpressions.RegexOptions.Multiline _
Or RegularExpressions.RegexOptions.IgnorePatternWhitespace _
Or RegularExpressions.RegexOptions.Compiled)
'Loop through each of the dependent cells.
For i = 0 To cellsWhichIndexTheList.Count - 1
'Get the cell.
vsoDependentCell = cellsWhichIndexTheList(i)
' Loop through the match collection to retrieve all
' matches and positions.
Dim mc As RegularExpressions.MatchCollection = numbersRegex.Matches(vsoDependentCell.Formula)
'Get the first match, ignore the rest.
myIntsAsString = numbersRegex.Match(vsoDependentCell.Formula).Value
indiciesUsedByReferences(i) = CInt(myIntsAsString)
Next
'Parse the list and get the individual items.
Dim origSplitList As List(Of String) = listAsString.Split(";").ToList
Dim keys As List(Of String)
'If we are removing unused items, this must be done differently.
If removeUnused Then
'Add an entry for each of the unique indicies referenced by other cells.
keys = New List(Of String)
For Each i As Integer In indiciesUsedByReferences.Distinct
keys.Add(origSplitList(i))
Next
Else
'If we aren't removing unused items, stick with the original.
keys = origSplitList
End If
If removeDuplicates Then
keys = keys.Distinct.ToList()
End If
' Sort the keys using the specified method.
If reverseAlpha = True Then
keys.Sort(Function(a, b) a.CompareTo(b) * -1)
Else
keys.Sort(Function(a, b) a.CompareTo(b))
End If
'Move specified string to top of list if directed.
If alwaysOnTop.Count > 0 Then
If keys.Contains(alwaysOnTop) Then
keys.RemoveAll(Function(x) x.Equals(alwaysOnTop))
keys.Insert(0, alwaysOnTop)
End If
End If
'A stringBuilder used to create the new list.
Dim newListBuilder As StringBuilder = New StringBuilder
' Loop over the sorted keys, excluding the final key.
For i = 0 To keys.Count - 2
'Add the value to the new list.
newListBuilder.Append(keys(i))
'Add the seperator.
newListBuilder.Append(";", 1)
Next
'Add the final value to the list, but don't add a seperator.
newListBuilder.Append(keys(keys.Count - 1))
'Set the cell to use the new formula.
vsoCell.Formula = StringToVisioFormula(newListBuilder.ToString)
Dim oldIndex As Integer
Dim formulaBuilder As New StringBuilder(50)
'Loop through each of the dependent cells.
For i = 0 To cellsWhichIndexTheList.Count - 1
'Get the cell.
vsoDependentCell = cellsWhichIndexTheList(i)
'Cast the string of integers to a useful index value.
oldIndex = indiciesUsedByReferences(i)
'Find the new index of the value.
newIndex = keys.IndexOf(origSplitList(oldIndex))
'Set the cell's formula, replacing our old index with a new one.
formulaBuilder.Append("INDEX(")
formulaBuilder.Append(newIndex.ToString)
formulaBuilder.Append(",")
formulaBuilder.Append(vsoCell.Name)
formulaBuilder.Append(")")
vsoDependentCell.FormulaForceU = formulaBuilder.ToString
'Clear the stringBuilder for reuse
formulaBuilder.Length = 0
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
#Region "Overloaded SortList Functions"
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c>;</c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c>""</c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><see cref="SortListArgs.removeDuplicates">removeDuplicate</see> + <see cref="SortListArgs.removeUnused">removeUnused</see></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell)
SortList(vsoCell, defaultDelimiter, defaultAlwaysOnTop, defaultArgs)
End Sub
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="delimiter">The delimiting character of the list.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c><paramref name="delimiter"/></c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c>""</c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><see cref="SortListArgs.removeDuplicates">removeDuplicate</see> + <see cref="SortListArgs.removeUnused">removeUnused</see></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell, ByVal delimiter As Char)
SortList(vsoCell, delimiter, defaultAlwaysOnTop, defaultArgs)
End Sub
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="delimiter">The delimiting character of the list.</param>
''' <param name="alwaysOnTop">A string which should always be placed on top of the list.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c><paramref name="delimiter"/></c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c><paramref name="alwaysOnTop"/></c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><see cref="SortListArgs.removeDuplicates">removeDuplicate</see> + <see cref="SortListArgs.removeUnused">removeUnused</see></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell, ByVal delimiter As Char, ByVal alwaysOnTop As String)
SortList(vsoCell, delimiter, alwaysOnTop, defaultArgs)
End Sub
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="delimiter">The delimiting character of the list.</param>
''' <param name="sortListArgs">Arguments used to determine various sort options. <seealso cref="SortListArgs">SortListArgs</seealso>.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c><paramref name="delimiter"/></c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c>""</c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><see cref="SortListArgs.removeDuplicates">removeDuplicate</see> + <see cref="SortListArgs.removeUnused">removeUnused</see></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell, ByVal delimiter As Char, ByVal sortListArgs As Integer)
SortList(vsoCell, delimiter, defaultAlwaysOnTop, sortListArgs)
End Sub
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="alwaysOnTop">A string which should always be placed on top of the list.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c>;</c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c><paramref name="alwaysOnTop"/></c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><see cref="SortListArgs.removeDuplicates">removeDuplicate</see> + <see cref="SortListArgs.removeUnused">removeUnused</see></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell, ByVal alwaysOnTop As String)
SortList(vsoCell, defaultDelimiter, alwaysOnTop, defaultArgs)
End Sub
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="alwaysOnTop">A string which should always be placed on top of the list.</param>
''' <param name="sortListArgs">Arguments used to determine various sort options. <seealso cref="SortListArgs">SortListArgs</seealso>.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c>;</c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c><paramref name="alwaysOnTop"/></c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><paramref name="sortListArgs"/></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell, ByVal alwaysOnTop As String, ByVal sortListArgs As Integer)
SortList(vsoCell, defaultDelimiter, alwaysOnTop, sortListArgs)
End Sub
''' <summary>
''' Overloads the <see cref="SortList">SortList</see> method for flexibility.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> containing the list.</param>
''' <param name="sortListArgs">Arguments used to determine various sort options. <seealso cref="SortListArgs">SortListArgs</seealso>.</param>
''' <remarks>
''' Calling this method is equivalent to calling <see cref="SortList"/> and passing the following parameters.
''' <list type="bullet">
''' <item>
''' <term>vsoCell</term><description><c><paramref name="vsoCell"/></c></description>
''' </item>
''' <item>
''' <term>delimiter</term><description><c>;</c></description>
''' </item>
''' <item>
''' <term>alwaysOnTop</term><description><c>""</c></description>
''' </item>
''' <item>
''' <term>sortListArgs</term><description><c><paramref name="sortListArgs"/></c></description>
''' </item>
''' </list>
''' </remarks>
Public Sub SortList(ByVal vsoCell As Visio.Cell, ByVal sortListArgs As Integer)
SortList(vsoCell, defaultDelimiter, defaultAlwaysOnTop, sortListArgs)
End Sub
#End Region
#Region "Sort List Support"
''' <summary>
''' Bitwise arguments for the <see cref="SortList">SortList</see> method.
''' </summary>
Public Enum SortListArgs As Integer
''' <summary>
''' Sort in reverse alphabetical order.
''' </summary>
reverseAlpha = 1
''' <summary>
''' Remove duplicate entries from the list.
''' </summary>
removeDuplicates = 2
''' <summary>
''' Remove unused entries from the list.
''' </summary>
removeUnused = 4
End Enum
''' <summary>
''' The default delimiter parameter used by <see cref="SortList">SortList</see>
''' </summary>
Private Const defaultDelimiter As Char = ";"
''' <summary>
''' The default alwaysOnTop parameter used by <see cref="SortList">SortList</see>
''' </summary>
Private Const defaultAlwaysOnTop As String = ""
''' <summary>
''' The default arguments by <see cref="SortList">SortList</see>
''' </summary>
Private Const defaultArgs As Integer = SortListArgs.removeDuplicates + SortListArgs.removeUnused
''' <summary>
''' Gets all of the cells which directly index the <paramref name="vsoCell"></paramref>.
''' Designed for use when the <paramref name="vsoCell"></paramref> is a list.
''' </summary>
''' <param name="vsoCell">The <see href="http://msdn.microsoft.com/en-us/library/ms368327%28v=office.12%29.aspx">Visio.Cell</see> to get references for.</param>
''' <returns>
''' A <see cref="List">List</see> containing all of the cells which directly index <paramref name="vsoCell"/>.
''' </returns>
Public Function GetIndexedCellReferences(ByVal vsoCell As Visio.Cell) As List(Of Visio.Cell)
Dim returnList As New List(Of Visio.Cell)
'Get the array of cells dependent on the list cell of the shape.
Try
'Don't check for dependents if formulaU = No Formula, it will cause death and destruction.
If Not vsoCell.FormulaU = "" Then
For Each dependentCell As Visio.Cell In vsoCell.Dependents
If dependentCell.FormulaU.Contains("INDEX(") And dependentCell.FormulaU.Contains(vsoCell.Name) Then
returnList.Add(dependentCell)
'We don't need to get the dependencies for a cell which is indexing.
Continue For
Else
For Each nestedDependentCell As Visio.Cell In GetIndexedCellReferences(dependentCell)
returnList.Add(nestedDependentCell)
Next
End If
Next
End If
Catch ex As System.Runtime.InteropServices.COMException
'Unexpected End of File, occurs when vsoCell.Dependents is an empty array of pointers, since ForEach is trying to enumerate it.
If ex.ErrorCode = -2032466967 Then
Return returnList
Else
'IDK what it is, throw it back up.
Throw
End If
End Try
Return returnList
End Function
''' <summary>
''' Formats the list for use in a Visio Cell used as either a fixed or variable list.
''' </summary>
''' <param name="list">The list.</param>
''' <param name="alwaysOnTop">A string which should always be on top of the list.</param>
''' <param name="reverseAlpha">if set to <c>true</c> [Sort in Reverse Alphabetical Order].</param>
''' <param name="removeDuplicates">if set to <c>true</c> [Remove Duplicate Entries].</param>
''' <returns></returns>
Public Function FormatList(ByVal list As String, _
ByVal usedList As String, _
ByVal alwaysOnTop As String, _
ByVal reverseAlpha As Boolean, _
ByVal removeDuplicates As Boolean, _
ByVal removeUnused As Boolean) _
As String
Try
'Parse the list and get the individual items.
Dim origSplitList As List(Of String) = list.Split(";").ToList
Dim usedSplitList As List(Of String) = usedList.Split(";").ToList
Dim keys As List(Of String)
'If we are removing unused items, this must be done differently.
If removeUnused Then
'Add an entry for each of the unique indicies referenced by other cells.
keys = New List(Of String)
For i = 0 To usedSplitList.Count - 1
keys.Add(usedSplitList(i))
Next
Else
'If we aren't removing unused items, stick with the original.
keys = origSplitList
End If
If removeDuplicates Then
keys = keys.Distinct.ToList()
End If
' Sort the keys using the specified method.
If reverseAlpha = True Then
keys.Sort(Function(a, b) a.CompareTo(b) * -1)
Else
keys.Sort(Function(a, b) a.CompareTo(b))
End If
'Move specified string to top of list if directed.
If alwaysOnTop.Count > 0 Then
If keys.Contains(alwaysOnTop) Then
keys.RemoveAll(Function(x) x.Equals(alwaysOnTop))
keys.Insert(0, alwaysOnTop)
End If
End If
'A stringBuilder used to create the new list.
Dim newListBuilder As StringBuilder = New StringBuilder
' Loop over the sorted keys, excluding the final key.
For i = 0 To keys.Count - 2
'Add the value to the new list.
newListBuilder.Append(keys(i))
'Add the seperator.
newListBuilder.Append(";", 1)
Next
'Add the final value to the list, but don't add a seperator.
newListBuilder.Append(keys(keys.Count - 1))
Return newListBuilder.ToString
Catch ex As Exception
Return Nothing
End Try
End Function
#End Region
#End Region
End Module