forked from brucemcpherson/cUseful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSheetUtils.gs
61 lines (55 loc) · 2.21 KB
/
SheetUtils.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
/**
* Utils contains useful functions for working with sheets
* @namespace
*/
var SheetUtils = (function (ns) {
/**
* given a range and a property name, fill it with a value
* @param {Range} range the range
* @param {string} propertyName the property name
* @param {*|function} fillValue the value to fill with, or function to create a value
* @param {Range} [headerRange=] an optional range for the headers, default is first data row
* @return {range} for chaining
*/
ns.rangeFill = function (range , propertyName, fillValue, headerRange) {
// camel case up property name
var name = propertyName.slice(0,1).toUpperCase() + propertyName.slice(1);
if (typeof range['get'+name] !== typeof range['set'+name] ||
typeof range['set'+name] !== 'function') {
throw new Error (name + ' should be a property of a range with a getter and setter');
}
// we'll always need the values to pass to a function, and also get the current properties
var values = range.getValues();
// set up default headers
columnNames = headerRange ? headerRange.getValues()[0] : values[0];
if (columnNames.length != values[0].length) {
throw new Error ('headers are length ' + columnNames.length +
' but should be ' + values[0].length);
}
// these are the properties that will be set
var properties = name === 'Values' ? values : range['get'+name]();
// iterate
return range['set'+name](
values.map(function(row,rowIndex) {
return row.map(function(cell,colIndex) {
return typeof fillValue === 'function' ?
fillValue ({
value:cell,
propertyValue:properties[rowIndex][colIndex],
columnIndex:colIndex,
rowValues:row,
rowIndex:rowIndex,
propertyValues:properties,
values:values,
range:range,
propertyName:propertyName,
columnNames:columnNames,
columnName:columnNames[colIndex],
is:function(n) { return columnNames[colIndex] === n; }
}) : fillValue;
});
})
);
};
return ns;
}) (SheetUtils || {});