我需要一个脚本,在一段时间后删除行

时间:2015-05-22 18:12:54

标签: google-apps-script google-sheets

是否可以制作一个脚本来删除超过3周的电子表格中的响应?

这是我尝试过的不起作用的脚本:

function duration() {
var sheet = SpreadsheetApp.getActiveSheet(); 
var data = sheet.getDataRange().getValues();
var newData = new Array();
var now = new Date();
newData.push([("Timestamp") , ("First Name") , ("Last Name") , ("Homeroom")]);
for(i in data){
 var row = data[i]; if (now - row[0] < 300000) { newData.push(row); 
 } 
} 

sheet.clearContents(); 
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData); 

1 个答案:

答案 0 :(得分:0)

这很有效。我测试了它:

function duration() {
  var sheet = SpreadsheetApp.getActiveSheet(); 
  var data = sheet.getDataRange().getValues();
  var newData = new Array();
  var now = new Date();

  var threeWeeksAgo = new Date();
  threeWeeksAgo.setDate(now.getDate() - 21);

  var theLastRow = sheet.getLastRow();  
  newData.push(["Timestamp","First Name","Last Name","Homeroom"]);

  for(var i=0;i<theLastRow;i++){
    var row = data[i];
    if (row[0] > threeWeeksAgo) {
      newData.push(row); 
    }; 
  }; 

  sheet.clearContents(); 
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
};