删除Google表格中包含任意数量已定义关键字的行

时间:2017-11-15 10:24:25

标签: javascript google-apps-script google-sheets

我想删除Google表格中包含任何关键字的行,这些关键字是某些预定义集合的成员。我目前有以下代码:

function removeKeywords() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var rows = sheet.getDataRange();
 var numRows = rows.getNumRows();
 var values = rows.getValues();

 var rowsDeleted = 0;
 for (var i = 0; i <= numRows - 1; i++) {

 var row = values[i];

 if (row[0].indexOf("admin","conference","department") > -1) {
 sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
 rowsDeleted++;
 }

 }
};

关键字&#39; admin&#39;,&#39; conference&#39;和&#39;部门&#39;触发删除行的功能。但我的代码不起作用。非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

看看是否有效

 function removeKeywords() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var rowsDeleted = 0;
 sheet.getDataRange()
     .getValues()
     .forEach(function (r, i) {
         if (["admin", "conference", "department"].indexOf(r[0]) > -1) {
             sheet.deleteRow((parseInt(i) + 1) - rowsDeleted);
             rowsDeleted++;
         }
     })
}