簡體   English   中英

Google表格:如果某些單元格為空白,如何刪除行?

[英]Google Sheets: How to delete rows if cells in certain are blank?

我目前有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);
};

更改“ if”,以便它還在元素3中查找空白和零:

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

這是我用來測試的工作表 ,它由一些“正常”行組成,然后是4個帶有空白和零的測試行。 對腳本所做的更改將刪除所有測試行。

除非您希望它刪除C和D中的單元格缺少值的行? 在這種情況下,您將使用&&運算符,並包裝兩個||。 括號中的表達式:

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

這是帶有其他零+空白組合的修訂版 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM