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?
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());
}
// 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.