forked from zmandel/workflow-apps-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.gs
100 lines (86 loc) · 3.06 KB
/
code.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
//MODEL
//
function updateColumnA(ss, sheet, rowStart, rowEnd) {
const startRow = Math.max(sheet.getFrozenRows() + 1, 2, rowStart); //assumes at least one row for header. considers more if frozen
const lastRow = rowEnd || sheet.getLastRow();
assert(lastRow >= startRow);
const firstColIndex = g_firstColIndex;
const lastColIndex = sheet.getLastColumn();
assert(lastColIndex >= firstColIndex);
var rangeA = sheet.getRange(startRow, 1, lastRow - startRow + 1, 1);
const valuesAOld = rangeA.getValues();
const formulasAOld = rangeA.getFormulas();
const bkAOld = rangeA.getBackgrounds();
const backgrounds = sheet.getRange(startRow, firstColIndex, lastRow - startRow + 1, lastColIndex - firstColIndex + 1)
.getBackgrounds();
if (g_bDebug)
log("startRow:" + startRow + " lastRow:" + lastRow);
var dataANew = {
bk: [],
colorFont: [],
value: [],
bClearedPending: false,
bChanged: false,
};
processRows(sheet, startRow, firstColIndex, rangeA, backgrounds, valuesAOld, formulasAOld, bkAOld, dataANew);
if (dataANew.bChanged) {
rangeA.setBackgrounds(dataANew.bk);
rangeA.setFontColors(dataANew.colorFont)
rangeA.setValues(dataANew.value);
if (dataANew.bClearedPending)
ss.toast("💙");
if (g_bDebug)
ss.toast(dataANew.value.length);
} else {
if (g_bDebug)
ss.toast(0);
}
}
function processRows(sheet, startRow, firstColIndex, rangeA, backgrounds, valuesAOld, formulasAOld, bkAOld, dataANew) {
const idSheet = sheet.getSheetId();
const formulasOld = rangeA.getFormulas();
assert(formulasOld.length == backgrounds.length);
assert(formulasOld.length == valuesAOld.length);
assert(valuesAOld.length == bkAOld.length);
assert(valuesAOld.length == formulasAOld.length);
// ↓
for (var i = 0; i < backgrounds.length; i++) {
const row = backgrounds[i];
var columnColoredFirst = null;
var colorFont = "#000000";
var bk = "#ffffff";
var val = valuesAOld[i][0];
if (val) {
// →
for (var j = 0; j < row.length; j++) {
if (row[j] && row[j] !== "#ffffff") {
columnColoredFirst = firstColIndex + j;
var rangeCellFound = sheet.getRange(startRow + i, columnColoredFirst);
const coloredCellAddress = rangeCellFound.getA1Notation();
colorFont = rangeCellFound.getFontColorObject().asRgbColor().asHexString();
bk = row[j];
val =
'=HYPERLINK("#gid='
+ idSheet + '&range=' + coloredCellAddress + '", "' + val + '")';
break;
}
}
}
if (!columnColoredFirst && formulasOld[i][0])
dataANew.bClearedPending = true;
dataANew.bk.push([bk]);
dataANew.colorFont.push([colorFont]);
dataANew.value.push([val]);
if ((val != valuesAOld[i][0] && val != formulasAOld[i][0]) || (bk || "").toLowerCase() != (bkAOld[i][0] || "").toLowerCase()) {
if (g_bDebug) {
log(i);
log(valuesAOld[i][0]);
log(formulasAOld[i][0]);
log(val);
log(bkAOld[i][0]);
log(bk);
}
dataANew.bChanged = true;
}
}
}