onEdit() specific cell doesn't fire the script
Explanation / Issue:
There are 2 things to note here:
-
You forgot to pass the event object as a parameter of the
CopyRange(e)
function. Without this parameter, the event object can't be used but your code uses it. -
Indeed you need an installable trigger because of
SpreadsheetApp.openById()
. You can indeed also create the trigger with the script you mentioned and this is also my recommended approach.
Solution:
Go to the export
spreadsheet, clear the previous code and add this code. Essentially, I am adding the event object into the CopyRange(e)
function and the trigger creation function. Then execute only the createOnEditTrigger
function:
function CopyRange(e) {
if (e.range.getA1Notation() === 'C1'){
var sss = SpreadsheetApp.openById('1ws8yg5_1k4ZZN2amfZ-cNvY-hUMUDGhbwnNPCGJhlAY'); //replace with source ID
var ss = sss.getSheetByName('export');
var range = ss.getRange('B3:C8');
var data = range.getValues();
var tss = SpreadsheetApp.openById('152JxdVi6Ssqz2ZZ65yDW5p5m0PnP63vApNgfP3jS_Pc'); //replace with destination ID
var ts = tss.getSheetByName('import'); //replace with destination Sheet tab name
ts.getRange(1, 2, 6,2).setValues(data); //you will need to define the size of the copied data see getRange()
}
}
function createOnEditTrigger() {
var ss = SpreadsheetApp.openById('1ws8yg5_1k4ZZN2amfZ-cNvY-hUMUDGhbwnNPCGJhlAY');
ScriptApp.newTrigger("CopyRange")
.forSpreadsheet(ss)
.onEdit()
.create();
}