-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgsTable.js
143 lines (138 loc) · 5.46 KB
/
gsTable.js
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
/**
* Creates table object based on Google sheet. Google sheet must have column descriptions in first row.
* Each table item represents a table row, item properties represent table columns.
* @param sheet {object} Source Google Sheet.
* @param defaultKey {string} [defaultKey = first column] Optional column name, indicates default key field.
* @returns GSTable {object} Object, containing table items.
*/
function gsTable(sheet, defaultKey){
var data = sheet.getDataRange().getValues();
var headers = data.shift();
var defaultKey = defaultKey || headers[0];
if (headers.indexOf(defaultKey) === -1) defaultKey = headers[0];
/**
* @property items Array of table items. Items properties represent sheet columns.
*/
this.items = data.map(function(row){
var item = {};
headers.map(function(header){
item[header] = row[headers.indexOf(header)];
});
return item;
});
/**
* Takes sheet cell value for indicated column description and returns matching table item.
* @param value {string} Value of sheet cell to match.
* @param keyField {string} [keyField = defaultKey] Optional name of column to match.
* @returns item {object} Object representing table item.
*/
this.getItem = function(value, keyField){
keyField = keyField || defaultKey;
var result = this.items.filter(function(item){
return item[keyField] === value;
});
if (result) return result[0];
}
/**
* Takes sheet cell value for indicated column description and returns all matching table items.
* @param value {string} Value of sheet cell to match.
* @param keyField {string} [keyField = defaultKey] Optional name of column to match.
* @returns items {object[]} Array of objects representing table items.
*/
this.getItems = function(value, keyField){
keyField = keyField || defaultKey;
var result = this.items.filter(function(item){
return item[keyField] === value;
});
return result;
}
/**
* Takes table item and appends it to source sheet.
* @param item {object} Item object. Object properties must match with sheet columns.
* @returns success {bool} Indicates if operation was successful.
*/
this.addItem = function(item){
var success = false;
var row = new Array(headers.length);
//populate row
for (var property in item) {
if (item.hasOwnProperty(property)) {
var index = headers.indexOf(property);
if (index > -1){
row[index] = item[property];
success = true;
}
}
}
if (success){
var range = sheet.getRange(this.items.length + 2, 1, 1, headers.length);
range.setValues([row]);
this.items.push(item);
}
return success;
}
/**
* Searches for keyValue in indicated column and updates value in target column.
* @param keyField {string} Name of column to search keyValue in.
* @param keyValue {string} Value to search for.
* @param field {string} Name of the column that will be updated.
* @param value {string} New cell value in specified column.
* @returns success {bool} Indicates if update was successful.
*/
this.updateItemValue = function(keyField, keyValue, field, value){
//validate arguments
if (headers.indexOf(field) === -1) return false;
if (headers.indexOf(keyField) === -1) return false;
//set values
for (var i = 0; i < this.items.length; i++){
if (this.items[i][keyField] === keyValue){
this.items[i][field] = value;
var rIndex = i + 2;
var cIndex = headers.indexOf(field) + 1;
var range = sheet.getRange(rIndex, cIndex);
range.setValue(value);
return true;
}
}
return false;
}
/**
* Matches sheet keyField column value with respective item property and updates all columns according to item properties.
* @param item {object} Item object. Object properties must match with sheet columns.
* @param keyField {string} [keyField = defaultKey] Optional name of sheet column and item property name to match.
* @returns success {bool} Indicates if update was successful.
*/
this.updateItem = function(item, keyField){
//validate arguments
keyField = keyField || defaultKey;
if (headers.indexOf(keyField) === -1) return false;
//get row index
var rIndex = 0;
for (var i = 0; i < this.items.length; i++){
if (this.items[i][keyField] === item[keyField]){
this.items[i] = item;
rIndex = i + 2;
break;
}
}
if (rIndex < 2) return false;
//transform item to row
var success = false;
var row = new Array(headers.length);
for (var property in item) {
if (item.hasOwnProperty(property)) {
var index = headers.indexOf(property);
if (index !== -1){
row[index] = item[property];
success = true;
}
}
}
if (!success) return success;
//set values
var range = sheet.getRange(rIndex, 1, 1, row.length);
var values = [row];
range.setValues(values);
return success;
}
}