0
votes

Sorry if this is a stupid question but how do I specify the next row via Google App Script, that a Google Form will populate with data, in its associated Google Spreadsheet.

The background is that I was playing with the basic remove duplicate rows tutorial https://developers.google.com/apps-script/articles/removing_duplicates and i noticed that when I 'removed duplicate rows' on a spread sheet associated with a form. And then I submitted some form data. The new row appeared as though no rows had been removed . . . there could be rows with nothing in them directly above the new row.

Thanks guys

1

1 Answers

0
votes

The tutorial uses the clearContents() method to clear the spreadsheet. This method only clears the data in the cells. You should delete the row (using Sheet.deleteRow()) which is a duplicate so that the next form submission goes into the intended row.

You could use something like

function removeDuplicates() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var newData = new Array();
  for(i in data){
    var row = data[i];
    var duplicate = false;
    for(j in newData){
      if(row.join() == newData[j].join()){
        duplicate = true;
      }
    }
    if(!duplicate){
      newData.push(row);
    }
  }
  sheet.deleteRows(1,data.length);
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}