-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilters-aux.gs
112 lines (97 loc) · 4.32 KB
/
filters-aux.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
/* Management Magic for Google Analytics
* Auxiliary functions for Filter Management
*
* Copyright ©2015 Pedro Avila ([email protected])
* Copyright ©2016 Gary Mu (Gary7135[at]gmail[dot]com)
***************************************************************************/
/**************************************************************************
* Adds a formatted sheet to the spreadsheet to faciliate data management.
* @param {boolean} createNew A boolean flag indicating whether to create a new sheet or format the current one
* @return {string} the formatted sheet name
*/
function formatFilterSheet(createNew) {
// Get common values.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
var d = new Date();
var sheetName = "Filters@"+ d.getFullYear()+'-'+ (d.getMonth()+1) +'-'+ d.getDate() +'_' + d.getHours()+':'+ d.getSeconds();
var cols = 20;
// normalize flag to create a new sheet
createNew = (createNew === undefined) ? false : true;
// Get the active sheet and its meta data
if (createNew) {
ss.insertSheet(sheetName, 0);
var sheet = ss.getActiveSheet();
} else {
var sheet = ss.getActiveSheet();
}
var numCols = sheet.getMaxColumns();
var deltaCols = numCols - cols;
// Add or remove columns based on the number of columns there should be
try {
if (deltaCols > 0) {
sheet.deleteColumns(cols, deltaCols);
} else if (deltaCols < 0) {
sheet.insertColumnsAfter(numCols, -deltaCols);
}
} catch (e) {
return Browser.msgBox(e.message);
}
// Set ranges.
var includeCol = sheet.getRange("A2:A");
var idCol = sheet.getRange("C2:C");
var typeCol = sheet.getRange("E2:E");
var fieldARequiredCol = sheet.getRange("M2:M");
var fieldBRequiredCol = sheet.getRange("P2:P");
var outputConstructorCol = sheet.getRange("R2:R");
var overrideOutputFieldCol = sheet.getRange("S2:S");
var caseCol = sheet.getRange("T2:T");
// Set header range.
var headerRange = sheet.getRange(1,1,1,sheet.getMaxColumns()); //num columns should be 20
ss.setNamedRange("header_row", headerRange);
// Set header values.
sheet.getRange("A1").setValue("Include in Update?");
sheet.getRange("B1").setValue("Account");
sheet.getRange("C1").setValue("ID");
sheet.getRange("D1").setValue("Name");
sheet.getRange("E1").setValue("Type");
sheet.getRange("F1").setValue("field");
sheet.getRange("G1").setValue("matchType");
sheet.getRange("H1").setValue("expressionValue");
sheet.getRange("I1").setValue("searchString");
sheet.getRange("J1").setValue("replaceString");
sheet.getRange("K1").setValue("fieldA");
sheet.getRange("L1").setValue("extractA");
sheet.getRange("M1").setValue("fieldARequired");
sheet.getRange("N1").setValue("fieldB");
sheet.getRange("O1").setValue("extractB");
sheet.getRange("P1").setValue("fieldBRequired");
sheet.getRange("Q1").setValue("outputToField");
sheet.getRange("R1").setValue("outputConstructor");
sheet.getRange("S1").setValue("overrideOutputField");
sheet.getRange("T1").setValue("caseSensitive");
// Set header formatting.
headerRange.setFontWeight("bold");
headerRange.setBackground("#4285F4");
headerRange.setFontColor("#FFFFFF");
// Index Column: protect & set background & font color.
idCol.protect().setDescription("prevent others from modifying the ids");
idCol.setBackground("#BABABA");
idCol.setFontColor("#FFFFFF");
// Include Column: modify data validation values.
var includeValues = ['✓', '✘'];
var includeRule = SpreadsheetApp.newDataValidation().requireValueInList(includeValues, true).build();
includeCol.setDataValidation(includeRule);
// Type Column: modify data validation values.
var typeValues = ['INCLUDE', 'EXCLUDE', 'LOWERCASE', 'UPPERCASE', 'SEARCH_AND_REPLACE', 'ADVANCED'];
var typeRule = SpreadsheetApp.newDataValidation().requireValueInList(typeValues, true).build();
typeCol.setDataValidation(typeRule);
// Set data validation for T/F columns (fieldA, fieldB, overrideOutputField, caseSensitive)
var tfValues = ['TRUE', 'FALSE'];
var tfRule = SpreadsheetApp.newDataValidation().requireValueInList(tfValues, true).build();
fieldARequiredCol.setDataValidation(tfRule);
fieldBRequiredCol.setDataValidation(tfRule);
overrideOutputFieldCol.setDataValidation(tfRule);
caseCol.setDataValidation(tfRule);
return sheet.getSheetName();
}