【发布时间】: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