I’m surprised there’s an Add Row, but no Delete Row for the custom action builder.
Use Case:
I’m trying to make a one-tap follow / unfollow user function for a social network type of app, but it only works for one-tap follow. If you add Delete Row as an option, we’d be able to do one tap unfollow, enabling slick social network experiences
The way I normally handle deletions is as follows:
I have a column in my table called “Is Deleted?” (for simplicity, I just use a number column)
I present a “Delete” button to the users
When a user taps on delete, I increment the value of “Is Deleted?” by 1
I then use a visibility condition to immediately remove the item from view
In some cases, I’ll have a trigger function running in the background that removes any “Is Deleted?” rows from the GSheet.
In other cases, I’ll actually leave the row intact and include a “Deleted At” timestamp and “Deleted By” email address/user ID to identify the user that deleted the item, and when. Both of these get set in the same custom action as the “Is Deleted?” increment.
@wjcv06@Darren_Murphy what scripts do you use to automate deletion and how long do they take to kick in? Can you/do you have them running continuously eg. every second or every 5 seconds?
The problem isn’t creating a hack to “delete” a row. It’s to actually delete a row and keep our sheets clean. I have an app with over 30,000 rows and all the data is sitting at the bottom because this feature doesn’t exist.
I’m sure it will come soon but no idea what the reasoning behind it is.
It depends on the use case.
If I need the row removed ASAP, then I’ll use an On Change trigger.
If I don’t, then I’ll use a time based trigger and set it to run periodically to clean things up.
Here’s an example of a generic script that I use:
function remove_deleted_rows(sheet) {
var rows = sheet.getLastRow();
for (var row=rows; row>=2; row--) {
var deleted = sheet.getRange(row,2).getValue();
if (deleted) {
sheet.deleteRow(row);
}
}
}
One important point is that I ALWAYS use column 2 (B) for my Is Deleted? flag (Column A is reserved for RowID). This gives me the confidence to know I can safely use that same function to delete rows in any sheet, and not have to worry about accidentally deleting an entire sheet. I learned this lesson the hard way
Agreed. Definitely not a good user experience nor a good development practice to try and hack deleting a row. Seems so fundamental. I’m sure they’re working on it, but need to find a way around this for now.
@ray_d another thing I should point out about the script I posted above (which may not be immediately obvious) is that when processing a sheet, it starts from the bottom and works its way up. This is absolutely vital. If you start from the top and work down, then you run the risk of deleting the wrong rows. This is because as soon as you delete a row, the indexes of all the following rows will change, and so you have an immediate off by one error.
Working from the bottom up avoids this problem. When you delete a row, the row indexes below will still change. But because those rows have already been processed, it doesn’t matter.
In terms of timing, although using an On Change trigger is relatively fast, it will still take up to a minute or two before the change is reflected in the app.
This is why I use the ‘Is Deleted?’ flag + visibility approach. It allows you to give the impression that the deletion is instant, even though the row is still there.
@Darren_Murphy Do you use integromat? I am asking because some scenarios break with “error 400” when rows are deleted. This is preventing me from doing a delete row in integromat.
For Sheets, this has to do with the fact that the Sheet API only allows clearing, not deleting but as I have discussed with @Jonathan_Kohn in another thread, they should have this for Glide Tables.
I’ve done a little with Integromat, but never tried using it to delete spreadsheet rows (as I already have a script solution). I keep telling myself that I need to learn more about Integromat, as I’m sure once I do I can probably save myself a whole heap of coding…
My guess would be that error comes from Integromat, as I’ve never encountered it using Apps Script to delete rows. In fact, I just did a quick test using the same script I posted earlier, processing a sheet that contains several arrayformulas, and it executed without error.
You can do this with integromat with two steps. Search one row then delete this row by the row number then repeat.
You have to do this one by one because the row number changes for all rows behind the deleted one so you have to search again to get the next valid row number.
So with a web hook you could create a smoth and dangerous solution. What happens when two users delete at the same time…
But not if you use a glide table.
+1 for the delete action.
I guess you are a script person but for those of us who are still learning how to use them, there is an alternative which is have Integromat delete it for you.
Yes, Integromat has a “delete row” action that could do this as well.