简体   繁体   English

根据日期删除 Google 表单提交

[英]Delete Google Form submission based on date

I'm trying to remove responses from Google forms that are older than a set age.我正在尝试从 Google forms 中删除超过设定年龄的响应。 I have a script to do this for the sheet, but this does not remove the responses from the form.我有一个脚本可以为工作表执行此操作,但这不会从表单中删除响应。 Removing all responses from the form is not an option as I need to retain the responses not older than the set age.从表单中删除所有回复不是一种选择,因为我需要保留不超过设定年龄的回复。 This is the script which works on the sheet, which I need an equivalent for on the form.这是在工作表上工作的脚本,我需要在表单上使用它。

script working on sheet在工作表上工作的脚本

function DeleteOldEntries() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //give your sheet name below instead of Sheet1
  var sheet = ss.getSheetByName("Sheet 1");
  
  var datarange = sheet.getDataRange();
  var lastrow = datarange.getLastRow();


  var currentDate = new Date();
  var oneweekago = new Date();
  oneweekago.setDate(currentDate.getDate() - 1095);
    
  for (i=lastrow;i>=2;i--) {
    var tempdate = sheet.getRange(i, 1).getValue();
    
    if(tempdate < oneweekago)
    {
      sheet.deleteRow(i);
    }
  }
}

You need to write a script to delete the response from the Form same as you write for the spreadsheet.您需要编写一个脚本来删除表单中的响应,就像您为电子表格编写的一样。 Please refer this link for deleting form single response by Id 请参阅此链接以按 ID 删除表单单一响应

Answer回答

I've created a Form and I managed to delete the submits older than a week.我创建了一个Form ,并设法删除了一周前的提交。 There are a few methods that you have to use to achieve that goal:您必须使用几种方法来实现该目标:

Steps脚步

  • Open your Form .打开您的Form You can use getActiveForm if you are using an Apps Script project contained in a Form document.如果您正在使用包含在Form文档中的 Apps 脚本项目,则可以使用getActiveForm Otherwise, use openById or openByUrl否则,使用openByIdopenByUrl
  • Get all the responses with getResponses使用getResponses获取所有响应
  • Get the timestamp of each with getTimestamp使用getTimestamp获取每个timestamp
  • Compare the two timestamps with a simple if statement用一个简单的 if 语句比较两个时间戳
  • When the condition is true当条件为真时

Code代码

function deleteSubmissions(){

  var oneweekago = new Date();
  oneweekago.setDate(oneweekago.getDate() - 1095);

  var form = FormApp.getActiveForm() // FormApp.openById() | FormApp.openByUrl()
  var responses = form.getResponses()

  for (var i=0; i<responses.length; i++){
    var r = responses[i]
    var responseTime = r.getTimestamp()
    if (responseTime > oneweekago){
      var id = r.getId()
      form.deleteResponse(id)
    }
  }
}

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM