Reputation: 319
The goal is that when I select two or more cells from the same column and click the "Delete" button, the rows of the selected cells are deleted.
So far I have created the following code:
function Eliminar_filas() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
spreadsheet.getRangeList([sheet.getRange(spreadsheet.getCurrentCell().getRow() + 1,1,1, sheet.getMaxColumns()).getA1Notation()]).activate();
spreadsheet.getActiveSheet().deleteRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
};
The problem is that I only get it to delete the row from one of the selected cells, and not all the rows from the selected cells.
There are no conditions or matches to be met, I just need that if for example I select cell B13, B15, B20, then rows 13, 15, 20 are deleted.
Now, in the range I cannot specify a specific interval because the selection of cells is random according to the product that I want to eliminate, that is, one day it could be cells B13, B15, B20, like another day it can be cells B1, B40, B80.
I've been struggling for many hours trying to find a solution, trying various methods, but without any success.
After an exhaustive search by google and stackoverflow, I have seen similar cases but always meeting some condition or coincidence and really, being a novice, I don't know how to adapt it to my needs.
Would someone around here be kind enough to help me with my code? What am I doing wrong? What is the best practice? Any help will be very appreciated!
And sorry for my English, I'm not good with it.
Thank you very much.
Upvotes: 1
Views: 2987
Reputation: 201593
I believe your goal as follows.
getActiveRangeList()
in Google Spreadsheet service. In your situation, I thought that this might be able to be used.When this point is reflected to a script, it becomes as follows.
function Eliminar_filas() {
SpreadsheetApp
.getActiveRangeList()
.getRanges()
.sort((a, b) => a.getRow() > b.getRow() ? -1 : 1)
.forEach(r => r.getSheet().deleteRows(r.getRow(), r.getNumRows()));
}
getActiveRangeList()
, and retrieve the ranges, and then, the retrieved ranges are sorted with the descending order, and each row is deleted.getNumRows()
, when you selected the multiple cells, this can be worked.Upvotes: 4