3

So, I've been trying to figure out how to stop the duplicate rows appearing in my google sheets response output from a google form. If found this link which sounds like it does exactly what I want (Form Google Script Prevent Duplicates), but cannot for the life of me work out how to edit the given answer to work on my sheet.

I have included a screenshot of my workbook to give an example of the structure of the data I'd like the edited code to run on, and also below is my attempt at making the code run correctly on my data structure.

My sheet structure that I'd like to run the code on. I want to use the email address as the 'unique' identifier, so any duplicate rows can be identified using that.

My attempt at adapting the code to work on the above data structure (I have absolutely no background with this scripting language, so please go easy on me if I've made a glaringly obvious error):

function updateExisting() {
  var s = SpreadsheetApp.getActiveSheet(),
//      s = ss.getSheetByName(''),
      lastRow = s.getLastRow(),
      lastValues = s.getRange('A'+lastRow+':C'+lastRow).getValues(),
      name = lastValues[0][0],
      allNames = s.getRange('B2:B').getValues(), 
      row, len;

  // TRY AND FIND EXISTING NAME
  for (row = 0, len = allNames.length; row < len - 1; row++)
    if (allNames[row][0] == name) {
      // OVERWRITE OLD DATA
      s.getRange('A2').offset(0, 0, row, 
lastValues.length).setValues([lastValues]);
      // DELETE THE LAST ROW
      s.deleteRow(lastRow);
      break;}
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
Smashing
  • 39
  • 2
  • 7
  • What is triggering this code to run? Is the function name associated with the "On Form Submit" trigger? Do you know if the code is running at all? – Alan Wells Jun 25 '18 at 14:26
  • I have just been trying to get the code to run successfully using Run/imported as macro up to now. It will be set to run on every submit trigger like you mention - I'm going to try and have a go with your code now, thank you for taking the time to help me out :) – Smashing Jun 25 '18 at 17:02

1 Answers1

3

Key words: duplicates, Google, spreadsheet, Sheets, Form, submission, edit, row, unique.

This code prevents duplicates in a Google Sheet when submitting a Google Form, by overwriting an existing row with the existing unique value, if one exists. The code searches one column in a spreadsheet and looks for a match. I tried to make it generic so that the code doesn't need to be changed depending upon what column the unique identifier is in. You need to make a couple of settings in the "User Settings" section to make it work. But that is better than needing to rewrite the code.

function updateExisting(columnWithUniqueIdentifier,sheetTabName) {
  var dataFromColumnToMatch,lastColumn,lastRow,rowWithExistingUniqueValue,rowOfDataJustSaved,
      sh,ss,valueToSearchFor;

  // USER SETTINGS - if the values where not passed in to the function
  if (!columnWithUniqueIdentifier) {//If you are not passing in the column number
    columnWithUniqueIdentifier = 2;//Hard code column number if you want
  }

  if (!sheetTabName) {//The sheet tab name was not passed in to the function
    sheetTabName = "Put your Sheet tab name here";//Hard code if needed
  }
  //end of user settings

  ss = SpreadsheetApp.getActiveSpreadsheet();//Get the active spreadsheet - this code must be in a project bound to spreadsheet
  sh = ss.getSheetByName(sheetTabName);

  lastRow = sh.getLastRow();
  lastColumn = sh.getLastColumn();

  //Logger.log('lastRow: ' + lastRow)

  rowOfDataJustSaved = sh.getRange(lastRow, 1, 1, lastColumn).getValues();//Get the values that were just saved

  valueToSearchFor = rowOfDataJustSaved[0][columnWithUniqueIdentifier-1];
  //Logger.log('valueToSearchFor: ' + valueToSearchFor)

  dataFromColumnToMatch = sh.getRange(1, columnWithUniqueIdentifier, lastRow-1, 1).getValues();
  dataFromColumnToMatch = dataFromColumnToMatch.toString().split(",");
  //Logger.log('dataFromColumnToMatch: ' + dataFromColumnToMatch)

  rowWithExistingUniqueValue = dataFromColumnToMatch.indexOf(valueToSearchFor);
  //Logger.log('rowWithExistingUniqueValue: ' + rowWithExistingUniqueValue)

  if (rowWithExistingUniqueValue === -1) {//There is no existing data with the unique identifier
    return;
  }

  sh.getRange(rowWithExistingUniqueValue + 1, 1, 1, rowOfDataJustSaved[0].length).setValues(rowOfDataJustSaved);
  sh.deleteRow(lastRow);//delete the row that was at then end
}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • So, I imported the code you wrote ('updateExisting') as a macro into that 3 row test sheet. Whenever it runs, the top (unique) row is deleted and the 2 identical rows are kept - I think I must be doing something wrong. I have hardcoded in the correct sheet name, and set the unique column to 2 (email). – Smashing Jun 25 '18 at 17:13
  • There was an error in the code. The code you are using is probably overwriting the wrong row. In the next to last line, add 1 to the "rowWithExistingUniqueValue " variable: `sh.getRange(rowWithExistingUniqueValue + 1,` Also, uncomment the `Logger.log()` statements, run the code, and then in the View menu choose Logs. – Alan Wells Jun 25 '18 at 17:20
  • Amazing, your edit worked perfectly, thank you so much! – Smashing Jun 25 '18 at 23:38