根据一系列字符串条件删除行

时间:2014-11-13 00:20:49

标签: javascript google-apps-script

我有数十个长的数十个电子表格,我想通过删除不满足条件的行来对它们进行子集化。

让我提出一个简化的例子。 Say Row C具有大学部门名称的字符串值(例如“ANTHRO”是人类学,“ART-HIST”是艺术史,等等)。该大学有很多部门,电子表格中每个部门都有很多条目,但我只想要人类学和艺术史的数据。因此,我的任务是编写一个脚本,删除不满足条件RowC =“ANTHRO”或“ART-HIST”的每一行。

问题是,我不知道如何在javascript / google-apps-script中定义一个带有一系列(字符串)值的变量。有一次尝试看到我将“剔除”变量定义为一个数组,其中包含脚本将判断数据的条件:

 var rowsDeleted = 0;
  var keep = ["ANTHRO", "ART-HIST"];
  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[2] != keep) {
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }
  }
}; 

然而它没有用。我知道我可以简单地写if(row[2] != "ANTHRO" || != "ART"),但实际上有两个以上的条件。定义所谓的“剔除”变量似乎更有效。

关于为什么阵列方法不起作用的任何见解?谢谢。

3 个答案:

答案 0 :(得分:2)

您可以尝试使用indexOf。如果行值不在数组内,它将返回值-1,否则将返回索引。

  var rowsDeleted = 0;
  var keep = ["ANTHRO",
              "ART-HIST"];
  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (keep.indexOf(row[2]) === -1) {
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }
  }
}; 

答案 1 :(得分:0)

对我来说,你真正要做的是根据特定情况过滤一组数据。首先,您可以将数千行数据存储到数组中,并且数组的每个元素都可以是表示许多列的行的对象。然后,您可以通过条件过滤数组,即行是否包含字符串“ANTHRO”或“ART-HIST”。可能的代码实现是:

var data = [
    { DEPT_ID: 1, ABBR_NAME: "ANTHRO", FULL_NAME: "Anthropology", StudentsNO: 240 },
    { DEPT_ID: 2, ABBR_NAME: "ART-HIST", NAME: "Art History", StudentsNO: 200 },
    { DEPT_ID: 3, ABBR_NAME: "MATH", FULL_NAME: "Mathematics", StudentsNO: 50 },
    { DEPT_ID: 4, ABBR_NAME: "CS", NAME: "Computer Science", StudentsNO: 79 }
];

function isAnthroOrArtHist(element) {
    return ["ANTHRO", "ART-HIST"].indexOf(element.ABBR_NAME) >= 0;
}
var newData = data.filter(isAnthroOrArtHist);

答案 2 :(得分:0)

我做同样的事情,我将Liquor Inventory列表连同另一个选项卡以及VENDOR名称,如果没有列出VENDOR名称,那么它隐藏了行

这是我使用的。您可以将它调整到您需要的位置。

function liquorOrderGuideWorking() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('LIQUOR ORDER'); 
 // Rows start at "1" - this will delete the first two rows
 //sheet.deleteRows(2, 567);

  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var liquorInventory = ss.getSheetByName('LIQUOR INVENTORY'); 

  var liquorInventoryRange = liquorInventory.getRange('B6:C573');  //Holds Vendor & Item Name
  var liquorInventoryTotalRange = liquorInventory.getRange('I6:I573'); //Holds QTY of each item

  var liquorOrder = ss.getSheetByName('LIQUOR ORDER'); //Gets the new sheet
  var liquorOrderRange = liquorOrder.getRange('A2:B569');  //Places Vendor & Item Name
  var liquorOrderQTYRange = liquorOrder.getRange('C2:C569'); //Places QTY

  liquorInventoryRange.copyTo(liquorOrderRange, {contentsOnly:true})
  liquorInventoryTotalRange.copyTo(liquorOrderQTYRange, {contentsOnly:true})

  var s = ss.getSheetByName('LIQUOR ORDER');
  var lastCol = s.getLastColumn();
  var lastRow = s.getLastRow();

  // assumes headers in row 1
  var r = s.getRange(2, 1, lastRow - 1, lastCol);

  // Note the use of an array
  r.sort([{ column: 1, ascending: true }, { column: 2, ascending: true}]);

  var ssHide = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ssHide.getSheetByName('LIQUOR ORDER');
  var maxRows = sheet.getMaxRows();

  //show all the rows
  sheet.showRows(1, maxRows);

  //get data from column B
  var data = sheet.getRange('A:A').getValues();

  //iterate over all rows
  for(var i=0; i< data.length; i++){
    //compare first character, if blank, then hide row
    if(data[i][0].charAt(0) == ''){
      sheet.hideRows(i+1);
    }
  }
Browser.msgBox('Liquor Order Guide','The Liquor Order Guide has been refreshed successfully.', Browser.Buttons.OK);
}