You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google表格批量删除指定词汇:修改单词汇删除代码支持多词汇

Solution for Bulk Deleting Rows with Specific Words in Google Sheets

Got it, let's tweak your script so it can handle as many target words/phrases as you need. Here's the modified code, plus a breakdown of what changed:

function deleteRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Log');
  var r = s.getRange('A:Z');
  var v = r.getValues();
  
  // Add all your target words/phrases here—one per string, separated by commas
  var wordsToDelete = ['Tom', 'Jerry', 'Q3 Report', 'Invalid Entry'];
  
  // Loop from bottom to top to avoid missing rows when deleting
  for(var i = v.length - 1; i >= 0; i--) {
    // Check if column A's value is in our target list
    if(wordsToDelete.includes(v[i][0])) {
      s.deleteRow(i + 1); // +1 because sheet rows start at 1, array indexes start at 0
    }
  }
}

Key Changes Explained:

  • Bulk Target Array: The wordsToDelete array is where you'll list every term you want to delete rows for. Just add new strings inside the brackets (make sure they're wrapped in quotes and separated by commas) — no limit to how many you can add.
  • Fixed Column Reference: I corrected v[0,i] to v[i][0] because getValues() returns a row-first 2D array. So v[i][0] refers to the value in column A of the i-th row in your data range, which is what you want to check.
  • Safe Deletion Loop: We still loop from the last row up to the first. This is important because deleting a row shifts all rows below it up by one—if you looped downwards, you'd skip over rows that moved into the index you already checked.

Quick Tip:

If you need to check a different column instead of A, just change the 0 in v[i][0] to the column index you want (e.g., 1 for column B, 2 for column C, etc.).

内容的提问来源于stack exchange,提问作者DUBYA RX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:29:16