J Doe
J Doe

Reputation: 1

Script does not trigger on FormSubmit: Remove Duplicates in Google Sheet of Google Form responses based on column

I am trying to remove older duplicate form responses based on a column using the following code.

The credit for the code goes to: http://www.jacorre.com/tutorial/remove-duplicate-rows-google-spreadsheets/

The code in my script is:

function removeDuplicates() {
 var ss = SpreadsheetApp.getActiveSpreadsheet(),
     responses = ss.getSheetByName('Name of Source Sheet'),
     range = responses.getDataRange(),
     numRows = range.getNumRows()-1,
     data = range.getValues(),
     columnHeadings = [data[0]],
     newData = [];
 for (var i=numRows; i>0; i--) {
  var row = data[i],
      duplicate = false;
  for (var j in newData) {
   if (row[4] == newData[j][4]) {
    duplicate = true;
// [4] is the column number from the 1st column. the above would be 1 + 4 = 5th column 
  }
  }
  if (!duplicate) {
   newData.push(row);
  }
 }
 var final = ss.getSheetByName('Name of Destination Sheet');
 if (!final) {
  var final = ss.insertSheet('Name of Destination Sheet');
 } 
 final.clearContents();
 final.getRange(1,1,1,columnHeadings[0].length).setFontWeight('bold').setValues(columnHeadings);
 final.getRange(2, 1, newData.length, newData[0].length).setValues(newData);
}

This has been set to trigger on Form Submit. It works well on new form submissions.

However, when an existing response is edited using 'Form Edit URL' from: https://webapps.stackexchange.com/questions/89551/show-url-used-to-edit-responses-from-a-google-form-in-a-google-spreadsheet-by-us/89566 the values are not updated into the new sheet.

But if the function is run manually the updated row is updated to the new sheet.

How can I sort this problem? Any help will be appreciated. Thank you.

Upvotes: 0

Views: 552

Answers (1)

Wicket
Wicket

Reputation: 38356

From my own answer posted at Web Applications SE.

I just did a test and found that the on form submit event it's not being triggered when a response is edited.

I'm not sure if the on form submit trigger is working as intended, if the above is due to a bug or to a glitch. To be sure, post an issue to the Google Apps Script Issue Tracker.

As a workaround, instead of using the on form submit event, use another way to run your script, like a time-drive trigger.

References

Upvotes: 0

Related Questions