Reputation: 1
I need to delete all rows except the last 2 from a google sheet. The number of rows can vary. But I need always the last 2 rows.
How should be the script to do this?
Upvotes: 0
Views: 593
Reputation: 64072
In regards to your comment on the other answer, perhaps this will help you.
function myfunc101() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('DailyHelpDesk');
const sr = 2;//data start row
const nr = 2;//number of rows to leave at the bottom
const rg = sh.getRange(sr, 1, sh.getLastRow() - sr - nr + 1,sh.getLastColumn());
sh.deleteRow(sr,rg.getNumRows());
}
Upvotes: 1
Reputation: 312
// getting sheet to work on
var sheet = SpreadsheetApp.getActive().getSheetByName(<sheet name goes here>);
start = 1; // Hard coded row number from where to start deleting
howManyToDelete = sheet.getLastRow() - 2; // Leaving the last two rows
sheet.deleteRows(start, howManyToDelete);
Upvotes: 0