I have a little code sample to highlight the difference between an object that I created and the trigger event object. In the trigger event object I can use the term e.range along with the standard range class functions and it works fine. However, I don't really know how to create a psuedo event object so that the range term is more than just the name of a property.
function onEdit(e) {
console.log(JSON.stringify(e));
var sh=e.range.getSheet();//e.range is a real range object
if(sh.getName()=='Sheet2' && e.range.columnStart>1) {
sh.getRange(1,1).setValue(JSON.stringify(e)+ '\n' + e.range.getA1Notation());
var rg=sh.getRange(e.range.getA1Notation());//e.range is a real range object
var myObj={value:rg.getValue,range:{columnStart:e.range.columnStart,rowStart:e.range.rowStart,columnEnd:e.range.columnEnd,rowEnd:e.range.rowEnd}};
console.log(JSON.stringify(myObj));
try{
sh.getRange(2,1).setValue(JSON.stringify(myObj)+ '\n' + myObj.range.getA1Notation());//myObj.range is not a real range object
}
catch(m) {
console.log('Error: ' + m);
}
try{
sh.getRange(3,1).setValue(e.range.getValue());
}
catch(n) {
console.log('Error: ' + n);
}
}
}
If you edit Sheet2 appropriately you will get the following Stack Driver Logs:
Stackdriver logs
May 29, 2020, 1:21:30 PM Debug {"source":{},"value":"55","user":{"email":"","nickname":""},"range":{"columnEnd":5,"columnStart":5,"rowEnd":3,"rowStart":3},"authMode":"LIMITED"}
May 29, 2020, 1:21:30 PM Debug {"range":{"columnStart":5,"rowStart":3,"columnEnd":5,"rowEnd":3}}
May 29, 2020, 1:21:30 PM Debug Error: TypeError: myObj.range.getA1Notation is not a function
Notice above I cannot use myObj.range with the getA1Notation() method. (note: I didn't actually expect it to work...I'd like to know how to build the object correctly so that it will work and I haven't stumbled on the correct explanation yet.
So the thing I'd like to learn is how to create a psuedo event object so I can use myObj.range in the same way as I can use e.range.
I feel real bad after Tanaike has gone to so much trouble. But my real goal is to be able to create the event object precisely as it is created by the server. So that I could test onEdit()s with a test function calling the onEdit(e) rather than requiring actual edits. And more importantly I'd really just like know how to build such an object.
I think this says what I'm shooting for. I want to know how to create the event object precisely as it is provided to us from the onEdit trigger including having e.range to be an object that is equivalent to Class Range and thus capable of utilizing any of the methods found here. I hope that's possible. I apologize for these late additions but they were prompted from some of Tanaike's questions. I'll probably need to review the question again and make it more clear.
This is some code I'm working on and it's far from complete but this is the sort of thing one might do with the knowledge of the answer to my question.
function onXditTest(row) {
var row=row||2;
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('OnEditTest');
const v=sh.getRange(row,1,1,sh.getLastColumn()).getValues()[0];
const xsh=ss.getSheetByName(v[5]);
const xrg=xsh.getRange(v[0],v[2],v[1]-v[0]+1,v[3]-v[2]+1);
const evobj={range:{rowStart:v[0],rowEnd:v[1],columnStart:v[2],columnEnd:v[3]},value:v[4],range:xrg,source:ss};
onXdit(evobj);
}
The real question is how to make evobj so that evobj.range is an object of Class Range and at the sametime have available evobj.range.columnStart, evobj.range.rowStart, evobj.range.columnEnd and evobj.range.rowEnd so that the code under test behaves the same way in onEdit(e) as it does in onXdit(e)
function onXdit(e) {//changed name so edits dont trigger the function
e.source.toast('Entry');
console.log(JSON.stringify(e));
const sh=e.range.getSheet();
if(sh.getName()=="Sheet2" && e.range.columnStart==4 && e.value=="TRUE" ) {
e.source.toast('Past Condition');
e.range.setValue("FALSE");
}
}



