-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_new_row.js
More file actions
148 lines (117 loc) · 5.4 KB
/
move_new_row.js
File metadata and controls
148 lines (117 loc) · 5.4 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
function syncReadyForMove() {
const SOURCE_SPREADSHEET_ID = "ID Табл. №1"; // ID Табл. №1
const TARGET_SPREADSHEET_ID = "ID Табл. №1"; // ID Табл. №2
const sourceSheetName = "назва аркушу в Табл. №1"; // назва аркушу в Табл. №1
const targetSheetName = "назва аркушу в Табл. №2"; // назва аркушу в Табл. №2
// Назви колонок
const statusColumnName = "Назва колонки зі статусом"; // назва колонки зі статусом
const uniqueColumnName = "Назва колонки з унікальним значенням"; // унікальний ідентифікатор
// значення які задіяні в колонках і робиться перевірка по ним, якщо це значення є то буде переносити в табл2
const statusCellValue = "ready"; // значення з колонки зі статусом
// Які поля переносимо
const columnsToCopy = [
"Lang",
"Topic",
"Site",
];
const FIRST_HEADER_ROW = 1; // заголовки в 1-му рядку
const FIRST_DATA_ROW = 2; // дані починаються з 2-го рядка
const sourceSS = SpreadsheetApp.openById(SOURCE_SPREADSHEET_ID);
const targetSS = SpreadsheetApp.openById(TARGET_SPREADSHEET_ID);
const sourceSheet = sourceSS.getSheetByName(sourceSheetName);
const targetSheet = targetSS.getSheetByName(targetSheetName);
// ---------- 1. Заголовки з Табл.1 ----------
const lastColumnSource = sourceSheet.getLastColumn();
const sourceHeaders = sourceSheet
.getRange(FIRST_HEADER_ROW, 1, 1, lastColumnSource)
.getValues()[0];
const statusColumnIndexSource = sourceHeaders.indexOf(statusColumnName) + 1;
const uniqueColumnIndexSource = sourceHeaders.indexOf(uniqueColumnName) + 1;
if (statusColumnIndexSource === 0 || uniqueColumnIndexSource === 0) {
Logger.log("Не знайдено колонку статусу в джерельному листі. Перевір назви заголовків.");
Logger.log("sourceHeaders: " + JSON.stringify(sourceHeaders));
return;
}
const sourceColIndexByName = {};
sourceHeaders.forEach((name, idx) => {
if (name) {
sourceColIndexByName[name] = idx + 1;
}
});
// ---------- 2. Дані з Табл.1 ----------
const lastRowSource = sourceSheet.getLastRow();
if (lastRowSource < FIRST_DATA_ROW) {
Logger.log("У джерельному листі немає даних.");
return;
}
const numRowsSource = lastRowSource - FIRST_DATA_ROW + 1;
const sourceData = sourceSheet
.getRange(FIRST_DATA_ROW, 1, numRowsSource, lastColumnSource)
.getValues();
// ---------- 3. Заголовки в Табл.2 ----------
const lastRowTarget = targetSheet.getLastRow();
const lastColumnTarget = targetSheet.getLastColumn();
if (lastRowTarget < FIRST_HEADER_ROW) {
Logger.log("У цільовому листі немає заголовків. Перевір, що заголовки в 2-му рядку.");
return;
}
const targetHeaders = targetSheet
.getRange(FIRST_HEADER_ROW, 1, 1, lastColumnTarget)
.getValues()[0];
const targetColIndexByName = {};
targetHeaders.forEach((name, idx) => {
if (name) {
targetColIndexByName[name] = idx + 1;
}
});
const uniqueColumnIndexTarget = targetHeaders.indexOf(uniqueColumnName) + 1;
if (uniqueColumnIndexTarget === 0) {
Logger.log("Не знайдено колонку. Перевір назви заголовків.");
Logger.log("targetHeaders: " + JSON.stringify(targetHeaders));
return;
}
let targetData = [];
if (lastRowTarget >= FIRST_DATA_ROW) {
const numRowsTarget = lastRowTarget - FIRST_DATA_ROW + 1;
targetData = targetSheet
.getRange(FIRST_DATA_ROW, 1, numRowsTarget, lastColumnTarget)
.getValues();
}
const existingIds = new Set(
targetData
.map(row => row[uniqueColumnIndexTarget - 1])
.filter(v => v !== "" && v !== null && v !== undefined)
);
// ---------- 4. Формуємо рядки для додавання ----------
const rowsToAppend = [];
for (let i = 0; i < sourceData.length; i++) {
const row = sourceData[i];
const status = row[statusColumnIndexSource - 1];
const uniqueValue = row[uniqueColumnIndexSource - 1];
const normalizedStatus = (status || "").toString().trim().toLowerCase();
const normalizedAgency = (agency || "").toString().trim().toLowerCase();
if (normalizedStatus === statusCellValue.toLowerCase() &&
uniqueValue) {
if (!existingIds.has(uniqueValue)) {
const newRow = new Array(targetHeaders.length).fill("");
columnsToCopy.forEach(colName => {
const srcIdx = sourceColIndexByName[colName];
const tgtIdx = targetColIndexByName[colName];
if (srcIdx && tgtIdx) {
newRow[tgtIdx - 1] = row[srcIdx - 1];
}
});
rowsToAppend.push(newRow);
existingIds.add(uniqueValue);
}
}
}
// ---------- 5. Запис в Табл.2 ----------
if (rowsToAppend.length > 0) {
const startRow = lastRowTarget + 1;
targetSheet
.getRange(startRow, 1, rowsToAppend.length, targetHeaders.length)
.setValues(rowsToAppend);
}
Logger.log("Готово. Додано рядків: " + rowsToAppend.length);
}