根据两个条件删除一行

时间:2019-09-21 16:48:14

标签: google-apps-script google-sheets timestamp

所以我在工作表中有一排数据。如果列H =“ Complete”并且列A的时间戳超过48小时,则需要删除该行。我的问题是添加经过时间条件。如果列H =“ Complete”,则脚本在删除行时起作用。

var SS = SpreadsheetApp.openById("SheetID");
var SHEET = SS.getSheetByName("Form Responses");
var RANGE = SHEET.getDataRange();
var day = 24*3600*1000
var today = parseInt((new Date().setHours(0,0,0,0))/day);


var DELETE_VAL = "Complete";
var COL_TO_SEARCH = 7; // The column to search for the DELETE_VAL (Zero is first)
var COL_TO_SEARCH_2 = 0; // The column to search for the date (Zero is first)


function deleteEachRow(){

  var rangeVals = RANGE.getValues();

  //Reverse the 'for' loop.
  for(var i = rangeVals.length-1; i >= 0; i--){
    if(rangeVals[i][COL_TO_SEARCH] === DELETE_VAL && rangeVals[i][COL_TO_SEARCH_2] < today){

      SHEET.deleteRow(i+1); 
    };
  };
};

1 个答案:

答案 0 :(得分:1)

function deleteIfOlderThan2DaysAgo() {
  var ss=SpreadsheetApp.openById("SheetID");
  var sh=ss.getSheetByName("Form Responses");
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var dt=new Date();
  var twoDaysAgo=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-2).valueOf();
  var d=0;
  for(var i=0;i<vA.length;i++){
    var col1=new Date(vA[i][0]).valueOf();
    if(vA[i][7]=="Complete" && col1<twoDaysAgo) {
      sh.deleteRow(i+1-d++); 
    }
  }
}