const SETTINGS = {
bookId: '1la6H9omAvlKuMkt-jwZGkp23K0wvV-9CXK9Kq1S0sTw',
dataSheetNames: ['Лист1', 'Лист2'],
archiveSheetName: ['Вывод данных'],
};
function createTrigger() {
ScriptApp.getProjectTriggers().forEach(
(trigger) =>
trigger.getHandlerFunction() === 'saveData' &&
trigger.getEventType() === ScriptApp.EventType.CLOCK &&
(ScriptApp.deleteTrigger(trigger) || console.info(`Tirgger ${trigger.getUniqueId()} was deleted`))
);
ScriptApp.newTrigger('saveData').timeBased().atHour(17).everyDays(1).create();
}
function saveData() {
const book = SpreadsheetApp.openById(SETTINGS.bookId);
const date = new Date();
const sheet = book.getSheetByName(SETTINGS.archiveSheetName);
// console.log(book);
SETTINGS.dataSheetNames.forEach((name) => {
const sheetD = book.getSheetByName(name);
const values = sheetD.getRange('A2:B').getValues();
saveData_(sheet, date, values);
});
console.info(`saveData was called successful`);
}
/**
* @param {globalThis.SpreadsheetApp.Sheet} sheet
* @param {Date} x
* @param {any[][]} values
*/
function saveData_(sheet, x, values) {
const tz = sheet.getParent().getSpreadsheetTimeZone();
const x_ = Utilities.formatDate(x, tz, 'dd.MM.yyyy');
const data = sheet.getDataRange().getValues();
const ys = data.map((row) => row[0]);
const xs = data[0].map((cell) => (cell && cell.getTime ? Utilities.formatDate(cell, tz, 'dd.MM.yyyy') : ''));
let indexX = xs.indexOf(x_);
if (indexX === -1) indexX = sheet.getLastColumn();
const preData = data.map((row) => [row[indexX] || '']);
preData[0][0] = preData[0][0] || x;
console.log(preData);
values.forEach((row) => {
if (row[0] !== '') {
const index = ys.indexOf(row[0]);
if (index !== -1) {
preData[index] = [preData[index][0] || row[1]];
}
}
});
console.log(JSON.stringify(preData));
sheet.getRange(1, indexX + 1, preData.length, 1).setValues(preData);
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var csvMenuEntries = [{ name: 'export as csv files', functionName: 'userActionSaveAllSheetsAsCSV' }];
ss.addMenu('csv', csvMenuEntries);
}
function userActionSaveAllSheetsAsCSV() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var ssid = ss.getId();
var fileInDrive = DriveApp.getFileById(ssid);
var folderInDrive = fileInDrive.getParents().next().getId();
const urls = sheets.map((sheet) => {
const filename = `${sheet.getParent().getName()} ${sheet.getName()}.csv`;
const array = sheet.getDataRange().getValues();
const url = saveArrayToCSV_(array, filename, folderInDrive);
return { url, filename, folderInDrive };
});
console.log(JSON.stringify(urls, null, ' '));
}
/**
* @param {string[][]} array
* @param {string} filename
* @param {string} folderInDrive
* @returns {string} The file url
*/
function saveArrayToCSV_(array, filename, folderInDrive) {
const data = array.map((row) => row.join(';')).join('\n');
const url = DriveApp.getFolderById(folderInDrive)
.createFile(filename, data, 'text/csv')
.getDownloadUrl()
.replace('?e=download&gd=true', '');
return url;
}
function saveData2() {
const book = SpreadsheetApp.openById(
'1FUSSiDQoXyvKXfzYydoUUfcCGYq_TskpRiwfb28_1Z0'
);
const date = new Date();
const sheetTotal = book.getSheetByName('TOTAL');
const sheetGOOG = book.getSheetByName('NASDAQ:GOOG');
const valuesGOOG = sheetGOOG.getRange('C3:U3').getValues().map(row => [date, 'NASDAQ:GOOG', ...row]);
appendData_(sheetTotal, valuesGOOG);
const sheetDIS = book.getSheetByName('NYSE:DIS');
const valuesDIS = sheetDIS.getRange('C3:U3').getValues().map(row => [date, 'NYSE:DIS', ...row]);
appendData_(sheetTotal, valuesDIS);
console.info(`saveData was called successful`);
}
function appendData_(sheet, values) {
if (sheet.getLastRow() === sheet.getMaxRows())
sheet.appendRow([]);
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length)
.setValues(values);
}
saveData2
задан для дневного триггера=INDEX(IF(
VLOOKUP(C6:AG6;{D3:D4\F3:F4};2)>=C6:AG6;
VLOOKUP(C6:AG6;{D3:D4\C3:C4};2);
))