SpreadsheetApp.flush()
. Этот метод останавливает все расчеты и пересчитывает все, что находится у вас в Таблице. Вероятно, из-за него у вас и проблемы.Всё выделяется сразу. Теперь видно с каким диапазоном работаю. Намного более понятно что попадает в значения диапазона.
/**
*
*/
function runOnce() {
trigger_();
}
/**
*
*/
function trigger_() {
try {
triggerAction();
} catch (error) {
console.error(error.message, error);
} finally {
var hours = 10;
var minutes = 17;
var seconds = 56;
var now = new Date();
var nextTime = new Date();
nextTime.setHours(0, 0, 24 * 3600 + hours * 3600 + minutes * 60 + seconds);
var delta = nextTime.getTime() - now.getTime();
ScriptApp.newTrigger('trigger_')
.timeBased()
.after(delta)
.create();
}
}
/**
*
*/
function triggerAction() {
console.log("I'm fine");
}
triggerAction
- это то, что выполняет ваш скриптrunOnce
- это то, что вы должны запустить один раз при первом запуске вашего триггера. Другие настройки не требуютсяtrigger_
- это и триггер и конфигурация вашего триггераUtilities
поддерживали локали.DateTimeFormat
function myFormatDate(date) {
const y = new Intl.DateTimeFormat('ru',{
year: 'numeric'
}).format(date)
const m = new Intl.DateTimeFormat('ru',{
month: 'long'
}).format(date)
return `${m}-${y}`
}
function test(){
const date = new Date();
console.log(Utilities.formatDate(date, "GMT+3", "MMM-yyyy"));
console.log(myFormatDate(date));
}
"MMM-d"
не вернет год - вернет дату. getDisplayValues()
var drr = sheet.getRange(sheet.getLastRow(), 1, 1, 3).getDisplayValues()[0];
0.00%
, то вернет именно 0.00%
, если 1-02-20
, то 1-02-20
.flush()
SpreadsheetApp.flush();
/**
*
* @param {GoogleAppsScript.Events.SheetsOnEdit} e
*/
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
SpreadsheetApp.getActive().toast(['Лист'].indexOf(activeSheet.getName()));
if (['Лист'].indexOf(activeSheet.getName()) == -1) return;
var r = SpreadsheetApp.getActiveRange();
var cols = r.getColumn();
if (cols == 6) {
// если изменяем 6 колонку, то тригер работает и:
var cell1 = activeSheet.getRange(2, 2); // во второй колонке берет данные из второй строки (там лежит формула)
var destination1 = activeSheet.getRange(activeSheet.getLastRow(), 2); // находит последнюю строку с данными и выбирает вторую колонку
cell1.copyTo(destination1); // копирует из второй строчки в последнюю
var cell2 = activeSheet.getRange(2, 3); // тоже самое но только для 3 колонки
var destination2 = activeSheet.getRange(activeSheet.getLastRow(), 3); // тоже самое но только для 3 колонки
cell2.copyTo(destination2); // тоже самое но только для 3 колонки
var cell3 = activeSheet.getRange(2, 5); // тоже самое но только для 5 колонки
var destination3 = activeSheet.getRange(activeSheet.getLastRow(), 5); // тоже самое но только для 5 колонки
cell3.copyTo(destination3); // тоже самое но только для 5 колонки
var cell4 = activeSheet.getRange(2, 8); // тоже самое но только для 8 колонки
var destination4 = activeSheet.getRange(activeSheet.getLastRow(), 8); // тоже самое но только для 8 колонки
cell4.copyTo(destination4); // тоже самое но только для 8 колонки
SpreadsheetApp.flush();
destination1.copyTo(
destination1,
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
false
); // во второй колонке меняет формулу на значение
destination2.copyTo(
destination2,
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
false
); // в третьей колонке меняет формулу на значение
destination3.copyTo(
destination3,
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
false
); // в пятой колонке меняет формулу на значение
destination4.copyTo(
destination4,
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
false
); // в восьмой колонке меняет формулу на значение
SpreadsheetApp.getActive().toast('Готово');
}
}
var MENU = [
{
caption: 'Пункт меню 1',
functionName: 'itemMenu',
},
{
caption: 'Пункт меню 2',
functionName: 'itemMenu',
},
{
caption: 'Пункт меню 3',
functionName: 'itemMenu',
},
];
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Test');
MENU.forEach(function(item, i) {
menu.addItem(item.caption, item.functionName + i);
});
menu.addToUi();
}
itemMenu
работает как-то такfunction itemMenu(e) {
var caption = e.item.caption;
var order = e.order;
Browser.msgBox(
Utilities.formatString('Был нажат %sй пункт меню: %s', order + 1, caption)
);
}
(function(self) {
MENU.forEach(function(item, i) {
self[item.functionName + i] = function() {
return self[item.functionName]({ item: item, order: i });
};
});
})(this);
function onEdit() {
run2();
}
function run2() {
/* Remove dash */
var sheet = SpreadsheetApp.getActiveSheet();
if (sheet.getName() === 'Журнал вода данных') return;
var archive = SpreadsheetApp.getActive().getSheetByName('Журнал вода данных');
var action = function(values, i, i2) {
var data = values.slice(i, i + i2);
archive
.getRange(archive.getLastRow() + 1, 1, data.length, data[0].length)
.setValues(data);
};
var condition = function(values, i) {
var row = values[i];
return (
i > 0 && row[0] !== '' && row[1] !== '' && row[3] !== '' && row[5] !== ''
);
};
deleteRowsByConditional_(sheet, condition, action);
}
function deleteRowsByConditional_(sheet, condition, action) {
sheet
.getDataRange()
.getValues()
.forEach(
function(_, i, arr) {
var j = arr.length - i - 1;
if (this.condition.apply(null, [arr, j])) {
this.isContinue++;
if (j > 0) return;
}
if (this.isContinue > 0) {
var prevPos = j + 1; // It's reversed
if (action) action(arr, prevPos, this.isContinue);
this.sheet.deleteRows(prevPos + 1, this.isContinue);
this.isContinue = 0;
return;
}
return;
},
{ sheet: sheet, condition: condition, isContinue: 0 }
);
}
run2()
к меню.Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data, specifically the following:
Supported services Notes Cache Works, but not particularly useful in custom functions HTML Can generate HTML, but cannot display it (rarely useful) JDBC Language Lock Works, but not particularly useful in custom functions Maps Can calculate directions, but not display maps Properties Spreadsheet Read only (can use most get*()
methods, but notset*()
).
Cannot open other spreadsheets (SpreadsheetApp.openById()
orSpreadsheetApp.openByUrl()
).URL Fetch Utilities XML