应用脚本如何根据条件删除并保留一些行

时间:2019-02-25 18:55:23

标签: javascript google-apps-script google-sheets google-sheets-formula google-sheets-macros

我将使用Google表格而不是Excel。所以我遇到了一些编码方面的问题。我以前曾经问过这个问题。

比方说,我连续有两天的数据(早,晚),有25列和超过5万行 我想保留一些符合条件的数据,并删除其他不需要的数据。

示例。我想从早期删除“ AAA”,“ BBB”,“ DDD”,“ FFF”,并保留其他“ CCC”,“ EEE”,“ GGG”。 而对于以后的日子,我想保留“ AAA”,“ BBB”,“ DDD”,“ FFF”并删除其他人

enter image description here

结果将是这样 enter image description here

这是我尝试的代码,但没有得到结果。

//text data in D, dates to evaluate earlyDay/lateDay in C

var Offset = 1;

function deleteRows() {
 var app = SpreadsheetApp.getActiveSpreadsheet();
 var targetSheet = app.getSheetByName('Sheet1');
 var r = targetSheet.getRange('C:C');
 var v = r.getValues();
 var r1 = targetSheet.getRange('D:D');
 var f = r1.getValues(); 
 var lateDay = new Date(getLateDay()).getTime();
 var earlyDay = new Date(getEarlyDay()).getTime();
  
 for(var i = f.length-1; i>=Offset; i--){
  var tmp = new Date(v[0,i]).getTime();
   if(tmp==earlyDay && (f[0,i]=="AAA" || f[0, i]=="BBB")){
    targetSheet.deleteRow(i+1);
   }
 }
  for(var i = f.length-1; i>=Offset; i--){
  var tmp = new Date(v[0,i]).getTime();
   if(tmp==lateDay && (f[0,i]!="AAA" && f[0, i]!="BBB")){
    targetSheet.deleteRow(i+1);
   }
 }
}

function getLateDay() {
 var app = SpreadsheetApp.getActiveSpreadsheet();
 var targetSheet = app.getSheetByName('Sheet1');
 var r = targetSheet.getRange('C:C');
 var v = r.getValues();
  for(var i = v.length-1; i>=Offset; i--){
  var tmp = new Date(v[0,i]).getTime();
  var tmp1 = new Date(v[0,i-1]).getTime();
   if(tmp>tmp1){
    return tmp;
   }
  }
  return null;
}

function getEarlyDay() {
 var app = SpreadsheetApp.getActiveSpreadsheet();
 var targetSheet = app.getSheetByName('Sheet1');
 var r = targetSheet.getRange('C:C');
 var v = r.getValues();
  for(var i = v.length-1; i>=Offset; i--){
  var tmp = new Date(v[0,i]).getTime();
  var tmp1 = new Date(v[0,i-1]).getTime();
   if(tmp<tmp1){
    return tmp;
   }
  }
  return null;
}

1 个答案:

答案 0 :(得分:0)

OP代码具有一些基本的语法和逻辑缺陷。使用Logger命令进行故障排除可能有助于OP识别问题。

1)错误的语法用于访问数组的值。例如,getLateDay使用v[0,i];这里有两个问题。

  • 数组的两个元素都必须放在方括号([0][i])中,
  • 它正在查询错误的元素。代替[0] [i],它应该是[i] [0]。

2)getLateDaygetEarlyDay正在查询C列中的值。相反,它们应该查询D列(日期列)。

3)getLateDaygetEarlyDay都应评估if(tmp>tmp1)。但是getLateDay应该返回“ tmp”,而getEarlyDay应该返回“ tmp1”。

4)getLateDaygetEarlyDay重复了许多主要代码,并且在很大程度上是相同的。为了提高性能,可以轻松将它们合并到主代码中。

5)仅需要一个循环就可以评估EarlyDay / LateDay和“ tmp”,尽管当然需要分别评估适用于EarlyDay和LateDay的alpha代码条件。我还发现,保留在LateDay中的值最好在一系列IF语句中进行评估。整个第二循环都可以删除。

6)如果不是为C列和D列使用单独的范围,而是为合并的列声明单个范围和值,则可能会减少混乱,并节省处理时间。随着OP任务的增加以及他们正在使用25列色谱柱,这将变得更加重要。

我已经修改了OP的代码以解决这些评论。我在代码中留下了几个Logger语句,以便OP可以评估代码不同状态下的变量。

//text data in D, dates to evaluate earlyDay/lateDay in C
var Offset = 1;

function deleteRows() {

  // set up the spreadsheet
  var app = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = app.getSheetByName('Sheet1');

  // define the data ranges and get values
  var r = targetSheet.getRange('C:C');
  var v = r.getValues();
  var r1 = targetSheet.getRange('D:D');
  var f = r1.getValues();
  //Logger.log("DEBUG: code: "+v[0][0]+", date: "+f[0][0].getTime());//DEBUG
  //Logger.log("DEBUG: length of f: "+f.length);//DEBUG

  // get the respective values for lateDay and earlyDay
  var lateDay = new Date(getLateDay()).getTime();
  var earlyDay = new Date(getEarlyDay()).getTime();
  //Logger.log("DEBUG: LateDay: "+lateDay+", EarlyDay: "+earlyDay); //DEBUG

  // Loop through the data, starting at te bottom.
  for (var i = f.length - 1; i > -1; i--) {

    // get the date for this row
    var tmp = new Date(f[i][0]).getTime();

    // evaluate if this row is early and the values of possible codes
    if (tmp == earlyDay && (v[i][0] == "AAA" || v[i][0] == "BBB" || v[i][0] == "DDD" || v[i][0] == "FFF")) {
      Logger.log("DEBUG: Option#1 i: " + i + ", row: " + (i + 1) + "- delete row , earlyDay: " + earlyDay + ", tmp: " + tmp + ", code: " + v[i][0]); //DEBUG

      // Note the row deleted is "i+1" because the deleteRow command matchs the actual row number, whereas the loop works on a zero-basis.
      targetSheet.deleteRow(i + 1);
    } else {
      //Logger.log("DEBUG: Option#1 - do nothing");//DEBUG
    }

    // evaluate if this row is late and the values of possible codes
    // Note this is an either or; a day cannot be both early and late
    if (tmp == lateDay) {
      Logger.log("today is Late Day");
      if (v[i][0] != "FFF") {
        if (v[i][0] != "DDD") {
          if (v[i][0] != "BBB") {
            if (v[i][0] != "AAA") {
              Logger.log("DEBUG: code <> AAA or BBB or DDD or FFF"); //DEBUG
              Logger.log("DEBUG: Option#2 i: " + i + ", row: " + (i + 1) + "- delete row , lateDay: " + lateDay + ", tmp: " + tmp + ", code: " + v[i][0]); //DEBUG
              targetSheet.deleteRow(i + 1);
            } else {
              //Logger.log("DEBUG: Option#2 - do nothing");//DEBUG
            }
          }
        }
      }
    }
  }
}

function getLateDay() {

  var app = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = app.getSheetByName('Sheet1');
  var r1 = targetSheet.getRange('D:D');
  var f = r1.getValues();
  for (var i = f.length - 1; i > -1; i--) {
    var tmp = new Date(f[i][0]).getTime();
    var tmp1 = new Date(f[i - 1][0]).getTime();
    //Logger.log("DEBUG: LateDay: i: "+i+", this day: "+tmp+", yesterday: "+tmp1);//DEBUG
    if (tmp > tmp1) {
      //Logger.log("DEBUG: return this day: "+tmp);//DEBUG
      return tmp;
    }
  }
  //Logger.log("DEBUG: return null");
  return null;
}

function getEarlyDay() {

  var app = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = app.getSheetByName('Sheet1');
  var r1 = targetSheet.getRange('D:D');
  var f = r1.getValues();
  for (var i = f.length - 1; i > -1; i--) {
    var tmp = new Date(f[i][0]).getTime();
    var tmp1 = new Date(f[i - 1][0]).getTime();
    //Logger.log("DEBUG: EarlyDay: i: "+i+", tmp: "+tmp+", tmp1: "+tmp1);//DEBUG
    if (tmp > tmp1) {
      //Logger.log("DEBUG: return this day: "+tmp1);//DEBUG
      return tmp1;
    }
  }
  //Logger.log("DEBUG: return null");//DEBUG
  return null;
}

屏幕截图前后

Before After