javascript - 如果在单元格中找到特定的 "word",则删除 Google 表格中的行

标签 javascript google-sheets google-sheets-api

我有一个包含 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);
};




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

Remove Rows

最佳答案

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

这个:

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

对此:

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


这里发生了什么:

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!


如果以后有人需要的话,这里是完整的代码,

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

关于javascript - 如果在单元格中找到特定的 "word",则删除 Google 表格中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116050/

相关文章:

google-apps-script - UrlFetchApp 超时

javascript - 如何从 google 表格中提取随机行并在 HTML 表中使用它

javascript - 如何更改jquery对话框按钮

javascript - 将 Form 的旧值与新值进行比较

google-apps-script - 是否可以在 Google-docs 电子表格中定义新函数?

google-apps-script - 谷歌表格: Batch getRangeByName via Apps Script

Javascript日期转换为时区格式

javascript - HTML 谷歌地图定制

android - 什么是 MIME 类型的 Google Drive 电子表格?

javascript - 为谷歌表格创建自定义函数