最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete row in Google Sheets if certain "word" is found in cell - Stack Overflow

programmeradmin2浏览0评论

I have a Google Sheet with over 3000 rows. Some of the rows contain words that are not relevant..So I need a way to delete these in bulk. For example, cells will contain something like:

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

I want to delete all of the rows that contain the word "old".

I found a script that's doing half the job, but it requires the "word" to match the entire cell, not just some of the cell.

Line 17 in the code below is what needs to be adjusted:


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

Here's the code:

/**
 * Deletes rows in the active spreadsheet that contain 'word' in column B
 * For more information on using the Spreadsheet API, see
 * 
 */

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
 * 
 */
function onOpen() {
 var sheet = SpreadsheetApp.getActiveSpreadsheet();
 var entries = [{
 name : "Remove rows where column B is 'old'",
 functionName : "readRows"
 }];
 sheet.addMenu("Remove Rows", entries);
};




It adds a menu at the top right..Looks like this,

I have a Google Sheet with over 3000 rows. Some of the rows contain words that are not relevant..So I need a way to delete these in bulk. For example, cells will contain something like:

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

I want to delete all of the rows that contain the word "old".

I found a script that's doing half the job, but it requires the "word" to match the entire cell, not just some of the cell.

Line 17 in the code below is what needs to be adjusted:


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

Here's the code:

/**
 * Deletes rows in the active spreadsheet that contain 'word' in column B
 * For more information on using the Spreadsheet API, see
 * https://developers.google./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./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);
};




It adds a menu at the top right..Looks like this,

Share Improve this question edited Mar 20, 2016 at 17:28 user4039065 asked Mar 20, 2016 at 16:02 OneezyOneezy 5,0237 gold badges48 silver badges75 bronze badges 5
  • stackoverflow./a/1789952/5297207 – Talya S Commented Mar 20, 2016 at 16:05
  • I'm not exactly sure on how to apply that – Oneezy Commented Mar 20, 2016 at 16:45
  • I need to only look for substrings, not the string then the substring. I'm playing with it – Oneezy Commented Mar 20, 2016 at 16:47
  • There is no functionality within Excel that is going to help you with this. – user4039065 Commented Mar 20, 2016 at 17:28
  • I know.. I'm using Google Sheets. And and a custom script (that I'm trying to make work) – Oneezy Commented Mar 20, 2016 at 17:30
Add a ment  | 

2 Answers 2

Reset to default 7

Using the indexOf trick, I've managed to get the desired effect by changing...

This:

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

To This:

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


What's happening here:

The 'indexOf' goes in and finds the position of the first occurrence of the word "old", then returns back a number length value. If it doesn't find the word, the result will be -1. So, as long as you specify greater than "> -1", it will be true..and you'll be good!


Here's the plete code if anyone else needs this in the future,

/**
 * Deletes rows in the active spreadsheet that contain 'word' in column B
 * For more information on using the Spreadsheet API, see
 * https://developers.google./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./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);
};
   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();
};
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>