[英]How to delete all rows except the last 2 from a google sheet using a Script?
I need to delete all rows except the last 2 from a google sheet.我需要从谷歌表中删除除最后 2 行之外的所有行。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.