Google script onEdit auto delete selected celds

Hello

Someone could help me by giving me an example of a googlse script where if a user fills in information in 1 cell it will automatically delete what was before.

Let’s say that:

A:1 - Cart

But if anyone modifies it, be in the car again or the cell be clean

1 Like

I’m not sure I read this right. Let me rephrase it:

  • You have a cell that already contains information.
  • You let people edit it, and upon edit, the cell will be cleaned or replaced with the new value?

Can you tell us more about your use case for this?

Hello, my friend!

Look, I’ve been looking at the forum to see what I want to do, but it doesn’t work for me.

I tried to create a google script.
But I find that the way the trigger should work is onEdit however I have read that through Glide you must use onChange for the script to work.

function onChange(e){

{

 if(SpreadsheetApp.getActiveSpreadsheet().getSheetName() == "Simulador")

//Poner número de columna que se va a modificar//
{
var range = e.range;
if(range.getColumn() == 4)

//Poner nombre de hoja y número de columna donde nos traerá la fecha//
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Simulador”).getRange(range.getRow() , 5).clearContent();
}
}

}

The previous one is the script I made but when I make a cell change from the App it is not edited, it is only edited if I do it myself by opening the google sheet. As I read and as I said you should use the onchange but I have no idea.

Firstly, rename your function. onChange should not be the name, let’s rename it to something like DeleteCell(e)

    function deleteCell(e){

{

 if(SpreadsheetApp.getActiveSpreadsheet().getSheetName() == "Simulador")
//Poner número de columna que se va a modificar//
{
var range = e.range;
if(range.getColumn() == 4)

//Poner nombre de hoja y número de columna donde nos traerá la fecha//
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Simulador”).getRange(range.getRow(), 5).clearContent();
}
}

}

Then navigate to Edit > Current project’s triggers.

image

Click “Add Trigger”

Choose the right function to run, then select event type “On change”.

image

I’m not sure I have fully understood your use case for this so if the script above doesn’t work then send me a message.

Hello, thank you for your answer.

Try to do it with the tiggers that Google gives the problem is that they detect any change in the sheet and I just need that if we say A:1 is modified (they write something) the content in other specific cells is deleted like B:2 :B3 and B:4. I tried another type of programming but it didn’t work:

function DeleteCell(e){
if(e.changeType == ‘EDIT’ || e.changeType == ‘EDIT’){

var sheet = SpreadsheetApp.getActive().getSheetByName(‘Simulator’);
sheet.getRange(‘D2’).clearContent();
sheet.getRange(‘Y2’).clearContent();
}
else {
return;
}
}

But it’s still changing in general.

Do you have a fixed list of cells that needs to be deleted?

If I only want 1 (D:1) cell to be deleted every time cell A:1 is changed

ezgif-1-aa4c9a67784a

Looking for something like this?

A further demonstration to show that it works with Glide edit.

ezgif-1-a9c0c82255f0

The script is:

function DeleteCell(e){
var r = e.source.getActiveRange();
var cell = r.getA1Notation();
if(cell == 'A2'){
  var sheet = SpreadsheetApp.getActive().getSheetByName('Clear Content');
  sheet.getRange('D2').clearContent();
  }
else {
return;
}
}

You got to insert an IF clause that gets the cell notation as well, to prevent other cells’ changes affect the destination cell.

Yes, that’s exactly what I want, but the information would have to be sent from an app created by Glide.
Because that’s where the problem arises that the macro doesn’t work

Yeah it’s in the second video. Refresh the thread to check it out.

Brother, thank you very much, that’s all I needed, it worked perfectly.

Thank you very much indeed

1 Like

Don’t hesitate to contact me when you need help again, I also sent a personal message!

1 Like