Google脚本在数组中找到一个值并追加值

时间:2018-10-24 16:53:56

标签: javascript arrays google-apps-script

我正在尝试基于qr代码创建一个“简单”的输入和输出系统。我的反应很好。我需要评估第一列的值是否存在,如果不存在,则将所有内容添加到新行,如果不检查日期是否相同,则将当前时间添加到第四列。

 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
        }

      }
    }

这总是给我提供帮助1。
谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

发问者在评估来自表单答复的信息以及目标表上是否已经存在学生的姓名时遇到问题。无论学生的详细信息如何,答案始终与特定的结果相符。

这样做的原因是,首先要评估的是学生的姓名是否在目标表上。注意“ break”命令。

if (vector2d[i][0] != nombre){
          option =1;
          break
}

因此,仅要求学生的姓名与目标表上的名字不匹配,并且IF语句被评估为TRUE,随后的“中断”结束了整个for循环。这样做的结果是,第一个结果未得到令人满意的评估,而其他两个结果从未得到评估。

由于学生的姓名可能不正确,并且这些结果推动了进一步的评估,所以我创建了两个循环。一个用于确定学生的姓名是否在目标上,第二,如果它在目标上,则可以评估其他标准。

此外,如评论中所述,评估阶段最多返回两项:1)选项和2)行。通过创建Javascript对象可以解决此问题。


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;

}