删除Google表格中的行,如果某些" word"在细胞中发现

时间:2016-03-20 16:02:03

标签: javascript google-sheets google-spreadsheet-api

我有一张超过3000行的Google表格。有些行包含不相关的单词。所以我需要一种方法来批量删除它们。例如,单元格将包含以下内容:

 # | Product
-------------------------------
 1 | Cool new product
 2 | Old product
 3 | Product that's old

我想删除包含单词" old"。

的所有行

我发现一个脚本可以完成一半的工作,但它需要"字#34;匹配整个细胞,而不仅仅是一些细胞。

以下代码中的第17行是需要调整的内容:

16 |
17 |      if (row[1] == 'old')
18 |

以下代码:

/**
 * Deletes rows in the active spreadsheet that contain 'word' in column B
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */

function readRows() {
 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[1] == 'old') {
 sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
 rowsDeleted++;
 }
 }
};


/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
 var sheet = SpreadsheetApp.getActiveSpreadsheet();
 var entries = [{
 name : "Remove rows where column B is 'old'",
 functionName : "readRows"
 }];
 sheet.addMenu("Remove Rows", entries);
};




它在右上角添加了一个菜单..看起来像这样,

Remove Rows

2 个答案:

答案 0 :(得分:4)

使用indexOf技巧,我通过改变设法获得了预期的效果......

此:

    if (row[1] == 'old')

至此:

    if (row[1].indexOf("old") > -1)


这里发生了什么:

  

&#39; indexOf&#39;进去找到的位置   第一次出现单词&#34; old&#34;,然后返回一个数字长度   值。如果找不到该单词,结果将为-1。所以,只要   当你指定大于&#34;&gt; -1&#34;,这将是真的......你会好的!


如果其他人在将来需要此代码,请填写完整的代码,

/**
 * Deletes rows in the active spreadsheet that contain 'word' in column B
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */

function readRows() {
 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[1].indexOf("old") > -1) {
 sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
 rowsDeleted++;
 }

 }
};


/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
 var sheet = SpreadsheetApp.getActiveSpreadsheet();
 var entries = [{
 name : "Remove rows where column B is 'old'",
 functionName : "readRows"
 }];
 sheet.addMenu("Remove Rows", entries);
};

答案 1 :(得分:0)

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

  var toDelete = [];

var re = new RegExp('old','gi'); 
  for (var row = 0; row < values.length; row++) { 
  for(var column = 0;column<values[row].length;column++){ 
  if (re.exec(values[row][column])){
  toDelete.push(row); } } }


  for(var deleteRow = toDelete.length-1; deleteRow >= 0;deleteRow--){
    sheet.deleteRow(toDelete[deleteRow]+1);
  }

  SpreadsheetApp.flush();
};