-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets.js
101 lines (86 loc) · 3.07 KB
/
sheets.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
// Utility functions for google sheets documents
/**
* Returns the emptied cell
* @param {cell} cell
*/
function emptyCell(cell) {
return cell.setFormula('');
}
/**
* Provided a cell in a sheet, add to it a link to a provided URL
* Does not apply any styles to the cell
* @param {cell} cell
* @param {String} url full href, e.g. https://drive.google.com
* @param {String} [textContent] text inside the cell. Defaults to DOWNLOAD DOCUMENT
*/
function addLinkInCell(cell, url, textContent) {
const cellValue = `=HYPERLINK("${url}"; "${textContent || 'DOWNLOAD DOCUMENT'}")`;
cell.setFormula(cellValue);
}
function getFirstValueInNamedRange(namedRange) {
return [namedRange.getName(), namedRange.getRange().getDisplayValues()[0][0]];
}
function getNamedRangesByRegexMatch(regexp, namedRanges) {
return namedRanges.filter(namedRange => namedRange.getName().match(regexp));
}
function stringMatchesNamedRange(string, namedRanges) {
return typeof namedRanges.find(namedRange => namedRange.getName() === string) !== 'undefined';
}
function getNamedRangesByExactMatches(namedRanges, ...namesToMatch) {
const re = new RegExp(`${namesToMatch.join('|')}`);
return namedRanges.filter(namedRange => namedRange.getName().match(re));
}
function getEditorName(editorEmail) {
const NAMES = 'EDITORS_NAMES';
const EMAILS = 'EDITORS_EMAILS';
var editorNamesValues = SpreadsheetApp.getActiveSpreadsheet()
.getRangeByName(NAMES)
.getValues();
var editorEmailValues = SpreadsheetApp.getActiveSpreadsheet()
.getRangeByName(EMAILS)
.getValues();
var index = 0;
while (true) {
if (!editorEmailValues[index][0]) {
return '';
}
if (editorEmailValues[index][0] === editorEmail) {
return editorNamesValues[index][0];
}
index++;
}
}
/**
*
* @param {[namedRange]} namedRanges Array with all named ranges. Passed as argument to avoid fetching named ranges multiple times, an operation which takes a few tenths of a second
* @param {String} rangeName
* @return Range matching the input rangeName
*/
function getRangeByName(namedRanges, rangeName) {
const matchingRanges = getNamedRangesByExactMatches(namedRanges, rangeName);
if (matchingRanges.length === 0) {
throw new Error('Function getRangeByName: range name not found.');
}
return getNamedRangesByExactMatches(namedRanges, rangeName)[0].getRange();
}
function getCellDownload({ namedRanges, sheet }) {
const DOWNLOAD_SLIDES = 'DOWNLOAD_SLIDES';
const DEFAULT_DOWNLOAD_SLIDES = 'A1';
return stringMatchesNamedRange(DOWNLOAD_SLIDES, namedRanges)
? getRangeByName(namedRanges, DOWNLOAD_SLIDES)
: sheet.getRange(DEFAULT_DOWNLOAD_SLIDES);
}
function getCell({ sheetName, cellA1Notation }) {
return SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(sheetName)
.getRange(cellA1Notation)
.getCell(1, 1);
}
function logToCell({ cell, prefix = '', msg, printLogs = true }) {
if (typeof cell === 'undefined' || !printLogs) return;
if (prefix === '') {
cell.setValue(`${msg.toUpperCase()}`);
return;
}
cell.setValue(`${prefix.toUpperCase()}: ${msg.toUpperCase()}`);
}