# Google script onEdit auto delete selected celds

**URL:** <https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573>\
**Category:** Ask for Help\
**Created:** [May 22, 2020, 12:11am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573 "2020-05-22T00:11:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flud](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/flud/32/7645_2.png) [@Flud](https://community.glideapps.com/u/Flud)\
**Post date:** [May 22, 2020, 12:11am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/1 "2020-05-22T00:11:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 22, 2020, 12:22am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/2 "2020-05-22T00:22:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Flud](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/flud/32/7645_2.png) [@Flud](https://community.glideapps.com/u/Flud)\
**Post date:** [May 22, 2020, 2:20am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/3 "2020-05-22T02:20:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 22, 2020, 2:27am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/4 "2020-05-22T02:27:47Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/7/72b3107e6c334051551fb2b59d4b4d3290825807.png)

Click “Add Trigger”

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/f/f42cbb726dce28f6caf1759d5de8834cdfb3bae1.png)

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

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/c/c24dc773288d66ed909bd4850278ce37fc113550.png)

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.

---

<div class="post-metadata">

**Author:** ![Flud](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/flud/32/7645_2.png) [@Flud](https://community.glideapps.com/u/Flud)\
**Post date:** [May 22, 2020, 3:30am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/5 "2020-05-22T03:30:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 22, 2020, 3:52am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/6 "2020-05-22T03:52:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Flud](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/flud/32/7645_2.png) [@Flud](https://community.glideapps.com/u/Flud)\
**Post date:** [May 22, 2020, 3:54am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/7 "2020-05-22T03:54:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 22, 2020, 4:09am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/8 "2020-05-22T04:09:14Z")

</div>

![ezgif-1-aa4c9a67784a](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/6/6991427a695e0cce3484a4f58cd8a85ed9b77544.gif)

Looking for something like this?

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 22, 2020, 4:15am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/9 "2020-05-22T04:15:49Z")

</div>

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

![ezgif-1-a9c0c82255f0](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/2X/c/c7b9dfb730dd7666573559030d0240c25c861ce4.gif)

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.

---

<div class="post-metadata">

**Author:** ![Flud](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/flud/32/7645_2.png) [@Flud](https://community.glideapps.com/u/Flud)\
**Post date:** [May 22, 2020, 4:20am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/10 "2020-05-22T04:20:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 22, 2020, 4:20am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/11 "2020-05-22T04:20:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Flud](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/flud/32/7645_2.png) [@Flud](https://community.glideapps.com/u/Flud)\
**Post date:** [May 22, 2020, 9:01pm UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/12 "2020-05-22T21:01:36Z")

</div>

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

Thank you very much indeed

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 23, 2020, 12:53am UTC](https://community.glideapps.com/t/google-script-onedit-auto-delete-selected-celds/9573/13 "2020-05-23T00:53:49Z")

</div>

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