-
Notifications
You must be signed in to change notification settings - Fork 2
/
sheethelpers.gs
51 lines (43 loc) · 1.16 KB
/
sheethelpers.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
/**
* Create the Property list Sheet
*/
function createSheet(name) {
// Get current spreadsheet and add our properties sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.insertSheet(name);
return sheet;
}
/**
* Get a sheet or create if doesn't exist by name and optional headers.
*/
function getOrCreateSheetByName(name, headers, clear){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(name);
// Create sheet if doesn't exist
if (sheet == null){
sheet = createSheet(name);
}
// Clear sheet if true
if (clear) {
sheet.clear();
}
// Add the headers to the sheet if the headers are defined and the sheet is empty
if (headers != null && isSheetEmpty(sheet)) {
var range = sheet.getRange(1, 1, 1, headers.length)
.setValues([headers]).setFontWeight('bold');
}
return sheet;
}
/**
* Helper for returning the property sheet
*/
function getMainSheet(headers, clear) {
const sheetName = "GA View Editor";
return getOrCreateSheetByName(sheetName, headers, clear);
}
/**
* Check if sheet is empty.
*/
function isSheetEmpty(sheet) {
return sheet.getDataRange().getValues().join("") === "";
}