I've made a well-working script to copy cells *OnEdit (col16 on sheet 2) if any row in Col16 = "Yes") to last row of custom column on sheet 1(now Col4). But I have a Query based data on sheet 2, so my script copies data only when I type "Yes" by hands. That's why now I need to run this script by button, or maybe as a time-driven trigger. Can't get it to work by button. How to change the following script to make it possible to work by clicking on button?
function onEdit(e) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
if (sheet.getName() !== '2' || e.range.getColumn() !== 16 || e.value !== 'Yes') return;
var value = e.range.offset(0,-12,1,1).getValue();
sheet = e.source.getSheetByName('1');
var grd = sheet.getRange("D:D").getValues();
var maxIndex = grd.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
sheet.getRange(maxIndex+2,e.range.getColumn()-12,1,1).setValue(value);
}
UPDATE: I've tried to change my script like this, assigned it to button, and after clicking on button- it say "Running..... then Finished..." but it don't copy anything (but it works when it's OnEdit(e), and I m not getting any error. But it seems it's not enough just change "OnEdit(e)" to "copymissings()", I need to change something in code, but I don't know what exactly. Please help:
function copymissings() {
var s = SpreadsheetApp.getActive();
if (s.getName() !== '2' || range.getColumn() !== 16 || value !== 'Yes') return;
var value = range.offset(0,-12,1,1).getValues();
sht = s.getSheetByName('1');
var grd = sheet.getRange("D:D").getValues();
var maxIndex = grd.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
sht.getRange(maxIndex+2,range.getColumn()-12,1,1).setValues(value);
}
Visualisation: CLICK TO SEE GIF
if (s.getName() !== '2' || range.getColumn() !== 16 || value !== 'Yes') return;
range and value are not defined. – Rubén