-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Utils_.gs
135 lines (115 loc) · 3.27 KB
/
Utils_.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
// JSHint - TODO
/* jshint asi: true */
/* jshint esversion: 6 */
(function(){"use strict"})()
// Utils_.gs
// =========
//
// Object template
var Utils_ = (function(ns) {
/*
Create a two-deep object from a 2D array. The first column is used as the
key and the value of each key is another object using the remaining headers
city pop height
---- --- ------
1 | city1 111 1111
2 | city2 222 2222
3 | city3 333 3333
{
city1: {
pop: 111,
height: 1111
},
city2: {
pop: 222,
height: 2222
},
city3: {
pop: 333,
height: 3333
},
}
*/
ns.createObjectFrom2DArray = function(sheet) {
const data = sheet.getDataRange().getValues()
const header = data.shift()
let object = {}
data.forEach(row => {
header.reduce((accumulator, currentValue, currentIndex) => {
var key = row[0];
if (currentIndex === 0) {
object[key] = {}
} else {
object[key][currentValue] = row[currentIndex]
}
return accumulator
}, {})
})
return object
}
/**
* Takes a sheet param and creates an array of json objects
* Keys to each json is based on the titles in the header row
*
* @param {Spreadsheet object} sheet Spreadsheet object (or 2D array)
*/
ns.getArrayOfObjects = function(sheet) {
const data = sheet.getDataRange().getValues()
const headers = data.shift()
return data.map(row => row.reduce((prev, curr,index) => {
prev[headers[index]] = curr
return prev
},{}))
}
/**
* Create an object where the keys are the header names and the value the header (zero) index
*
* @param {Sheet} sheet
* @param {number} headerRowNumber [OPTIONAL, DEFAULT = 1]
* @return {object}
*/
ns.getHeaderIndexes = function(sheet, headerRowNumber) {
if (headerRowNumber === undefined) headerRowNumber = 1
let headerColumnIndexes = {}
sheet
.getRange(headerRowNumber, 1, 1, sheet.getLastColumn())
.getValues()[0]
.forEach((headerName, index) => {headerColumnIndexes[headerName.trim()] = index})
return headerColumnIndexes
}
/**
* Search a 2D array
*
* @param Array valueArray - 2D array to seach
* @param object valueToFind
* @param columnIndex - column to search [DEFAULT: 0]
* @return rowIndex
*/
ns.findIn2DArray = function(valueArray, valueToFind, columnIndex) {
const rowIndex = valueArray.findIndex(row => row[columnIndex] === valueToFind)
if (rowIndex === -1) throw new Error(`Could not find ${valueToFind}`)
return rowIndex
}
/**
* Get the active spreadsheet, failing that the test one.
*
* @return {Spreadsheet} spreadsheet
*/
ns.getSpreadsheet = function() {
var spreadsheet = SpreadsheetApp.getActive()
if (spreadsheet === null) {
if (!PRODUCTION_VERSION_) {
spreadsheet = SpreadsheetApp.openById(TEST_SHEET_ID_)
} else {
throw new Error('No active spreadsheet')
}
}
return spreadsheet
}
ns.alert = function(message) {
var spreadsheet = SpreadsheetApp.getActive()
if (spreadsheet === null) return
SpreadsheetApp.getUi().alert(message)
}
return ns
})({})