Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在google工作表中循环删除行-比较日期_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 在google工作表中循环删除行-比较日期

Google apps script 在google工作表中循环删除行-比较日期,google-apps-script,google-sheets,Google Apps Script,Google Sheets,代码需要删除日期早于当前日期的行。我没有得到任何语法错误,但我认为这是一个语义问题,因为代码不会以任何方式更改电子表格 function checkDateValue(e) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; var todaysDate = new Date().getDate(); var columnGCells = sheet.getRange("G2:G

代码需要删除日期早于当前日期的行。我没有得到任何语法错误,但我认为这是一个语义问题,因为代码不会以任何方式更改电子表格

function checkDateValue(e) {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
    var todaysDate = new Date().getDate();
    var columnGCells = sheet.getRange("G2:G");
    var columnG = columnGCells.getValues();

    for (i = 1; i < sheet.height; i++) {
        var endDate = new Date()
        endDate = columnG[i];
        if (todaysDate.getYear() >= endDate.getYear()){
          if (todaysDate.getMonth() >= endDate.getMonth()){
            if (todaysDate.getDay >= endDate.getDay()){
              sheet.deleteRow(i);
            }
          }
        }
      }
};
checkDateValue();
函数checkDateValue(e){
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var todaysDate=new Date().getDate();
var columnGCells=sheet.getRange(“G2:G”);
var columngg=columnGCells.getValues();
对于(i=1;i=endDate.getYear()){
如果(todaysDate.getMonth()>=endDate.getMonth()){
如果(todaysDate.getDay>=endDate.getDay()){
第1页删除第(i)行;
}
}
}
}
};
checkDateValue();

代码可能如下所示:

function checkDateValue(e) {
  var colG_values,columnGCells,columnNumber,endDate,i,lastRow,
      sheet,startRow,todaysDate;
  
  startRow = 2;//Get data starting in this row
  columnNumber = 7://The column number to get the data from

  sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  lastRow = sheet.getLastRow();
  
  todaysDate = new Date().getDate();
  //Logger.log('todaysDate: ' + todaysDate);

  columnGCells = sheet.getRange(startRow,columnNumber,lastRow-1,1);
  colG_values = columnGCells.getValues();

  for (i = lastRow; i > 1; i--) {//Loop through values from last to first-
       //Need to stop at row one-
    endDate = colG_values[i];
    endDate = new Date(endDate);
    
    if (todaysDate.getYear() >= endDate.getYear()){
      if (todaysDate.getMonth() >= endDate.getMonth()){
         if (todaysDate.getDay >= endDate.getDay()){
           sheet.deleteRow(i);//Delete this row
        }
      }
    }
  };
};

function runTest() {
  checkDateValue();
};

图纸没有
height
属性。使用
getLastRow()
方法。但是,即使你改变了,你仍然会有问题。要删除行,需要自下而上进行迭代。您需要递减
i--
而不是递增计数器
i++