Google表格:如果某些单元格为空白,如何删除行?

时间:2014-11-01 02:01:48

标签: google-apps-script google-sheets

我目前有一张13,000行的表格。我必须创建一个脚本,只有在C&列中的单元格中才会删除整行。 D为0或空白。我目前的脚本仅适用于C列中的单元格。如何在D列中添加。这是我的脚本:感谢您的见解!

/*** Deletes rows in the active spreadsheet that contain 0 or
* a blank value in column "C". 
* 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[2] == 0 || row[2] == '') {
  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 C and D is 0 or blank",
  functionName : "readRows"
  }];
 sheet.addMenu("Script Center Menu", entries);
};

1 个答案:

答案 0 :(得分:2)

更改'if',因此它也会在元素3中查找空白和零:

if (row[2] == 0 || row[2] == '' || row[3] == 0 || row[3] == '') {

Here's the Sheet我曾经测试它,包括几个“普通”行,然后是4个带有空白和零的测试行。对脚本的更改将删除所有测试行。

除非您希望它删除C和D中的单元格缺少其值的行?在这种情况下,你会使用&amp;&amp;运算符,并包装两个||括号中的表达式:

if ((row[2] == 0 || row[2] == '') && (row[3] == 0 || row[3] == '')) {
带有额外零+空白组合的

Here's a revised sheet:)

相关问题