-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_columns_with_source.js
More file actions
67 lines (57 loc) · 2.83 KB
/
import_columns_with_source.js
File metadata and controls
67 lines (57 loc) · 2.83 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
// This Google Apps Script imports specific columns from multiple external Google Sheets into the active sheet.
// It skips header rows, appends the source name (title) next to each value, and writes the last update timestamp in cell D1.
function importSpecificColumns() {
const sources = [
{
id: '10lPwwwLWslnz8QE7ZKtGZNUIvktLamExRf6idPvrU6E', // ID таблиці з урл https://docs.google.com/spreadsheets/d/10lPwwwLWslnz8QE7ZKtGZNUIvktLamExRf6idPvrU6E
sheetName: 'Аркуш1', // Назва аркуша
columnLetter: 'B', // Яку колонку витягнути
title: 'selected donors' // Назва з якою буде пов'язуватись запис
},
{
id: '1yQnpVzfDqUIKt3sJd7GYj5jUYhAki552oyIEYHi9lVI',
sheetName: 'Donors Approve',
columnLetter: 'C',
title: 'Donors Approve'
},
{
id: '1yQnpVzfDqUIKt3sJd7GYj5jUYhAki552oyIEYHi9lVI',
sheetName: 'links 6 month',
columnLetter: 'A',
title: 'links 6 month'
}
];
// Відкрити активну таблицю
const targetSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const targetSheet = targetSpreadsheet.getSheetByName('Обрані колонки') || targetSpreadsheet.insertSheet('Обрані колонки'); // Створити або відкрити вказаний аркуш
targetSheet.clearContents(); // Очистити аркуш
targetSheet.getRange('A1').setValue('row domain');
targetSheet.getRange('B1').setValue('list');
let targetRow = 2;
sources.forEach(source => {
const sourceSpreadsheet = SpreadsheetApp.openById(source.id);
const sourceSheet = sourceSpreadsheet.getSheetByName(source.sheetName);
const colNumber = letterToColumn(source.columnLetter);
const lastRow = sourceSheet.getLastRow();
// Якщо є хоча б 2 рядки (тобто є що копіювати), копіюємо з 2 рядка (пропускаєм назву колонки)
if (lastRow > 1) {
let columnData = sourceSheet.getRange(2, colNumber, lastRow - 1).getValues(); // Починаємо з 2 рядка
columnData = columnData.map(row => [String(row[0]).toLowerCase()]);
const titlesArray = columnData.map(() => [source.title]);
targetSheet.getRange(targetRow, 1, columnData.length, 1).setValues(columnData);
targetSheet.getRange(targetRow, 2, titlesArray.length, 1).setValues(titlesArray);
targetRow += columnData.length;
}
});
// Записати дату й час оновлення в C1
const now = new Date();
targetSheet.getRange('C1').setValue(`Оновлено: ${now.toLocaleString()}`);
}
function letterToColumn(letter) {
let column = 0;
for (let i = 0; i < letter.length; i++) {
column *= 26;
column += letter.charCodeAt(i) - 'A'.charCodeAt(0) + 1;
}
return column;
}