简体   繁体   English

如何使用脚本从谷歌工作表中删除除最后 2 行之外的所有行?

[英]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.

相关问题 如何使用Google脚本在Google电子表格中除特定列以外的所有工作表大写? - How to capitalize all sheet except specific columns on Google spreadsheet using Google Script? 您无法删除工作表 Google Sheets Script 上的所有行 - You can't delete all the rows on the sheet Google Sheets Script 使用 Apps 脚本从 Google 表格中的另一张表格中删除包含值的行 - Delete rows containing value from another sheet in Google Sheets using Apps Script 使用带有 gmail 搜索词和工作表中的多行的 Google 表格删除带有脚本的 gmail,以进行批量处理 - Delete gmails with script using google sheet with gmail search terms and multiple rows in sheet for bulk processing 如果选中复选框,则使用脚本从Google表格中的行生成文档 - Using a script to generate docs from rows in Google Sheet if checkbox is checked 如何使用 Apps 脚本从 Google 表格中的表格末尾获取 5 行? - How do I get 5 rows from the end of my sheet in Google Sheets using Apps Script? 如何根据 Google App Script 中的定义删除旧条目的 Google Sheet 行? - How to delete Google Sheet rows which are old entries as per definition in Google App Script? 从谷歌表中自动删除单词的脚本 - Script to automatically delete words from google sheet 如何使用 Google Apps Script 在来自 Google 表格的多行中拆分 Google 幻灯片表中的数据(基于字符限制)? - How to split data in a Google Slides Table (based on character limit) in multiple rows coming in from a Google Sheet using Google Apps Script? 如何将Google工作表的最后(n)行复制到新工作表 - How to copy the last (n)rows of a google sheet to a new sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM