在表单脚本中-获取Google表单值并弹出一个对话框以提交表单

时间:2019-05-14 16:01:01

标签: forms validation google-apps-script dialog

Google表单/表格-用户在表单上单击“提交”时,我要检查Google表格中是否已存在响应之一。如果是这样,我要防止提交表单,并弹出一条消息,告诉他们其数据已分配。

我尝试在电子表格级别添加Google脚本。但是弹出消息不起作用,因为表单是最终用户与之交互的表单,而不是工作表。

工作表中的代码

//Add code to form submit on the Add Key form
function onFormSubmit(e) {
  /* a variable name with Camel Case - every new word Capital */

  /* tell it to trigger on form submit */
  var ss = SpreadsheetApp.getActiveSpreadsheet();  /* sheet for data to goto */
  var sheet_SLAAssignedIdsResponses = ss.getSheetByName("SLAAssignedIdsRESPONSES"); 

  /* calls to the spreadsheet takes a lot of time - resource & same with writing to spreadsheet */
  /* better to to NOT go row by row - SLOW!!! */
  /* have it in memory in an array - faster, then modify it and spit it back out */
  /* house it in memory & read/write when have to */
  var lastRow_SLAAssignedIdsResponses = sheet_SLAAssignedIdsResponses.getLastRow(); /* getting the last added row */  

  var StudentAssignmentExistsAlready = "";
  var SLAIdValue = "";
  var SLAIdCount = "";
  var SLAStudentName  = "";

  // Assign formulas to G and H
   // G - Count # of times SLA ID appears assigned.
  var cell = sheet_SLAAssignedIdsResponses.getRange("G" + lastRow_SLAAssignedIdsResponses); 
    //=IF(ISBLANK(A2),"",COUNTIF(D:D,D2:D))
  cell.setFormula('=IF(ISBLANK(A2:A),"",COUNTIF(D:D,D2:D))');

  // H - Full Name
  var cell = sheet_SLAAssignedIdsResponses.getRange("H" + lastRow_SLAAssignedIdsResponses); 
  // IF(ISBLANK(E2:E)=FALSE,TEXT(E2:E,"00"),"")
  //=B2:B&" "&C2:C
  cell.setFormula('=B2:B&" "&C2:C');

  /*********************************************************/
  // Get the incoming SLAIdValue trying to be assigned.
  SLAIdValue = sheet_SLAAssignedIdsResponses.getRange("D" + lastRow_SLAAssignedIdsResponses).getValue(); 
  //SLAIdValue = sheet_SLAAssignedIdsResponses.getRange(lastRow_SLAAssignedIdsResponses, 3).getValue();
  Logger.log("The value of SLA ID " + SLAIdValue);

  // Get the COUNT for SLAId
  //SLAIdCount = sheet_SLAAssignedIdsResponses.getRange(lastRow_SLAAssignedIdsResponses, 6).getValue();
  SLAIdCount = sheet_SLAAssignedIdsResponses.getRange("G" + lastRow_SLAAssignedIdsResponses).getValue(); 
  Logger.log("The value of SLA ID COUNT " + SLAIdCount);

  // Get the current student assigned
  //SLAIdCount = sheet_SLAAssignedIdsResponses.getRange(lastRow_SLAAssignedIdsResponses, 6).getValue();
  SLAStudentName = sheet_SLAAssignedIdsResponses.getRange("H" + lastRow_SLAAssignedIdsResponses).getValue(); 
  Logger.log("The value of SLA Student Name " + SLAStudentName);

  /*********************************************************/
    // 1 - Look to see if the count for the current row is > 1 - If so, this SLA account is already assigned.
    // 2 - Find the SLAId already assigned for this student that is not equal to this one.

   **if (SLAIdCount >= 2 ){    
     Logger.log("Pop up response ");
   //  var ss = SpreadsheetApp.getActiveSpreadsheet();  /* sheet for data to goto */
    // var sheet_SLAAssignedIdsResponses = ss.getSheetByName("SLAAssignedIdsRESPONSES");

     var sh=SpreadsheetApp.getUi();
     var response=sh.alert("Duplicate SLA ID Assignemnt", "Please select a different Google Id for the Non-District Student.", sh.ButtonSet.OK);

     // Proceed with deleting the LastRow, as we cannot allow this entry. 


   }**

  } // OnFormSubmit END

我希望我可以回送一个对话框,以使表单提交者他们尝试分配的信息已经分配给其他人。

现在,我无法返回对话框。 我需要有关Google Apps for Forms脚本的帮助。

1 个答案:

答案 0 :(得分:0)

您确实知道Google表单设置具有一个内置选项,可以限制每个用户一次提交。