-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.gs
1050 lines (885 loc) · 42.4 KB
/
code.gs
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
/////////////////////////////////
//VARIABLES AND CONSTANTS
//Open Time Start Time Stuck Time Completed Time Defer Time Last Status Update by Last Reset time Last Reset by
var HDR_TASKID = ["ID", -1];
var HDR_PROJECT = ["Project", -1];
var HDR_TYPE = ["Type", -1];
var HDR_STATUS = ["Status", -1];
var HDR_DESCRIPTION = ["Task Description", -1];
var HDR_ASSIGNER = ["Assigner", -1];
var HDR_ASSIGNEE = ["Assignee", -1];
var HDR_PRIORITY = ["Priority", -1];
var HDR_OPENED_ON = ["Opened On", -1];
var HDR_STARTED_ON = ["Started On", -1];
var HDR_COMPLETED_ON = ["Completed On", -1];
var HDR_WAITING_FROM = ["Waiting from", -1];
var HDR_WIP_FOLDER = ["WIP Folder", -1];
var HDR_TASK_SPEC = ["Task Spec", -1];
var HDR_RFI_SPEC = ["RFI Spec", -1];
var HDR_TIME_TAKEN = ["Duration (min)",-1];
var HDR_BLISS = ["BL/ISS",-1];
///////////////////////////////////////////////////
//User sheet headers
var HDR_INITIALS = ["Initials", -1];
var HDR_ROLES = ["Roles",-1];
var HDR_STATUS_ACTIVE = ["Status",-1];
var HDR_NAME = ["Name",-1];
var HDR_EMAIL = ["Email", -1];
var HDR_ID = ["ID", -1];
var ROLE_EXPERT = "Expert";
var ROLE_TASK_MASTER = "Task Master"
var HDR_CLOCK_STATUS = ["Clock Status", -1];
///////////////////////////////////////////////////////
//Log sheet headers
var HDR_LOG_TASKID = ["ID", -1];
var HDR_LOG_STATUS = ["Status", -1];
var HDR_LOG_INITIALS = ["Initials", -1];
var HDR_LOG_TIME = ["Time", -1];
var HDR_LOG_TIME_ZONE = ["Timezone", -1];
//Project sheet headers
var HDR_PROJECT_CODE= ["Project Code", -1];
var HDR_PROJECT_NAME = ["Project Name", -1];
var HDR_PROJECT_CLIENT = ["Client Name", -1];
var HDR_BACKLOG = ["Product / Dept Backlog", -1];
var HDR_PLANNING_MOM = ["Planning Session MOM", -1];
var HDR_PRODUCT = ["Product", -1];
//has to be defined after all array elems are declared..
var TASK_HEADERS = [HDR_TASKID, HDR_PROJECT, HDR_TYPE, HDR_STATUS, HDR_DESCRIPTION, HDR_ASSIGNER, HDR_ASSIGNEE, HDR_PRIORITY, HDR_OPENED_ON, HDR_STARTED_ON, HDR_COMPLETED_ON, HDR_WAITING_FROM, HDR_WIP_FOLDER, HDR_TASK_SPEC, HDR_RFI_SPEC];
var SUB_TASK_HEADERS_TO_EMPTY = [HDR_STATUS, HDR_DESCRIPTION, HDR_OPENED_ON, HDR_STARTED_ON, HDR_COMPLETED_ON, HDR_WAITING_FROM, HDR_WIP_FOLDER, HDR_TASK_SPEC, HDR_RFI_SPEC];
var HDR_INITIALS = ["Initials", -1];
/*status*/
var STATUS_OPEN = "Open";
var STATUS_INPROGRESS = "In Progress";
var STATUS_WAIT_RFI = "Waiting-RFI";
var STATUS_WAIT_STA = "Waiting-STA";
var STATUS_COMPLETED = "Completed";
var STATUS_DEFER = "Waiting-Pause";
var STATUS_CANCEL = "Cancelled";
var STATUS_DELETED = "Deleted";
var STATUS_CLOCK_IN = "In";
var STATUS_CLOCK_OUT = "Out";
/*sheet names*/
var SH_MASTER = "T.Main";
var SH_USERS = "Users";
var SH_TASKS_USERS_PREFIX = "T.User.";
var SH_CLOCK = "Clock";
var SH_LOG = "Log";
var SH_PROJECTS = "Projects";
var SH_WAITING="T.Waiting";
var SH_LISTS="Lists";
var SH_SPECIAL_COLOR = "FFDE00";
var ID_SELF = '1jBEellzk45PZopydkz4TclV_vjqqzbJVo2H7kpcBhNw';
var ID_ARCHIVAL_FOLDER = '0B2sB0DUvFeWQbGdTWmxyS2lfdVk';
var ID_TASK_ARTIFACT_FOLDER_ROOT = '0B2sB0DUvFeWQZGhWZ2tXX3IzZlU';
var ID_RFI_TEMPLATE = '15N_JBWDgpZXkv-P3xH2x_yhWgDZvS6odpd9Ni5_TUtk';
var ID_SPEC_TEMPLATE = '1zHZAcu9YN7A7ECC5UQzYMLXD_q_9B5h3OjO899Gt2oE';
var ID_BACKLOG = '18FV21vyY8UmsJH0ufSAW62oAKKuRExQoPM3m_JqQTEw';
var WIP_FOLDERNAME = "WIP-[task_id]";
var RFI_FILENAME = "RFI - [task_id]";
var SPEC_FILENAME = "TaskSpec-[task_id]";
var ID_PREFIX = "T.";
var SEC1_RESERVED_LAST = 2;
var DATE_FORMAT = "dd-MMM-yyyy HH:mm";
var LAST_PROGRESS_TIME = "LAST_PROGRESS_TIME";
var CLOCKOUT_WAITING_TIME_IN_MIN = 120;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ******* START REGION: Triggers. These are event handlers for events from the googlespreadsheets
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onEdit(e) {
var runtimeCountStart = new Date();
/* event details */
var sheets = e.source;
var u = e.user;
var rng = e.range;
var sh = rng.getSheet();
//var value = e.value;
var value = rng.getValue();
/* Extract the task ID from the query in ID column*/
var colID = getColumn(sh, HDR_TASKID);
var colStart = rng.getColumn();
var rowStart = rng.getRow();
var taskId = sh.getRange(rowStart, colID).getValue();
if(typeof value == 'undefined')
value = '';
/* If user is in main sheet */
if (isMainSheet()){
if(colStart == colID){
SpreadsheetApp.getUi().alert("ID must not be entered manually. Please click on Open Task to generate task ID");
rng.clearContent();
return;
}
var userInitials = sh.getRange(rowStart, getColumn(sh, HDR_ASSIGNEE)).getValue();
var userSheet = getSheet(SH_TASKS_USERS_PREFIX+userInitials, false);
if(userSheet != null){
/*reflect update on section 2 of user sheet*/
if(isSubTaskId(taskId))
return;
var subTaskCell = findFirstCell(taskId, userSheet.getRange(SEC1_RESERVED_LAST+1, 1, userSheet.getLastRow()));
if(subTaskCell[0]<1)
return;
userSheet.getRange(subTaskCell[0], colStart).setValue(value);
}
}
else if(isCurrentUserSheet()){
/* If the user is editing in section 1 */
if(isSectionOne()){
SpreadsheetApp.getUi().alert("Not allowed to edit in Section 1. Please undo your changes and edit in section 2");
//e.range.clearContent();
return;
}
/* If the User is in section 2 */
/* Use the taskID to update the content on the Main sheet in the corresponding cell */
var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER);
var cellTaskIDMain = getMainIdCellArr(sh, SpreadsheetApp.getActiveSheet().getActiveRange());
var mainRow = (cellTaskIDMain[0])[0];
if(mainRow<1)
return;
mainSheet.getRange(mainRow, rng.getColumn()).setValue(value);
}
runtimeCountStop(runtimeCountStart);
}
/*inserts menu items into the spreadsheet menu and declares menu callbacks*/
function onOpen() {
var menuTasks = SpreadsheetApp.getUi().createMenu('TIG Tasks');
menuTasks.addItem('Start Task', 'onStartTask');
menuTasks.addItem('Complete Task', 'onCompleteTask');
menuTasks.addSeparator();
menuTasks.addItem('Open RFI', 'onOpenRFI');
menuTasks.addItem('Close RFI', 'onCloseRFI');
menuTasks.addSeparator();
menuTasks.addItem('Request Approval for Subtasks', 'onRequestSubTaskApproval');
menuTasks.addItem('Approve Subtasks', 'onApproveSubTasks');
menuTasks.addItem('Breakdown Subtasks', 'onBreakdownSubTasks');
menuTasks.addSeparator();
menuTasks.addItem('Create RFI Spec', 'onCreateRFI');
menuTasks.addItem('Create Task Spec', 'OnCreateTaskInputSpec');
menuTasks.addItem('Create WIP Folder', 'OnCreateWIPFolder');
menuTasks.addSeparator();
menuTasks.addItem('Open Task', 'onOpenTask');
menuTasks.addItem('Cancel Task', 'OnCancelTask');
menuTasks.addItem('Pause Task', 'OnDeferTask');
menuTasks.addToUi();
SpreadsheetApp.getUi()
.createMenu('TIG Options')
.addItem('Jump to last row', 'jumpToLastRow')
.addItem('Jump to task breakdown', 'jumpToSubTaskBreakDown')
.addItem('Jump to my sheet', 'jumpToMySheet')
.addSeparator()
.addItem('Clock In', 'onClockIn')
.addItem('Clock Out', 'onClockOut')
.addToUi();
SpreadsheetApp.getUi()
.createMenu('TIG Admin')
.addItem('Add User Sheet1', 'onSetupUser')
.addSeparator()
.addItem('Clean Tasks', 'onCleanTasks')
.addToUi();
setSpecialSheet();
};
// ******* END REGION: Triggers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ******* START REGION: Menu Callbacks
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function onTest() {
SpreadsheetApp.getActiveSpreadsheet().toast(SpreadsheetApp.getActiveSheet().getActiveCell().get);
}
function onClockIn() {
if(isClockedIn())
SpreadsheetApp.getUi().alert("Already Clocked in");
else
clockIn("User");
}
function onClockOut() {
if(isClockedIn())
clockOut("User");
else
SpreadsheetApp.getUi().alert("Not Clocked in");
}
function onIdle(){
var userSheet = getSheet(SH_USERS,false);
var lastColumn = userSheet.getLastColumn();
var lastRow = userSheet.getLastRow();
var idCol = getColumn(userSheet, HDR_ID);
var clockStatusCol = getColumn(userSheet, HDR_CLOCK_STATUS);
var initialsCol = getColumn(userSheet, HDR_INITIALS);
var nameCol = getColumn(userSheet, HDR_NAME);
for (var i = 1; i<=lastRow; i++){
var vals = userSheet.getRange(i, idCol, 1, lastColumn).getValues()[0];
var ind = vals[0].indexOf("U");
var isUserEntry = (ind === 0);
if(isUserEntry){
var isUserClockedIn = (vals[clockStatusCol - 1].indexOf("In")===0);
if(isUserClockedIn){
var initials = vals[initialsCol - 1];
var lpTime = getLastProgressTime(initials);
if(lpTime==null){
/*for the case of first time users*/
setLastProgresstime(initials);
continue;
}
// tickDurationClock(initials); //TODO: RJ review
var startTime = strToDate(lpTime.replace(' UTC',''));
var endTime = strToDate(getTime().replace(' UTC',''));
var timeTaken = endTime.getTime() - startTime.getTime();
var minutesTaken = Math.floor(timeTaken/(60*1000));
//if last progress time was more than 2 hours ago, clock out
if(minutesTaken >= CLOCKOUT_WAITING_TIME_IN_MIN)
clockOut("Auto", vals[nameCol-1], initials, getAutoClockOutTime(CLOCKOUT_WAITING_TIME_IN_MIN));
}
}
}
}
function tickDurationClock(initials){
var mainSheet = getSheet(SH_MASTER),
lastRow = mainSheet.getLastRow(),
assigneeCol= getColumn(mainSheet, HDR_ASSIGNEE),
timeTakenCol = getColumn(mainSheet, HDR_TIME_TAKEN),
statusCol = getColumn(mainSheet, HDR_STATUS),
lastCol = mainSheet.getLastColumn();
for(var i=2; i<=lastRow; i++){
var row = mainSheet.getRange(i, 1, 1, lastCol).getValues(),
result = row[0];
if(result[assigneeCol-1] === initials && result[statusCol-1] === STATUS_INPROGRESS){
var duration = result[timeTakenCol-1];
if(duration === '')
doStatusChange(null, null, HDR_TIME_TAKEN, mainSheet, mainSheet.getRange(i, timeTakenCol), 5, initials);
else
doStatusChange(null, null, HDR_TIME_TAKEN, mainSheet, mainSheet.getRange(i, timeTakenCol), duration+5, initials);
}
}
}
function onStartTask() {
var runtimeCountStart = new Date();
if(otherUserSheet() || !isCurrentUserSheet()) {
SpreadsheetApp.getUi().alert("Cannot start task. Please start the task from your task sheet [" + SH_TASKS_USERS_PREFIX + getCurrentUserDetails()[0] + "]");
return;
}
/*Automatic clock In*/
if(!isClockedIn()){
clockIn("Auto");
SpreadsheetApp.getActiveSpreadsheet().toast("You have been Clocked in automatically");
}
/*status updates*/
var arr = doStatusChangeSTART(STATUS_INPROGRESS, HDR_ASSIGNEE, HDR_STARTED_ON, SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange(), getTime());
runtimeCountStop(runtimeCountStart);
recordRuntime('onStartTask');
}
function onBreakdownSubTasks() {
var sheet = SpreadsheetApp.getActiveSheet();
var rng = SpreadsheetApp.getActiveSheet().getActiveRange();
//statusValue, ByCol, OnCol, sheet, rng, tm;
var sheet_main = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER);
var colIDMain = getColumn(sheet_main, HDR_TASKID);
var colID = getColumn(sheet, HDR_TASKID);
var cHDR_DESCRIPTION = getColumn(sheet, HDR_DESCRIPTION);
var cHDR_STATUS = getColumn(sheet, HDR_STATUS);
var cHDR_OPENED_ON = getColumn(sheet, HDR_OPENED_ON);
var cHDR_STARTED_ON = getColumn(sheet, HDR_STARTED_ON);
var cHDR_COMPLETED_ON = getColumn(sheet, HDR_COMPLETED_ON);
var cHDR_WAITING_FROM = getColumn(sheet, HDR_WAITING_FROM);
var cHDR_WIP_FOLDER = getColumn(sheet, HDR_WIP_FOLDER);
var cHDR_TASK_SPEC = getColumn(sheet, HDR_TASK_SPEC);
var numRows = rng.getNumRows();
var startRow = rng.getRow();
/*loop over active range*/
for (var i = startRow; i < startRow + numRows; i++){
var taskid = sheet.getRange(i , colID).getValue();
var cellTaskIDMain = findFirstCell(taskid, sheet_main.getRange(2, colIDMain, sheet_main.getLastRow(), 1));
if(cellTaskIDMain[0] < 0){
continue;
}
/*if subtasks have already been generated, skip inserting again*/
var subTaskCell = findFirstCell(taskid, sheet.getRange(SEC1_RESERVED_LAST+1, colID, sheet.getLastRow()));
if(subTaskCell[0]>0)
continue;
/*****Section-2 sub task insert****/
var taskvalues = sheet.getRange(i, 1, 1, sheet.getLastColumn()).getValues();
//--changes here--
sheet.insertRows(SEC1_RESERVED_LAST+1, 5);
var lastRow = SEC1_RESERVED_LAST + 2;
/*ensure it is written beyond the reserved ranges of section 1*/
//if(lastRow < SEC1_RESERVED_LAST+1){
// lastRow = lastRow + (SEC1_RESERVED_LAST+1-lastRow);
//}
/*clone of task record*/
var insertedTask = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn());
insertedTask.setBackground("#E0F2F1");
insertedTask.setValues(taskvalues);
/*sub task 1*/ //=CONCATENATE($A$20, "." ,ROW() - ROW($A$20))
var insertedSubTask1 = sheet.getRange(lastRow + 1, 1, 1, sheet.getLastColumn());
taskvalues[0][colID - 1] = taskid + "." + 1;
taskvalues[0][cHDR_DESCRIPTION - 1] = "";
taskvalues[0][cHDR_STATUS- 1] = "";
taskvalues[0][cHDR_OPENED_ON- 1] = "";
taskvalues[0][cHDR_STARTED_ON- 1] = "";
taskvalues[0][cHDR_COMPLETED_ON - 1] = "";
taskvalues[0][cHDR_WAITING_FROM- 1] = "";
taskvalues[0][cHDR_WIP_FOLDER - 1] = "";
taskvalues[0][cHDR_TASK_SPEC - 1] = "";
insertedSubTask1.setValues(taskvalues);
/*sub task 2*/
insertedSubTask1 = sheet.getRange(lastRow + 2, 1, 1, sheet.getLastColumn());
taskvalues[0][colID - 1] = taskid + "." + 2;
insertedSubTask1.setValues(taskvalues);
/*sub task 3*/
insertedSubTask1 = sheet.getRange(lastRow + 3, 1, 1, sheet.getLastColumn());
taskvalues[0][colID - 1] = taskid + "." + 3;
insertedSubTask1.setValues(taskvalues);
}
}
function onCompleteTask() {
var runtimeCountStart = new Date();
if(otherUserSheet() || !isCurrentUserSheet()) {
SpreadsheetApp.getUi().alert("Cannot complete task. Please complete the task from your task sheet [" + SH_TASKS_USERS_PREFIX + getCurrentUserDetails()[0] + "]");
return;
}
/*update status and time taken*/
var time = getTime();
var arr = doStatusChangeCOMPLETE(time, SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange());
var sh = SpreadsheetApp.getActiveSheet();
var rng = SpreadsheetApp.getActiveSheet().getActiveRange();
var colID = getColumn(sh, HDR_TASKID);
var rowStart = rng.getRow();
var taskId = sh.getRange(rowStart, colID).getValue();
//if not maintask id,
if(isSubTaskId(taskId))
return;
var waitingSheet = getSheet(SH_WAITING);
var lastRow = waitingSheet.getLastRow();
var lastColumn = waitingSheet.getLastColumn();
var statusColumn = getColumn(waitingSheet, HDR_STATUS);
var idColumn = getColumn(waitingSheet, HDR_TASKID);
var assigneeColumn = getColumn(waitingSheet, HDR_ASSIGNEE);
var currentUserInitials = getCurrentUserDetails()[0];
for(var r = 2; r <= lastRow; r++){
var status = waitingSheet.getRange(r, statusColumn).getValue();
var assignee = waitingSheet.getRange(r, assigneeColumn).getValue();
if(status === STATUS_DEFER && assignee === currentUserInitials){
var mainSheet = getSheet(SH_MASTER);
var deferedTaskRange = waitingSheet.getRange(r, 1, 1, lastColumn);
updateStatusInactiveRange(STATUS_INPROGRESS, HDR_ASSIGNEE, HDR_WAITING_FROM, "", waitingSheet, deferedTaskRange);
break;
}
}
runtimeCountStop(runtimeCountStart);
recordRuntime('onCompleteTask');
}
function onCreateRFI() {
var runtimeCountStart = new Date();
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
createArtifacts(false, true);
/*SpreadsheetApp.getActiveSpreadsheet().toast("RFI document created. Please open the document from column '" + HDR_RFI_SPEC + "' of the selected task(s). Please edit the document and then use the 'Open RFI' menu item to get the RFI published");*/
runtimeCountStop(runtimeCountStart);
recordRuntime('onCreateRFI');
}
function onOpenRFI() {
var runtimeCountStart = new Date();
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
var arr;
/*fetch the assigner*/
var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER);
var assignerCol = getColumn(mainSheet, HDR_ASSIGNER);
var idCol = getColumn(mainSheet, HDR_TASKID);
var descCol = getColumn(mainSheet, HDR_DESCRIPTION);
var rfiSpecCol = getColumn(mainSheet, HDR_RFI_SPEC);
/*iterate over each row in main sheet and update the status + send an email*/
var mainTasks = getMainIdCellArr(SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange());
for(var i = 0; i < mainTasks.length; i++){
var row = (mainTasks[i])[0];
var assigner = mainSheet.getRange(row, assignerCol).getValue();
var taskId = mainSheet.getRange(row, idCol).getValue();
var desc = mainSheet.getRange(row, descCol).getValue();
var rfiSpec = mainSheet.getRange(row, rfiSpecCol).getValue();
/*email recipients*/
var emailArr = getExpertEmail();
if(assigner !== '')
emailArr.push(getEmail(assigner));
emailArr.concat(getTaskMasterEmail());
/*build email body & subject*/
var currUserName = (getCurrentUserDetails())[1];
var message = "RFI raised by " + currUserName+" on task " + taskId + ": " + desc + "\nLink: "+rfiSpec;
var sub = "RFI - "+taskId;
/*make status change to task*/
arr = doStatusChange(STATUS_WAIT_RFI, HDR_ASSIGNEE, HDR_WAITING_FROM, mainSheet, mainSheet.getRange(row, idCol), getTime());
/*send mail*/
sendEmail(emailArr, sub, message);
}
runtimeCountStop(runtimeCountStart);
recordRuntime('onOpenRFI');
}
function onCloseRFI() {
var runtimeCountStart = new Date();
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
var arr = updateStatus(STATUS_INPROGRESS, HDR_ASSIGNEE, HDR_WAITING_FROM, '');
runtimeCountStop(runtimeCountStart);
recordRuntime('onCloseRFI');
}
function onRequestSubTaskApproval() {
var runtimeCountStart = new Date();
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
//var arr = doStatusChange(STATUS_WAIT_STA, HDR_ASSIGNEE, HDR_WAITING_FROM,
// SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange(), getTime());
updateStatus(STATUS_WAIT_STA, HDR_ASSIGNEE, HDR_WAITING_FROM, getTime());
runtimeCountStop(runtimeCountStart);
recordRuntime('onRequestSubTaskApproval');
}
function onApproveSubTasks() {
var runtimeCountStart = new Date();
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
// var arr = doStatusChange(STATUS_INPROGRESS, HDR_ASSIGNEE, HDR_WAITING_FROM,
// SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange(), "");
updateStatus(STATUS_OPEN, null, HDR_WAITING_FROM, "");
runtimeCountStop(runtimeCountStart);
recordRuntime('onApproveSubTasks');
}
function OnCreateWIPFolder() {
var runtimeCountStart = new Date();
createArtifacts(false, false);
runtimeCountStop(runtimeCountStart);
recordRuntime('OnCreateWIPFolder');
}
function OnCreateTaskInputSpec() {
var runtimeCountStart = new Date();
createArtifacts(true, false);
runtimeCountStop(runtimeCountStart);
recordRuntime('OnCreateTaskInputSpec');
}
function onOpenTask() {
var runtimeCountStart = new Date();
/*if not on main sheet, alert!*/
if(!isMainSheet()){
SpreadsheetApp.getUi().alert("Task must be opened from main sheet");
return;
}
var arr = doStatusChange(STATUS_OPEN, HDR_ASSIGNER, HDR_OPENED_ON,
SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange(), getTime());
runtimeCountStop(runtimeCountStart);
recordRuntime('onOpenTask');
}
function onOpenSubtask() {
//generateSubTaskIDs();
//var arr = doStatusChange(STATUS_OPEN, HDR_ASSIGNER, HDR_OPENED_ON, SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange(), getTime());
}
function OnCancelTask() {
var runtimeCountStart = new Date();
if(!getCurrentUserDetails == "RJ") {
if ((otherUserSheet() || !isCurrentUserSheet())) {
SpreadsheetApp.getUi().alert("Cannot cancel task. Please -Cancel- the task from your task sheet [" + SH_TASKS_USERS_PREFIX + getCurrentUserDetails()[0] + "]");
return;
}
}
/*update status and time taken*/
var time = getTime();
var arr = doStatusChangeCANCEL(time, SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange());
var sh = SpreadsheetApp.getActiveSheet();
var rng = SpreadsheetApp.getActiveSheet().getActiveRange();
var colID = getColumn(sh, HDR_TASKID);
var rowStart = rng.getRow();
var taskId = sh.getRange(rowStart, colID).getValue();
//if not maintask id,
if(isSubTaskId(taskId))
return;
var waitingSheet = getSheet(SH_WAITING);
var lastRow = waitingSheet.getLastRow();
var lastColumn = waitingSheet.getLastColumn();
var statusColumn = getColumn(waitingSheet, HDR_STATUS);
var idColumn = getColumn(waitingSheet, HDR_TASKID);
var assigneeColumn = getColumn(waitingSheet, HDR_ASSIGNEE);
var currentUserInitials = getCurrentUserDetails()[0];
for(var r = 2; r <= lastRow-1; r++){
var status = waitingSheet.getRange(r, statusColumn).getValue();
var assignee = waitingSheet.getRange(r, assigneeColumn).getValue();
if(status === STATUS_DEFER && assignee === currentUserInitials){
var mainSheet = getSheet(SH_MASTER);
var deferedTaskRange = waitingSheet.getRange(r, 1, 1, lastColumn);
updateStatusInactiveRange(STATUS_INPROGRESS, HDR_ASSIGNEE, HDR_WAITING_FROM, "", waitingSheet, deferedTaskRange);
break;
}
}
runtimeCountStop(runtimeCountStart);
recordRuntime('OnCancelTask');
}
function OnDeferTask() {
var runtimeCountStart = new Date();
updateStatus(STATUS_DEFER, HDR_ASSIGNEE, HDR_WAITING_FROM, getTime());
runtimeCountStop(runtimeCountStart);
recordRuntime('OnDeferTask');
}
// ******* END REGION: Menu Callbacks
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ******* START REGION: Utility Functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* update the status on main sheet and section 2 */
function updateStatus(statusValue, ByCol, OnCol, OnColValue){
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
var arr;
var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER);
var mainTasks = getMainIdCellArr(SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange());
if((mainTasks[0])[0] < 0){
doStatusChange(statusValue, ByCol, OnCol, SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange(), OnColValue);
return;
}
for(var i = 0; i < mainTasks.length; i++){
var row = (mainTasks[i])[0];
var idCol = getColumn(mainSheet, HDR_TASKID);
var assigneeCol = getColumn(mainSheet, HDR_ASSIGNEE);
var initials = mainSheet.getRange(row, assigneeCol).getValue();
if(initials == '')
initials = null;
arr = doStatusChange(statusValue, ByCol, OnCol, mainSheet, mainSheet.getRange(row, idCol), OnColValue, initials);
}
return arr;
}
function updateStatusInactiveRange(statusValue, ByCol, OnCol, OnColValue, sheet, range){
var mainTasks = getMainIdCellArr(sheet, range);
for(var i = 0; i < mainTasks.length; i++){
var row = (mainTasks[i])[0];
var idCol = getColumn(sheet, HDR_TASKID);
var mainSheet = getSheet(SH_MASTER);
arr = doStatusChange(statusValue, ByCol, OnCol, mainSheet, mainSheet.getRange(row, idCol), OnColValue);
}
return arr;
}
/* create artifacts and update the column values on main sheet and section 2*/
function createArtifacts(bCreateTaskSpec, bCreateRFIDoc){
if(otherUserSheet()){
SpreadsheetApp.getUi().alert("Cannot do operation for somebody else.");
return;
}
var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER);
if(isOwnUserSheet()){
var mainTasks = getMainIdCellArr(SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange());
if((mainTasks[0])[0] < 0){
SpreadsheetApp.getUi().alert("Cannot create subtask artifacts");
return;
}
for(var i = 0; i < mainTasks.length; i++){
var row = (mainTasks[i])[0];
var idCol = getColumn(mainSheet, HDR_TASKID);
doCreateTaskArtifacts(bCreateTaskSpec, bCreateRFIDoc, mainSheet, mainSheet.getRange(row, idCol));
}
} else{
doCreateTaskArtifacts(bCreateTaskSpec, bCreateRFIDoc,
SpreadsheetApp.getActiveSheet(), SpreadsheetApp.getActiveSheet().getActiveRange());
}
}
/*Important function: does the actual setting of status/time/duration value(s)*/
function doStatusChange(statusValue, ByCol, OnCol, sheet, rng, OnColValue, initials) {
var numRows = rng.getNumRows();
var startRow = rng.getRow();
var info = [];
var ignored = "";
if(initials == null)
initials = getCurrentUserDetails()[0];
var colStatus = (statusValue == null || typeof(statusValue)==='undefined' )? null : getColumn(sheet, HDR_STATUS);
var colBy = (ByCol == null || typeof(ByCol)==='undefined') ? null : getColumn(sheet, ByCol);
var colOn = (OnCol == null || typeof(OnCol)==='undefined') ? null : getColumn(sheet, OnCol);
var colID = getColumn(sheet, HDR_TASKID);
var userSheet = getSheet(SH_TASKS_USERS_PREFIX + initials, false);
for (i = startRow; i < startRow + numRows; i++){
/*If task description is not set, ignore*/
if (!isDescSet(i, sheet)){
ignored += "[" + i + "] ";
continue;
}
/*set defaults*/
setDefaultProps(i, sheet);
var taskId = sheet.getRange(i , colID).getValue();
/*reflect update on section 2 of user sheet*/
var subTaskCell = findFirstCell(taskId, userSheet.getRange(SEC1_RESERVED_LAST+1, colID, userSheet.getLastRow()));
/*if the subtask exists in section 2*/
if(subTaskCell[0] != -1){
var subTaskRow = subTaskCell[0];
if(colStatus !=null)
userSheet.getRange(subTaskRow, colStatus).setValue(statusValue);
if (colBy != null)
userSheet.getRange(subTaskRow, colBy).setValue(initials);
if (colOn != null)
userSheet.getRange(subTaskRow, colOn).setValue(OnColValue);
}
/*set the values*/
if(colStatus !=null)
sheet.getRange(i, colStatus).setValue(statusValue);
if (colBy != null && sheet.getRange(i, colBy).getValue()!='')
sheet.getRange(i, colBy).setValue(initials);
if (colOn != null)
sheet.getRange(i, colOn).setValue(OnColValue);
/*update sheet log*/
if(statusValue!=null && statusValue.length>0)
updateLog(taskId, initials, statusValue);
info.push(["rows", i]);
}
if (ignored != "")
SpreadsheetApp.getActiveSpreadsheet().toast("Cannot change status: Description is empty for row(s) " + ignored);
info["ignored"] = ignored;
return info;
}
/*important function: does the actual setting of the status, time and initials*/
function doStatusChangeSTART(statusValue, ByCol, OnCol, sheet, rng, tm) {
//Logger.log("started status-change-start " + " | " + statusValue + " | " + ByCol + " | " + OnCol + " | " + sheet + " | " + rng + " | " + tm) ;
var sheet_main = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER);
var colIDMain = getColumn(sheet_main, HDR_TASKID);
var colID = getColumn(sheet, HDR_TASKID);
var cHDR_DESCRIPTION = getColumn(sheet, HDR_DESCRIPTION);
var cHDR_STATUS = getColumn(sheet, HDR_STATUS);
var cHDR_OPENED_ON = getColumn(sheet, HDR_OPENED_ON);
var cHDR_STARTED_ON = getColumn(sheet, HDR_STARTED_ON);
var cHDR_COMPLETED_ON = getColumn(sheet, HDR_COMPLETED_ON);
var cHDR_WAITING_FROM = getColumn(sheet, HDR_WAITING_FROM);
var cHDR_WIP_FOLDER = getColumn(sheet, HDR_WIP_FOLDER);
var cHDR_TASK_SPEC = getColumn(sheet, HDR_TASK_SPEC);
var numRows = rng.getNumRows();
var startRow = rng.getRow();
//Logger.log("initialized status-change-start");
/*loop over active range*/
for (var i = startRow; i < startRow + numRows; i++){
var taskid = sheet.getRange(i , colID).getValue();
//Logger.log("Looking for matching tasks in main sheet. Processing " + taskid);
var cellTaskIDMain = findFirstCell(taskid, sheet_main.getRange(2, colIDMain, sheet_main.getLastRow(), 1));
/*if it is a sub-task*/
if(cellTaskIDMain[0] < 0){
//Logger.log(taskid + " is a subtask, applying status change");
doStatusChange(statusValue, ByCol, OnCol, sheet, rng, tm);
continue;
}
/*if subtasks have already not been generated, then generate & insert sub tasks*/
var subTaskCell = findFirstCell(taskid, sheet.getRange(SEC1_RESERVED_LAST+1, colID, sheet.getLastRow()));
if(subTaskCell[0]<0){
/*****Section-2 sub task insert****/
var taskvalues = sheet.getRange(i, 1, 1, sheet.getLastColumn()).getValues();
//--changes here--
sheet.insertRows(SEC1_RESERVED_LAST+1, 5);
var lastRow = SEC1_RESERVED_LAST + 2;
/*ensure it is written beyond the reserved ranges of section 1*/
//if(lastRow < SEC1_RESERVED_LAST+1){
// lastRow = lastRow + (SEC1_RESERVED_LAST+1-lastRow);
//}
/*clone of task record*/
var insertedTask = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn());
insertedTask.setBackground("#E0F2F1");
insertedTask.setValues(taskvalues);
/*sub task 1*/ //=CONCATENATE($A$20, "." ,ROW() - ROW($A$20))
var insertedSubTask1 = sheet.getRange(lastRow + 1, 1, 1, sheet.getLastColumn());
taskvalues[0][colID - 1] = taskid + "." + 1;
taskvalues[0][cHDR_DESCRIPTION - 1] = "";
taskvalues[0][cHDR_STATUS- 1] = "";
taskvalues[0][cHDR_OPENED_ON- 1] = "";
taskvalues[0][cHDR_STARTED_ON- 1] = "";
taskvalues[0][cHDR_COMPLETED_ON - 1] = "";
taskvalues[0][cHDR_WAITING_FROM- 1] = "";
taskvalues[0][cHDR_WIP_FOLDER - 1] = "";
taskvalues[0][cHDR_TASK_SPEC - 1] = "";
insertedSubTask1.setValues(taskvalues);
/*sub task 2*/
insertedSubTask1 = sheet.getRange(lastRow + 2, 1, 1, sheet.getLastColumn());
taskvalues[0][colID - 1] = taskid + "." + 2;
insertedSubTask1.setValues(taskvalues);
/*sub task 3*/
insertedSubTask1 = sheet.getRange(lastRow + 3, 1, 1, sheet.getLastColumn());
taskvalues[0][colID - 1] = taskid + "." + 3;
insertedSubTask1.setValues(taskvalues);
}
/*status update on main sheet*/
doStatusChange(statusValue, ByCol, OnCol, sheet_main, sheet_main.getRange(cellTaskIDMain[0], cellTaskIDMain[1]), tm );
}
}
/* Updates the time taken column for a task on main sheet*/
function doStatusChangeCOMPLETE(time, sheet, range){
var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER),
numRows = range.getNumRows(),
startRow = range.getRow(),
timeTakenCol = getColumn(sheet, HDR_TIME_TAKEN),
idCol = getColumn(sheet, HDR_TASKID),
blissCol = getColumn(sheet, HDR_BLISS),
projectCol = getColumn(sheet, HDR_PROJECT),
mainIdCol = getColumn(mainSheet, HDR_TASKID),
startDateCol = getColumn(mainSheet, HDR_STARTED_ON),
info = [],
result = [],
ignored = "",
userInitials = sheet.getRange(startRow, getColumn(sheet, HDR_ASSIGNEE)).getValue(),
userSheet = getSheet(SH_TASKS_USERS_PREFIX+userInitials, false),
rowArray = getMainIdCellArr(sheet, range);
/*fetch the task IDs in current position - as these maybe disttorted after change*/
for (var i = startRow; i < startRow + numRows; i++){
var taskid = sheet.getRange(i , idCol).getValue();
var blissId = sheet.getRange(i, blissCol).getValue();
var projectCode = sheet.getRange(i, projectCol).getValue();
result.push(taskid + "|" + i + "|" + blissId + "|" + projectCode);
}
var display = "";
for(var j = 0; j < result.length; j++){
var i = result[j].split("|");
var taskid = i[0];
var row = i[1];
var blissId = i[2];
var projectCode = i[3];
var cellTaskIDMain = findFirstCell(taskid, mainSheet.getRange(2, idCol, mainSheet.getLastRow(), 1));
var rowMain = cellTaskIDMain[0];
/*if it is a sub task*/
if(rowMain < 0){
doStatusChange(STATUS_COMPLETED, null, HDR_COMPLETED_ON, sheet, sheet.getRange(row, timeTakenCol), time);
} else{
/*skip if description is missing*/
if (!isDescSet(rowMain, mainSheet)){
ignored += "[" + rowMain + "] ";
continue;
}
doStatusChange(STATUS_COMPLETED, null, HDR_COMPLETED_ON, mainSheet, mainSheet.getRange(cellTaskIDMain[0], timeTakenCol), time);
deleteEmptySubtasks(taskid, sheet);
updateBLISSStatus(blissId, projectCode, STATUS_BL_CLOSED_COMPLETE);
}
}
info["ignored"] = ignored;
return info;
}
/* Updates the time taken column for a task on main sheet*/
function doStatusChangeCANCEL(time, sheet, range){
var mainSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SH_MASTER),
numRows = range.getNumRows(),
startRow = range.getRow(),
timeTakenCol = getColumn(sheet, HDR_TIME_TAKEN),
idCol = getColumn(sheet, HDR_TASKID),
blissCol = getColumn(sheet, HDR_BLISS),
projectCol = getColumn(sheet, HDR_PROJECT),
mainIdCol = getColumn(mainSheet, HDR_TASKID),
startDateCol = getColumn(mainSheet, HDR_STARTED_ON),
info = [],
result = [],
ignored = "",
userInitials = sheet.getRange(startRow, getColumn(sheet, HDR_ASSIGNEE)).getValue(),
userSheet = getSheet(SH_TASKS_USERS_PREFIX+userInitials, false),
rowArray = getMainIdCellArr(sheet, range);
/*fetch the task IDs in current position - as these maybe disttorted after change*/
for (var i = startRow; i < startRow + numRows; i++){
var taskid = sheet.getRange(i , idCol).getValue();
var blissId = sheet.getRange(i, blissCol).getValue();
var projectCode = sheet.getRange(i, projectCol).getValue();
result.push(taskid + "|" + i + "|" + blissId + "|" + projectCode);
}
var display = "";
for(var j = 0; j < result.length; j++){
var i = result[j].split("|");
var taskid = i[0];
var row = i[1];
var blissId = i[2];
var projectCode = i[3];
var cellTaskIDMain = findFirstCell(taskid, mainSheet.getRange(2, idCol, mainSheet.getLastRow(), 1));
var rowMain = cellTaskIDMain[0];
/*if it is a sub task*/
if(rowMain < 0){
doStatusChange(STATUS_CANCEL, null, HDR_COMPLETED_ON, sheet, sheet.getRange(row, timeTakenCol), time);
} else{
/*skip if description is missing*/
if (!isDescSet(rowMain, mainSheet)){
ignored += "[" + rowMain + "] ";
continue;
}
doStatusChange(STATUS_CANCEL, null, HDR_COMPLETED_ON, mainSheet, mainSheet.getRange(cellTaskIDMain[0], timeTakenCol), time);
deleteEmptySubtasks(taskid, sheet);
}
updateBLISSStatus(blissId, projectCode, STATUS_BL_WORKABLE);
}
info["ignored"] = ignored;
return info;
}
/*important function: creates gdrive folders, spec and rfi files*/
function doCreateTaskArtifacts(bCreateTaskSpec, bCreateRFIDoc, sheet, rng) {
if(sheet==null)
sheet = SpreadsheetApp.getActiveSheet();
var rng_sel;
if(rng==null)
rng_sel = sheet.getActiveRange();
else
rng_sel = rng;
var numRows = rng_sel.getNumRows();
var startRow = rng_sel.getRow();
var colID = getColumn(sheet, HDR_TASKID);
var colDesc = getColumn(sheet, HDR_DESCRIPTION);
var colFolder = getColumn(sheet, HDR_WIP_FOLDER);
var colTaskSpec = bCreateTaskSpec === false ? null : getColumn(sheet, HDR_TASK_SPEC);
var colRFIspec = bCreateRFIDoc === false ? null : getColumn(sheet, HDR_RFI_SPEC);
var colBliss = getColumn(sheet, HDR_BLISS);
var colProject = getColumn(sheet, HDR_PROJECT);
var userSheet = getCurrentUserSheet();
var taskParentFolder = DriveApp.getFolderById(ID_TASK_ARTIFACT_FOLDER_ROOT);
for (i = startRow; i < startRow + numRows; i++){
setDefaultProps(i, sheet);
/*check if WIP folder already exists*/
var taskid = sheet.getRange(i, colID).getValue();
var description = sheet.getRange(i, colDesc).getValue();
var foldername = getWIPFolderName(taskid);
var subFolders = taskParentFolder.getFolders();
var bExists = false;
var taskFolder = null;
while (subFolders.hasNext()) {
var subFolder = subFolders.next();
if (subFolder.getName() == foldername) {
taskFolder = subFolder;
break;
}
}
var blissID = sheet.getRange(i, colBliss).getValue();
var project = sheet.getRange(i, colProject).getValue();
var blSheetName = getBacklogSheet(project).getSheetName();
var value = "";
/*create WIP folder if it doesnt exist*/
if (taskFolder == null){