-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_of_cell_change.js
More file actions
64 lines (49 loc) · 2.34 KB
/
date_of_cell_change.js
File metadata and controls
64 lines (49 loc) · 2.34 KB
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
function onEdit(e) {
try {
const sheet = e.source.getActiveSheet();
const editedRange = e.range;
// === Налаштування ===
const SHEETS_CONFIG = {
"Аркуш1": { // назва аркуша
watchColumnName: "Status", // назва колонки, яку відстежуємо
dateColumnName: "Date_Last_Checked" // назва колонки, куди ставимо дату
},
// "Аркуш2": {
// watchColumnName: "Status",
// dateColumnName: "Date_Last_Checked"
// }
};
const headerRowIndex = 1; // Номер рядку в якому заголовки
// === Налаштування кінець ===
const sheetName = sheet.getName();
if (!SHEETS_CONFIG[sheetName]) return;
const { watchColumnName, dateColumnName } = SHEETS_CONFIG[sheetName];
const headers = sheet.getRange(headerRowIndex, 1, 1, sheet.getLastColumn()).getValues()[0]
.map(h => h ? String(h).trim() : "");
const watchColIndex = headers.indexOf(watchColumnName) + 1;
const dateColIndex = headers.indexOf(dateColumnName) + 1;
if (watchColIndex < 1 || dateColIndex < 1) return;
const startRow = editedRange.getRow();
const startCol = editedRange.getColumn();
const numRows = editedRange.getNumRows();
const numCols = editedRange.getNumColumns();
if (startRow + numRows - 1 <= headerRowIndex) return;
// Якщо змінено діапазон і в нього входить колонка, що слідкується
if (watchColIndex < startCol || watchColIndex > (startCol + numCols - 1)) {
return;
}
// Для кожного редагованого рядка поставимо дату у колонку Дата Статусу
const nowStr = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd.MM.yyyy");
// Масово збираємо значення, щоб мінімізувати звернення до API
const outRange = sheet.getRange(startRow, dateColIndex, numRows, 1);
const outValues = outRange.getValues();
for (let r = 0; r < numRows; r++) {
const currentRow = startRow + r;
if (currentRow <= headerRowIndex) continue;
outValues[r][0] = nowStr;
}
outRange.setValues(outValues);
} catch (err) {
Logger.log("onEdit error: " + err);
}
}