The questioner was having a problem with the evaluation of information from a form response and whether a student's name already existed on the target sheet. Regardless of the student details, the response was always consistent with a specific outcome.
The reason for this was that the first thing to be evaluated was whether the student's name was on the target sheet. Note the "break" command.
if (vector2d[i][0] != nombre){
option =1;
break
}
So it only required that the student's name did not match the first name on the target sheet and the IF statement was evaluated as TRUE, and the subsequent "break" ended the entire for
loop. The effect is that the first outcome was not satisfactorily evaluated, and the other two outcomes were never evaluated.
Because the student's name may or may not be on the target, and those outcomes drove further evaluations, I created two loops. One to establish whether the student's name was on the target, and a second, in the event that it was on the target, to enable evaluation of the other criteria.
In addition, as noted in comments, the evaluation phase returned up to two items: 1) Option and 2) Row. This was resolved by creating a Javascript object.
function onFormSubmit(e) {
//getting the response items
//Logger.log("%s", JSON.stringify(e));//DEBUG
var nombre = e.namedValues['Name'][0];
var tiempo = e.namedValues['Marca temporal'][0];
// Logger.log("nombre = "+ nombre+", and time stamp is "+tiempo); //DEBUG
// get timestamp
var timestamp = e.range.getValues()[0];
// Logger.log("timestamp = "+timestamp);//DEBUG
// get the day and month from the timestamp
var hoja = timestamp[0].getMonth();
var dia = timestamp[0].getDate();
// Logger.log("timemonth"+hoja+", and dia = "+dia);// DEBUG
// get the sheet number
var sheetnum = +hoja + 1;
// Logger.log("sheetnum = "+sheetnum);//DEBUG
// set up the target spreadsheet and sheet
var sobid = SpreadsheetApp.openById("1gqbTqb2xOt3rORKcQ_7weGynj9eWypt5gS6ahKVfci8");
var spsheets = sobid.getSheets();
var monthsheet = spsheets[sheetnum];
// Logger.log("Sheet#:"+sheetnum+", name: "+monthsheet.getName());//DEBUG
// get the last row on the target
var last = monthsheet.getLastRow();
// Logger.log("last = "+last);//DEBUG
// get the data from the target
var vector2d = monthsheet.getRange(2, 1, last, 4).getValues();
// Evaluate whether student name is already on Target &/or day and timestamp are entered
var reply = Evaluar(vector2d, nombre, dia, option, last);
//Logger.log("Evaluar result #"+reply["result"]+"; option = "+reply["option"]+"; row = "+reply["row"]+"; i = "+(reply["i"]+2));//DEBUG
//copy the "option" number to the target
var range = monthsheet.getRange(2, 6); // F2
range.setValue(reply["option"]); //copy option to //F2
//use results and switch to identify fields to be updated on target.
switch (reply["option"]) {
case 1:
var range = monthsheet.getRange(last + 1, 1); //Ax
range.setValue(nombre); //paste name
var range = monthsheet.getRange(last + 1, 2); //Bx
range.setValue(dia); //paste day
var range = monthsheet.getRange(last + 1, 3); //Cx
range.setValue(tiempo); //paste the timestamp
break
case 2:
var range = sheet.getRange(reply["row"], 4);
range.setValue(tiempo);
break;
}
}
function Evaluar(vector2d, nombre, dia, option, last) {
var row = 0;
var i = 0;
var reply = "";
//loop #1 evaluate if student name exists and status of day and duration
for (var i = 0; i < vector2d.length; i++) {
if (vector2d[i][0] == nombre) {
if (vector2d[i][0] == nombre && vector2d[i][1] == dia) {
if (vector2d[i][0] == nombre && vector2d[i][1] == dia && vector2d[i][3] == "") {
reply = {
"option": 1,
"row": 0,
"result": 'name, day no date',
"i": (+i + 2)
};
break;
} else {
reply = {
"option": 1,
"row": 0,
"result": 'name, day, and date',
"i": (+i + 2)
};
break;
}
}
}
} // end for
//loop #2 - evaluate if student name is on the target sheet
for (var x = 0; x < vector2d.length; x++) {
if (vector2d[x][0] == nombre) {
break;
} else {
reply = {
"option": 1,
"row": 0,
"result": 'no name',
"i": (+i + 2)
};
}
} // end for
return reply;
}