-
Notifications
You must be signed in to change notification settings - Fork 0
/
#onEdit.gs
142 lines (114 loc) · 2.86 KB
/
#onEdit.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
/**
* Test function for onEdit. Passes an event object to simulate an edit to
* a cell in a spreadsheet.
*
* Check for updates: https://stackoverflow.com/a/16089067/1677912
*
* See https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
*/
function getEditObject(range) {
var e =
{
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : range,
value : range.getValue(),
authMode : "LIMITED"
};
return e;
}
function ObjectOnEdit(e)
{
var self = this;
// range
var r = e.range;
// var r = SpreadsheetApp.getActive().getActiveRange();
var numCols = r.getWidth();
var numRows = r.getHeight();
var boolCell = true;
if (numRows > 1 || numCols > 1) { boolCell = false; }
var numRow = r.getRow();
var numCol = r.getColumn();
var columns = [];
if (boolCell) { columns.push(numCol); }
else
{
for (var i = numCol; i <= numCol + numCols - 1; i++)
{
columns.push(parseInt(i));
}
}
self.range = r;
self.numCols = numCols;
self.numRows = numRows;
self.numRow = numRow;
self.numCol = numCol;
self.boolCell = boolCell;
self.columns = columns;
self.getLastRow = r.getLastRow();
self.getLastCol = r.getLastColumn();
// get value or values
// value [[value]];
// values [[val1, val2]];
this.getValues = function()
{
if (!self.boolCell) { return r.getValues(); }
var value = e.value;
if (value == undefined || typeof value == 'object') { value = r.getValue(); }
return [[value]];
}
// old value
var oldValue = e.oldValue;
if (oldValue == undefined) { oldValue = ''; }
if (typeof oldValue == 'object') { oldValue = ''; }
self.oldValue = oldValue;
// sheet and file
var sheet = r.getSheet();
var file = sheet.getParent();
var idFile = file.getId();
var nameSheet = sheet.getName();
var idSheet = sheet.getSheetId();
self.sheet = sheet;
self.file = file;
self.idFile = idFile;
self.nameSheet = nameSheet;
self.idSheet = idSheet;
}
/* check, if row of range is > then row of headers
results:
* 0 -- don't match
* 1 -- match
*/
function checkRowDv(row, rowHead) {
if (row <= rowHead) { return 0; }
return 1;
}
/* check, column matches
input:
* col - 10
* cols - 10 or [10,11] or "1,2,3"
* delim - ","
results:
* 0 -- don't match
* 1 -- match
*/
function checkColDv(col, cols, delim) {
// number
if (typeof cols == "number") {
if (cols == col) { return 1; }
return 0;
}
// array
var arr = [];
if (Array.isArray(cols)) {
arr = cols;
}
// string
if (typeof cols == "string") {
arr = cols.split(delim);
// convert into nums
arr = arr.map(Number);
}
if (arr.indexOf(col) != -1) { return 1; }
return 0;
}