I have been trying to create a script that will prevent duplicate entries from being submitted to a Google Sheet from a Google Form. However, I am encountering an error that says "No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it." I have checked the sharing settings and confirmed that I am the owner of the form and the sheet. I have also tried creating a copy of the form and using the new ID, but the problem persists. I have also tried modifying the script by changing the ID of the form and sheet, but I keep getting syntax errors. I am not sure what is causing the problem, and I am looking for a solution. This is the code I have been using.
function onFormSubmit(event) {
var form = FormApp.openById("1FAIpQLScvWCoM3uospk3rd1JVfcecA3mU32oNtReow71j_2lL9ZjaMg");
var formResponses = form.getResponses([formResponses.length-1]);
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
var name = itemResponses[0].getResponse();
var spreadsheet = SpreadsheetApp.openById("1JtVDBs4vXP3sirpc5eN6FsLBierR4ytZh9q-LLIWTJg").getSheetByName("Sheet1");
var data = spreadsheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
if (data[i][0] == name) {
SpreadsheetApp.getUi().alert("already registered");
return;
}
}
}
I have checked the sharing settings and confirmed that I am the owner of the form and the sheet. I have also tried creating a copy of the form and using the new ID, but the problem persists. I have also tried modifying the script by changing the ID of the form and sheet, but I keep getting syntax errors. I am looking to have the form show the message already registered when a name already exists on the sheet.