How can I remove the last N rows of the data range using Google Apps Script?
CodePudding user response:
Replace N with the number of rows you want to delete:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
sheet.deleteRows(lastRow-N-1, N)
CodePudding user response:
Try it this way if you just have data and no formulas
function removeLastN(n=5) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet1");
let vs = sh.getDataRange().getValues();
vs = vs.filter((r,i) =>i < (vs.length - n));
sh.clearContents();
sh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}