Colin Wood
Colin Wood

Reputation: 47

Delete Google Form submission based on date

I'm trying to remove responses from Google forms that are older than a set age. 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);
    }
  }
}

Upvotes: 0

Views: 1363

Answers (2)

fullfine
fullfine

Reputation: 1461

Answer

I've created a Form and I managed to delete the submits older than a week. There are a few methods that you have to use to achieve that goal:

Steps

  • Open your Form. You can use getActiveForm if you are using an Apps Script project contained in a Form document. Otherwise, use openById or openByUrl
  • Get all the responses with getResponses
  • Get the timestamp of each with getTimestamp
  • Compare the two timestamps with a simple if statement
  • 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:

Upvotes: 1

Deven Ramani
Deven Ramani

Reputation: 781

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

Upvotes: 0

Related Questions