Trigger script onEdit event from App

Hi
I have written a script which needs to run when ever user enter new data. Pl let me know how to achieve that.

I have solved this issue with onChange(e)

2 Likes

Hello
I cannot recover data with onEdit (e). All of my data seems undefined. Could you give me a sample code that works to inspire me
thanks in advance

function onEdit(e)
{  

 var range = e.range;
  
Logger.log("currentRange"+ range)
Logger.log("currentSource"+ e.source)
Logger.log("value"+ e.value)
Logger.log("Oldvalue"+ e.oldvalue)
}

As you can see from the post above, the onEdit(e) event trigger does not work with Glide, only the onChange(e) trigger does.

Thank you
do you have a little bit of code to find out which cell has been modified?

Simply change the name of function to onChange(e) and assign spreadsheet trigger. Now your code will work with Glide.

Thank you
It is exactly what i do but range is undefined as e.source or e.oldvalue
Can you help me

The (e) will not return specific cell information, only these triggers:

changeType The type of change ( EDIT , INSERT_ROW , INSERT_COLUMN , REMOVE_ROW , REMOVE_COLUMN , INSERT_GRID , REMOVE_GRID , FORMAT , or OTHER ).

Ok but How to know that cell changed ??

Glide does not pass that to your spreadsheet. You’ll need to write a script that captures that something in the whole spreadsheet has changed, whether it’s adding or removing a row or column or any of the triggers I mentioned above. Then in the script, you’ll reference a change that may be different than the last time an edit was made.

I’ll give you an example script in my next post.

function trailerDelete(e) { //This is the onChange(e) trigger
var app = SpreadsheetApp;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

var targetSheet = app.getActiveSpreadsheet().getSheetByName(“Report”);
// You can replace “Report” in the above line, to the sheet that you are referencing.

//Logger.log(targetSheet.getRange(2, 2).getValue() + " Test");
if (e.changeType==“EDIT”){
for (var i = 2; i < 6;i++){
if (targetSheet.getRange(i,2).getValue() ==""){
targetSheet.getRange(i, 3).clear();
targetSheet.getRange(i, 4).clear();
targetSheet.getRange(i, 5).clear();
// The 3 examples above are the columns and corresponding row that need to be //deleted
}
}
}
}

The example above is what I use to clear a set of values when I check to see if a cell is empty. It iterates through my entire sheet.

I must tell you that the script can take time to run depending on the size of your sheet and how much data you are trying to edit. Also, there is the Glide/Google lag if your account is not pro.

You may use getActiveCell() to identify the cell where modifications done. But one thing, for simple editing purposes you do not require all this information. Glide will automatically edit current row.

I go step by step by testing the code sent.

the ligne 8 is :"var targetSheet = app.getActiveSpreadsheet().getSheetByName(“Suivi”);

"SyntaxError: Invalid or unexpected token (ligne 8, fichier “Focus.gs”)"

I don’t understand what could be the problùme
1 function traitement(e)
2 {
3 Logger.log(‘change’);
4 var sheet = spreadsheet.getActiveSheet();
5 var app = SpreadsheetApp;
6 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
7
8 var targetSheet = app.getActiveSpreadsheet().getSheetByName(“Suivi”);

}

![Capture d’écran 2020-04-30 Ă  15.29.45|690x239](upload://o7OqlAEicLAJsCXHX2M2ayNJ5I9.png)

Thank you

Hello, I copied and pasted your code and setup pages to match and the code wont even save. Error line 5. Unexpected or Invalid token. So the reason for this error if anyone else from the US attempt to copy and paste is because the quotation marks used are different from ours, they have to be replaced with our quotations marks. They even appear slightly different. (US - " ", the ones in the code - “ “), so be mindful if you use this solution. Also this applies to some code elsewhere on the forums where a single apostrophe is allowed in other countries where full quotes are required in the US.

function TestFuncion_onChange(e) {
    const ss = e.source;
    const range = ss.getActiveRange();

  if(e.changeType === 'EDIT'){
    Logger.log("currentCell: "+ range.getA1Notation());
    Logger.log("value: "+ range.getValue());         
  }
}
1 Like