【问题标题】:Delete row in Google Sheets if certain "word" is found in cell如果在单元格中找到某些“单词”,则删除 Google 表格中的行
【发布时间】:2016-03-20 16:02:03
【问题描述】:

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

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

我想删除所有包含单词“old”的行。

我发现一个脚本完成了一半的工作,但它需要“单词”来匹配整个单元格,而不仅仅是部分单元格。

下面代码中的第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);
};




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

【问题讨论】:

  • 我不确定如何应用它
  • 我只需要查找子字符串,而不是字符串然后是子字符串。我在玩它
  • Excel 中没有任何功能可以帮助您解决此问题。
  • 我知道.. 我正在使用 Google 表格。还有一个自定义脚本(我正在尝试制作)

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


【解决方案1】:

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

这个:

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

致此:

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


这里发生了什么:

'indexOf'进入并找到 单词“old”的第一次出现,然后返回一个数字长度 价值。如果它没有找到这个词,结果将是-1。所以,只要 当您指定大于“> -1”时,这将是真的..您会很好!


如果其他人将来需要,这里是完整的代码,

/**
 * 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);
};

【讨论】:

  • 我添加了一个简单的脚本,根据正则表达式删除行
  • 这还能用吗?我收到 indexOf 的 Typescript 错误。我认为是它在对象中找不到字符串,但我选择了应该能够选择内容的单元格/范围。
【解决方案2】:
   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();
};

【讨论】:

  • 这是一个作业,不是匹配测试,而且我很确定 JS 无论如何都没有用于正则表达式匹配的语法糖,所以需要.test() 或类似的东西。
猜你喜欢
  • 2022-07-05
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多