1

I'm trying create a "simple" in and out system, based on qr codes. I'm getting the responses fine. I need to evaluate if the value on the first column exists or not, if it doesn't exist, add everything to a new row, if not check if the day is the same if its then add the current time to the 4th column.

 efunction alsubmit(e) {
  //getting the form ready
  var formResponse = e.response;
  var itemResponses = formResponse.getItemResponses();
  //getting the items
  var nombre = itemResponses[0].getResponse()
  var tiempo =  formResponse.getTimestamp();
  // get the spreadsheet where the info will be stored ready  
  var ss = SpreadsheetApp.openById('1gqbTqb2xOt3rORKcQ_7weGynj9eWypt5gS6ahKVfci8');
  var dia = tiempo.getDate();
  var hoja = tiempo.getMonth();
  var sheet = ss.getSheets()[hoja+1];
  // coordenates
  var last = sheet.getLastRow();
  var row = 0;
  // values from the sheet
  var vector2d = sheet.getRange(2,1,last,4).getValues();
 // switch variable
  var option = 0;
Evaluar(vector2d, nombre, dia, option);
  var range = sheet.getRange(2,6);
  range.setValue(option);
  switch(option){
    case 1:
  var range = sheet.getRange(last+1,1);
  range.setValue(nombre);
  var range = sheet.getRange(last+1,2);
  range.setValue(dia);
  var range = sheet.getRange(last+1,3);
  range.setValue(tiempo);
  break    
    case 2:
  var range = sheet.getRange(row,4);
  range.setValue(tiempo);
  break   
 }

}

  function Evaluar(vector2d, nombre, dia, option, row){
      for (var i=0; i<vector2d.length; i++){
        if (vector2d[i][0] != nombre){
          option =1;
          break
        }
        if (vector2d[i][0] == nombre && vector2d[i][1] == dia && vector2d[i][3] == null){
          option =2;
          row = i+1;
          break
        }
        if (vector2d[i][0] == nombre && vector2d[i][1] == dia && vector2d[i][3] != null){
          option =1;
          break
        }

      }
    }

It's always giving me the case 1.
Thanks for the help!!

  • Welcome. I have requested access to your spreadsheet to see the data layout. – Tedinoz Oct 28 '18 at 23:42
  • 1) The function "Evaluar" doesn't _return_ a value to alsubmit. I reckon "Option" always has a value of 0. 2) You don't declare any variable at line 20 to hold the value of Evaluar if it was returned. 3) Suggest you debug &/or run Execution Transcript &/or add some Logger.log lines to get values of variables at important moments; my guess is that a key value is undefined. 4) Highly recommend you read the answer by Serge insas in [Call another function from within same script file](https://stackoverflow.com/questions/20428462/call-another-function-from-within-same-script-file). – Tedinoz Oct 29 '18 at 00:08
  • Oops. Meant to add, "A function can only return one value". But Evaluar appear to set values for 'row' **and** 'option' in one case. You might want to read [Return multiple values and access them?](https://stackoverflow.com/questions/39585021/return-multiple-values-and-access-them) which outlines a good approach to returning and accessing multiple values. – Tedinoz Oct 29 '18 at 00:19

1 Answers1

0

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;

}
Tedinoz
  • 5,911
  • 3
  • 25
  • 35