-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaperlessDocumentDataExporter
1289 lines (1096 loc) · 41.2 KB
/
PaperlessDocumentDataExporter
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Attribute VB_Name = "PaperlessDocumentDataExporter"
Private p&, token, dic
Global UserToken As String
Const pageSize As Integer = 200
Const ProgrammVersion As String = "v2.0.1"
Sub GetDocumentTypes()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' Set workbook names
Set wsResult = ThisWorkbook.Sheets("document_types")
Set overview = ThisWorkbook.Sheets("overview")
' Build url for query
paperlessUrl = paperlessAPIUrl & "document_types/"
' Set first row of target sheet to fill in data
firstrowlocal = 2
' read data
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub GetCustomFields()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' Set workbook names
Set wsResult = ThisWorkbook.Sheets("custom_fields")
Set overview = ThisWorkbook.Sheets("overview")
' Build url for query
paperlessUrl = paperlessAPIUrl & "custom_fields/"
' Set first row of target sheet to fill in data
firstrowlocal = 2
' read data
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub GetCorrespondents()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' Set workbook names
Set wsResult = ThisWorkbook.Sheets("correspondents")
Set overview = ThisWorkbook.Sheets("overview")
' Set first row of target sheet to fill in data
firstrowlocal = 2
' Build url for query
paperlessUrl = paperlessAPIUrl & "correspondents/"
' read data
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub GetTags()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' Set workbook names
Set wsResult = ThisWorkbook.Sheets("tags")
Set overview = ThisWorkbook.Sheets("overview")
' Set first row of target sheet to fill in data
firstrowlocal = 2
' Build url for query
paperlessUrl = paperlessAPIUrl & "tags/"
' read data
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub GetUsers()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' Set workbook names
Set wsResult = ThisWorkbook.Sheets("users")
Set overview = ThisWorkbook.Sheets("overview")
' Set first row of target sheet to fill in data
firstrowlocal = 2
' Build url for query
paperlessUrl = paperlessAPIUrl & "users/"
' read data
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub GetStoragePaths()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' Set workbook names
Set wsResult = ThisWorkbook.Sheets("storage_paths")
Set overview = ThisWorkbook.Sheets("overview")
' Set first row of target sheet to fill in data
firstrowlocal = 2
' Build url for query
paperlessUrl = paperlessAPIUrl & "storage_paths/"
' read data
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub GetDocuments()
Dim firstrowlocal As Integer
Dim paperlessUrl As String
Dim wsResult As Object
Dim Query As String
' Get API-Url
paperlessAPIUrl = ThisWorkbook.Names("paperlessAPIUrl").RefersToRange.Value
' Get Token for login
If UserToken = "" Then
UserToken = GetToken()
End If
' set workbook names
Set wsResult = ThisWorkbook.Sheets("documents")
Set overview = ThisWorkbook.Sheets("overview")
' Set first row of target sheet to fill in data
firstrowlocal = 2
' Get query from input
overview.Activate
Query = ThisWorkbook.Names("document_query").RefersToRange.Value
' execute query
paperlessUrl = paperlessAPIUrl & "documents/?" & Query
result = GetQuery(paperlessUrl, wsResult, firstrowlocal)
' Jump back to first sheet
overview.Activate
End Sub
Sub ReplaceColumns()
Dim target_sheet As Object
Dim col As Integer
Dim source_sheet As Object
Dim firstrowlocal As Integer
Dim o As Integer
Set target_sheet = ThisWorkbook.Sheets("documents")
answer = MsgBox("Replace ids by names in sheet " & target_sheet.Name & "?", vbOKCancel)
If answer = vbCancel Then
Exit Sub
End If
On Error GoTo ErrorRaise
' replace all values in column correspondents
Set source_sheet = ThisWorkbook.Sheets("correspondents")
target_field = "correspondent"
col = Application.WorksheetFunction.match(target_field, target_sheet.Rows("1:1"), 0)
firstrowlocal = 2
result = ReplaceIDs(target_sheet, col, firstrowlocal, source_sheet)
' replace all values in column document_type
Set source_sheet = ThisWorkbook.Sheets("document_types")
target_field = "document_type"
col = Application.WorksheetFunction.match(target_field, target_sheet.Rows("1:1"), 0)
firstrowlocal = 2
result = ReplaceIDs(target_sheet, col, firstrowlocal, source_sheet)
' replace all values in storage path
Set source_sheet = ThisWorkbook.Sheets("storage_paths")
target_field = "storage_path"
col = Application.WorksheetFunction.match(target_field, target_sheet.Rows("1:1"), 0)
firstrowlocal = 2
result = ReplaceIDs(target_sheet, col, firstrowlocal, source_sheet)
' replace all values in column owner
Set source_sheet = ThisWorkbook.Sheets("users")
target_field = "owner"
col = Application.WorksheetFunction.match(target_field, target_sheet.Rows("1:1"), 0)
firstrowlocal = 2
result = ReplaceIDs(target_sheet, col, firstrowlocal, source_sheet)
' replace all values in first column tags
Set source_sheet = ThisWorkbook.Sheets("tags")
target_field = "tagStart"
col = Application.WorksheetFunction.match(target_field, target_sheet.Rows("1:1"), 0)
firstrowlocal = 2
result = ReplaceIDs(target_sheet, col, firstrowlocal, source_sheet)
coloftagStart = col
On Error GoTo 0
target_field = "tag"
For o = coloftagStart + 1 To 100
If target_sheet.Cells(1, o).Value = "tag" Then
result = ReplaceIDs(target_sheet, o, firstrowlocal, source_sheet)
Else
Exit For
End If
Next o
MsgBox "Done", vbInformation
Exit Sub
ErrorRaise:
field = source_sheet.Name
MsgBox "Error: field " & target_field & " not found on sheet " & target_sheet.Name, vbInformation
End Sub
Sub ClearAll()
answer = MsgBox("Delete all data from all sheets?", vbOKCancel)
If answer = vbCancel Then
Exit Sub
End If
Sheets("storage_paths").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("correspondents").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("document_types").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("tags").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("users").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("documents").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("custom_fields").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
' Jump back to first sheet
Sheets("overview").Activate
End Sub
Function ReplaceIDs(target_sheet As Object, col As Integer, firstrowlocal As Integer, source_sheet As Object)
Dim search As Variant
Dim result As String
CountofRows = target_sheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = firstrowlocal To CountofRows
search = target_sheet.Cells(i, col).Value
If IsNumeric(search) And search > 0 Then
result = Application.WorksheetFunction.VLookup(search, source_sheet.Range("A:B"), 2, False)
target_sheet.Cells(i, col).Value = result
End If
Next i
End Function
Function GetQuery(paperlessUrl As String, wsResult As Object, firstrowlocal As Integer)
Dim jsonResponse As String
Dim http As Object
Dim col As Integer
Dim i As Long
Dim Filter As String
Dim check As Variant
Dim cols As Variant
Dim partdic As Variant
Dim num1 As Double
Dim num2 As Integer
Dim numbersOfRuns As Integer
' add page-size = 1 to query, just to get the count of result
If Right(paperlessUrl, 1) = "/" Then
queryUrl = paperlessUrl & "?page_size=1"
Else
queryUrl = paperlessUrl & "&page_size=1"
End If
' Make the first API request
Debug.Print queryUrl
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", queryUrl, False
http.setRequestHeader "Authorization", "Token " & UserToken
http.send
' Get the JSON response
jsonResponse = http.responsetext
Set dic = ParseJSON(jsonResponse)
' Get count
Count = CInt(dic("fields.count"))
' Ask user to go on
If Count > 0 Then
answer = MsgBox("Got " & Count & " rows from paperless. Go on and overwrite data in sheet?", vbOKCancel)
If answer = vbCancel Then
Exit Function
End If
End If
' Clear all lines from declared first row on
wsResult.Activate
wsResult.Rows(CStr(firstrowlocal) & ":" & CStr(firstrowlocal)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
wsResult.Select
CountofCols = wsResult.Cells(1, wsResult.Columns.Count).End(xlToLeft).Column
Debug.Print "Anzahl Spalten: " & CountofCols
' calculate numbers of queries
num1 = Count / pageSize
num2 = Count Mod pageSize
If num2 = 0 Then
numbersOfRuns = num1
Else
numbersOfRuns = WorksheetFunction.RoundUp(num1, 0)
End If
Debug.Print "Anzahl Abfragen: " + CStr(numbersOfRuns)
z = 0
For Page = 1 To numbersOfRuns
' add page-size = 10000 to query, just to get the count of result
If Right(paperlessUrl, 1) = "/" Then
queryUrl = paperlessUrl & "?page_size=" & pageSize & "&page=" & Page
Else
queryUrl = paperlessUrl & "&page_size=" & pageSize & "&page=" & Page
End If
' Make the second API request
Debug.Print queryUrl
http.Open "GET", queryUrl, False
http.setRequestHeader "Authorization", "Token " & UserToken
http.send
' Get the JSON response
jsonResponse = http.responsetext
Set dic = ParseJSON(jsonResponse)
'Debug.Print ListPaths(dic)
' Write results to cells
i = 0
Do
' Loop through each column in the result sheet to find matching fields
For col = 1 To CountofCols
check = ""
cols = Null
NewValue = ""
VersionTable = ""
fieldName = wsResult.Cells(1, col).Value
Filter = "fields.results(" & i & ")." & CStr(fieldName)
'Debug.Print "sarching for " & Filter & " for column " & col & " (" & fieldName & ")"
Select Case fieldName
' tags field: seperate column for each tag
Case "tagStart"
Filter = "fields.results(" & i & ").tags"
check = dic(Filter)
If check <> "[]" Then
cols = Array(Filter & "(*)", "")
VersionTable = GetFilteredTable(dic, cols)
If IsArray(VersionTable) Then
Length = UBound(VersionTable)
col = col - 1
For y = 1 To Length
col = col + 1
If (wsResult.Cells(firstrowlocal - 1, col).Value = fieldName Or wsResult.Cells(firstrowlocal - 1, col).Value = "tag") Then
wsResult.Cells(firstrowlocal + z + i, col).Value = CStr(VersionTable(y, 1))
Else
y = Length
col = col - 1
End If
Next y
Else
wsResult.Cells(firstrowlocal + z + i, col).Value = "null"
End If
Else
wsResult.Cells(firstrowlocal + z + i, col).Value = "null"
End If
' content field: set a prefix to ensure, that content is always text
Case "content"
If dic(Filter) <> "null" Then
wsResult.Cells(firstrowlocal + z + i, col).Value = "'" & dic(Filter)
Else
wsResult.Cells(firstrowlocal + z + i, col).Value = ""
End If
' normal fields
Case "name", "username", "path", "id", "correspondent", "document_type", "storage_path", "title", "created", "created_date", "modified", "added", "deleted_at", "archive_serial_number", "original_file_name", "archived_file_name", "owner", "user_can_change", "is_shared_by_requester", "page_count"
If dic(Filter) <> "null" Then
wsResult.Cells(firstrowlocal + z + i, col).Value = dic(Filter)
Else
wsResult.Cells(firstrowlocal + z + i, col).Value = ""
End If
' do nothing, already done before
Case "tag"
' notes
Case "notes"
u = 0
notesstring = ""
Do
' check note has id
Filter = "fields.results(" & i & ").notes(" & u & ").id"
field_id = dic(Filter)
' check note was not deleted
Filter = "fields.results(" & i & ").notes(" & u & ").deleted_at"
field_deleted_at = dic(Filter)
' check note was not restored
Filter = "fields.results(" & i & ").notes(" & u & ").restored_at"
field_restored_at = dic(Filter)
' then, read value
Filter = "fields.results(" & i & ").notes(" & u & ").note"
If CInt(field_id) > 0 And (field_deleted_at = "null" Or (field_deleted_at <> "null" And field_restored_at <> "null")) Then
If u > 0 Then
notesstring = notesstring & Chr(10)
End If
notesstring = notesstring + "[" & u + 1 & "] " & dic(Filter)
End If
u = u + 1
Loop While field_id > 0
wsResult.Cells(firstrowlocal + z + i, col).Value = notesstring
' perhabs a custom field?
Case Else
' check if field name of sheet documents exits on sheet custom_fields
On Error Resume Next
test = 0
test = Application.WorksheetFunction.VLookup(fieldName, ThisWorkbook.Sheets("custom_fields").Range("A:B"), 2, False)
On Error GoTo 0
' when found
If test > 0 Then
' iterate through all custom fields in json-structure
u = 0
Do
' check if field matches
Filter = "fields.results(" & i & ").custom_fields(" & u & ").field"
field = dic(Filter)
' then, read value
If CInt(field) = CInt(test) Then
Filter = "fields.results(" & i & ").custom_fields(" & u & ").value"
wsResult.Cells(firstrowlocal + z + i, col).Value = dic(Filter)
End If
u = u + 1
Loop While field > 0
End If
End Select
Next col
i = i + 1
' check if next row also exits
Filter = "fields.results(" & i & ").id"
' only go on, when next row has id
Loop While CInt(dic(Filter)) > 0 And i < pageSize
z = z + pageSize
Next Page
MsgBox "Done", vbInformation
Exit Function
noresult:
answer = MsgBox("no results", vbOKCancel)
Exit Function
End Function
Function GetToken()
If UserToken = "" Then
UserToken = CStr(InputBox("Please enter Token to acccess your paperless-ngx instance.", "Token"))
End If
GetToken = UserToken
End Function
'##################################################################################################################
' Helper-functions for parsing JSON
'##################################################################################################################
Function ParseJSON(json$, Optional key$ = "fields") As Object
p = 1
token = Tokenize(json)
Set dic = CreateObject("Scripting.Dictionary")
If token(p) = "{" Then ParseObj key Else ParseArr key
Set ParseJSON = dic
End Function
Function ParseObj(key$)
Do: p = p + 1
Select Case token(p)
Case "]"
Case "[": ParseArr key
Case "{"
If token(p + 1) = "}" Then
p = p + 1
dic.Add key, "null"
Else
ParseObj key
End If
Case "}": key = ReducePath(key): Exit Do
Case ":": key = key & "." & token(p - 1)
Case ",": key = ReducePath(key)
Case Else: If token(p + 1) <> ":" Then dic.Add key, token(p)
End Select
Loop
End Function
Function ParseArr(key$)
Dim e&
Do: p = p + 1
Select Case token(p)
Case "}"
Case "{": ParseObj key & ArrayID(e)
Case "[": ParseArr key
Case "]": Exit Do
Case ":": key = key & ArrayID(e)
Case ",": e = e + 1
Case Else: dic.Add key & ArrayID(e), token(p)
End Select
Loop
End Function
Function Tokenize(s$)
Const Pattern = """(([^""\\]|\\.)*)""|[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?|\w+|[^\s""']+?"
Tokenize = RExtract(s, Pattern, True)
End Function
Function RExtract(s$, Pattern, Optional bGroup1Bias As Boolean, Optional bGlobal As Boolean = True)
Dim c&, m, n, v
With CreateObject("vbscript.regexp")
.Global = bGlobal
.MultiLine = False
.IgnoreCase = True
.Pattern = Pattern
If .test(s) Then
Set m = .Execute(s)
ReDim v(1 To m.Count)
For Each n In m
c = c + 1
v(c) = n.Value
If bGroup1Bias Then If Len(n.submatches(0)) Or n.Value = """""" Then v(c) = n.submatches(0)
Next
End If
End With
RExtract = v
End Function
Function ArrayID$(e)
ArrayID = "(" & e & ")"
End Function
Function ReducePath$(key$)
If InStr(key, ".") Then ReducePath = Left(key, InStrRev(key, ".") - 1) Else ReducePath = key
End Function
Function ListPaths(dic)
Dim s$, v
For Each v In dic
s = s & v & " --> " & dic(v) & vbLf
Next
Debug.Print s
End Function
Function GetFilteredValues(dic, match)
Dim c&, i&, v, w
v = dic.keys
ReDim w(1 To dic.Count)
For i = 0 To UBound(v)
If v(i) Like match Then
c = c + 1
w(c) = dic(v(i))
End If
Next
If c = 0 Then
c = 1
End If
ReDim Preserve w(1 To c)
GetFilteredValues = w
End Function
Function GetFilteredTable(dic, cols)
Dim c&, i&, j&, v, w, z
v = dic.keys
z = GetFilteredValues(dic, cols(0))
ReDim w(1 To UBound(z), 1 To UBound(cols) + 1)
For j = 1 To UBound(cols) + 1
z = GetFilteredValues(dic, cols(j - 1))
For i = 1 To UBound(z)
w(i, j) = z(i)
Next
Next
GetFilteredTable = w
End Function
Function OpenTextFile$(f)
With CreateObject("ADODB.Stream")
.Charset = "utf-8"
.Open
.LoadFromFile f
OpenTextFile = .ReadText
End With
End Function
'##################################################################################################################
' Function for building the application the first time
'##################################################################################################################
Sub A_BuildApplication()
On Error GoTo startBuild
ThisWorkbook.Sheets("overview").Select
MsgBox ("The applications already exits")
Exit Sub
startBuild:
On Error GoTo 0
ActiveSheet.Name = "overview"
Cells.Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Columns("A:A").ColumnWidth = 31
Columns("B:B").ColumnWidth = 31
Columns("D:D").ColumnWidth = 31
Range("A2").Select
ActiveCell.FormulaR1C1 = "1. Set API-Url"
Range("B2").Select
ActiveCell.FormulaR1C1 = "http://your-ip-to-paperless/api/"
Range("B2").Select
ActiveWorkbook.Names.Add Name:="paperlessAPIUrl", RefersToR1C1:= _
"=overview!R2C2"
Range("B2").Select
Selection.Hyperlinks.Delete
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A4").Select
ActiveCell.FormulaR1C1 = "2. Get correspondent names"
Range("B4").Select
ActiveSheet.Buttons.Add(175.2, 43.2, 141.6, 14.4).Select
Selection.OnAction = "GetCorrespondents"
Selection.Characters.Text = "Get correspondents"
With Selection.Characters(start:=1, Length:=18).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("A6").Select
ActiveCell.FormulaR1C1 = "3. Get document type names"
Range("B6").Select
ActiveSheet.Buttons.Add(175.2, 72, 141.6, 14.4).Select
Selection.OnAction = "GetDocumentTypes"
Selection.Characters.Text = "Get document types"
With Selection.Characters(start:=1, Length:=18).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("A8").Select
ActiveCell.FormulaR1C1 = "4. Get tag names"
Range("B8").Select
ActiveSheet.Buttons.Add(175.2, 100.8, 141.6, 14.4).Select
Selection.OnAction = "GetTags"
Selection.Characters.Text = "Get tags"
With Selection.Characters(start:=1, Length:=8).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("A10").Select
ActiveCell.FormulaR1C1 = "5. Get user names"
Range("B10").Select
ActiveSheet.Buttons.Add(175.2, 129.6, 141.6, 14.4).Select
Selection.OnAction = "GetUsers"
Selection.Characters.Text = "Get users"
With Selection.Characters(start:=1, Length:=9).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("A12").Select
ActiveCell.FormulaR1C1 = "6. Get storage path names"
Range("B12").Select
ActiveSheet.Buttons.Add(175.2, 158.4, 141.6, 14.4).Select
Selection.OnAction = "GetStoragePaths"
Selection.Characters.Text = "Get storage paths"
With Selection.Characters(start:=1, Length:=17).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("A14").Select
ActiveCell.FormulaR1C1 = "7. Get custom field names"
Range("B14").Select
ActiveSheet.Buttons.Add(175.2, 187.2, 141.6, 14.4).Select
Selection.OnAction = "GetCustomFields"
Selection.Characters.Text = "Get custom fields"
With Selection.Characters(start:=1, Length:=17).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("A16").Select
ActiveCell.FormulaR1C1 = "8. Get documents"
Range("B16").Select
ActiveSheet.Buttons.Add(175.2, 216, 141.6, 14.4).Select
Selection.OnAction = "GetDocuments"
Selection.Characters.Text = "Get documents"
With Selection.Characters(start:=1, Length:=13).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Range("D15").Select
ActiveCell.FormulaR1C1 = "Query for documents"
Range("D16").Select
ActiveCell.FormulaR1C1 = "tags__id=1"
Range("D16").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Range("D16").Select
ActiveWorkbook.Names.Add Name:="document_query", RefersToR1C1:= _
"=overview!R16C4"
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("A18").Select
ActiveCell.FormulaR1C1 = "9. Replace ids by names"
Range("B18").Select
ActiveSheet.Buttons.Add(175.2, 244.8, 141.6, 14.4).Select
Selection.OnAction = "ReplaceColumns"
Range("D28").Select
ActiveSheet.Shapes.Range(Array("Button 8")).Select
Selection.Characters.Text = "Replace ids"
With Selection.Characters(start:=1, Length:=11).Font
.Name = "Calibri"
.FontStyle = "Standard"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone