One*_*ezy 1 javascript google-sheets google-spreadsheet-api
我有一个超过3000行的Google表格。有些行包含不相关的单词。因此,我需要一种批量删除这些单词的方法。例如,单元格将包含以下内容:
# | Product
-------------------------------
1 | Cool new product
2 | Old product
3 | Product that's old
Run Code Online (Sandbox Code Playgroud)
我想删除所有包含单词“ old”的行。
我发现一个脚本完成了一半的工作,但是它需要“单词”来匹配整个单元,而不仅仅是某些单元。
以下代码中的第17行是需要调整的内容:
16 |
17 | if (row[1] == 'old')
18 |
Run Code Online (Sandbox Code Playgroud)
这是代码:
/**
* 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);
};
Run Code Online (Sandbox Code Playgroud)
它会在右上角添加一个菜单。看起来像这样,
使用indexOf技巧,通过更改...,我设法获得了预期的效果。
这个:
if (row[1] == 'old')
Run Code Online (Sandbox Code Playgroud)
为此:
if (row[1].indexOf("old") > -1)
Run Code Online (Sandbox Code Playgroud)
这里发生了什么事:
'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);
};
Run Code Online (Sandbox Code Playgroud)