使用Google应用脚本生成特定于用户的表单

时间:2017-08-11 14:53:49

标签: google-apps-script

我试图从一家大银行创建一个包含20个左右多项选择题的表格(调查,不对或错,如果重要的话)。

我已经看到了一些关于如何从谷歌电子表格生成表单的示例(例如http://alicekeeler.com/2014/12/12/google-forms-create-a-quiz-from-a-question-bank/接近),但我的问题更进一步,我需要生成一个20随机的表单来自银行的每个用户的问题。

在网上搜索并阅读了大量文档之后,我仍然很难确定是否可以使用Google应用脚本为每个观看者生成不同的表单(如果是的话,如何)或者我是否应该转向更强大的东西。

1 个答案:

答案 0 :(得分:0)

来自问题库的随机问题

一个示例,说明如何进行项目,从问题库中随机选择问题,并以html格式显示。单击提交请求并确保已回答所有问题并将响应存储在电子表格中。 Here's a link to this code and a video demo of the project

这是Code.gs文件:

function onOpen()
{
    SpreadsheetApp.getUi().createMenu('Questions Menu')
      .addItem('Questions', 'questionsToHtml')
      .addToUi();
}

function selectTest()
{
  var qA=selectQuestions(5,24);
  Logger.log(qA);
}

function selectQuestionIndexes(n,m)
{
  var set=[];
  do
  {
    var i=getRandomIntInclusive(0,m);
    if(set.indexOf(i)<0)
    {
      set.push(i);
    }
  }while(set.length<n);
  return set;
}

function getRandomIntInclusive(min, max) 
{
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

function getCpData()
{
  var ss=SpreadsheetApp.getActive();
  var cpSh=ss.getSheetByName('ControlPanel');
  var cpRg=cpSh.getDataRange();
  var cpVa=cpRg.getValues();
  var qsrcSh=ss.getSheetByName(cpVa[1][0]);
  var adesSh=ss.getSheetByName(cpVa[1][1]);
  var qnum=cpVa[1][2];
  var cpData={'qSrc':cpVa[1][0],'aDes':cpVa[1][1],'qNum':cpVa[1][2]};
  return cpData;
}

function getAnswerIndexes()
{
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName(getCpData().qSrc);
  var rg=sh.getRange(1,1,1,sh.getLastColumn());
  var vA=rg.getValues();
  var re=/Answer \d{1,2}/i;
  var fidx=0;
  var lidx=0;
  var first=true;
  for(var i=0;i<vA[0].length;i++)
  {
    if(String(vA[0][i]).match(re))
    if(first)
    {
      fidx=i;
      first=false;
    }
    else
    {
      lidx=i;
    }
  }
  return {'firstIdx':fidx,'lastIdx':lidx};
}

function testResp()
{
  var row = [[1,'What is the question?','no'],[2,'What is the question?','no'],[3,'What is the question?','no']];
  recordData(row);

}


function recordData(responses)
{
  if(responses)
  {
    var ss=SpreadsheetApp.getActive();
    var sheetname=getCpData().aDes;
    var sh=ss.getSheetByName(sheetname);
    var ts=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss"); 
    for(var i=0;i<responses.length;i++)
    {
      responses[i].splice(0,0,ts);
      sh.appendRow(responses[i]);
    }
  }
  return true;
}

function questionsToHtml(web) 
{
  var web=(typeof(web)!='undefined')?web:false;
  var br='<br />';
  var cm=',';
  var ss=SpreadsheetApp.getActive();
  var cpData=getCpData();
  var qnum=cpData.qNum;
  var qa=getQAndA();
  var qi=getAnswerIndexes();
  var s='';
  for(var i=0;i<qa.length;i++)
  {
    //s+='<table>';
    //s+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' , qa[i][0],qa[i][1],(qa[i][2])?qa[i][2]:'&nbsp;',(qa[i][3])?qa[i][3]:'&nbsp;',(qa[i][4])?qa[i][4]:'&nbsp;',(qa[i][5])?qa[i][5]:'&nbsp;');
    //s+='</table>';
    s+='<div id="d' + qa[i][0] + '" style="font-weight:bold;">' + qa[i][1];
    s+='<input type="hidden" value="' + qa[i][0] + '" class="hiding" />';
    for(var j=qi.firstIdx;j<=qi.lastIdx;j++)
    {
      if(qa[i][j])
      {
        s+=br + '<input type="radio" name="n'+ qa[i][0] +'" value="' + qa[i][j] + '" />' + qa[i][j];
      }
    }
    s+='</div>'


  }
  s+='<div id="controls">';
  s+=br + '<input type="button" value="Submit" onClick="recordData();" />';
  //s+=br + '<input type="button" value="Do It Again" onClick="google.script.run.questionsToHtml();" />';
  s+='</div>';
  s+='</body></html>';
  //Logger.log(s);
  if(!web)
  {
    var userInterface=HtmlService.createHtmlOutputFromFile('htmlToBody').append(s).setWidth(1000).setHeight(500);
    SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Current Form')
   }
   else
   {
     var output=HtmlService.createHtmlOutputFromFile('htmlToBody').append(s);
     return output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
   }
}

function doGet()
{
  return questionsToHtml(true)
}

function getQAndA()
{
  var qa=[];
  var cma=',';
  var ss=SpreadsheetApp.getActive();
  var cpData=getCpData();
  var qsrcSh=ss.getSheetByName(cpData.qSrc);
  var qsrcRg=qsrcSh.getRange(2,1,qsrcSh.getLastRow()-1,qsrcSh.getLastColumn());
  var qsrcVa=qsrcRg.getValues();
  var qs=selectQuestionIndexes(cpData.qNum,qsrcVa.length-1);
  var aIdxs=getAnswerIndexes();
  for(var i=0;i<qsrcVa.length;i++)
  {
    var qas='';
    if(qs.indexOf(i)>-1)
    {
      qas+=qsrcVa[i][0] + cma + qsrcVa[i][1];
      for(j=aIdxs.firstIdx;j<=aIdxs.lastIdx;j++)
      {
        if(qsrcVa[i][j])
        {
            qas+= cma + qsrcVa[i][j];
        }
      }
      qa.push(qas.split(cma));
    }
  }
  return qa;
}

这是htmlToBody.html文件

<!DOCTYPE html>
<html>
<head>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $(function() {

      });
    function recordData()
    {
      var responses=[];
      var cm=',';
      var divs = document.getElementsByTagName("div");
      for(var i=0;i<divs.length;i++)
      {
        var id=divs[i].getAttribute(['id']);
        var qnum=$('div#' + id + ' ' + 'input.hiding').val();
        var question=document.getElementById(id).innerHTML;
        var answer=$('input[name="n' + qnum + '"]:checked').val();
        if(id!='controls')
        {
          if(!answer)
          {
            window.alert('You did not answer question number ' + Number(i+1) + '. It is a requirement of this survey that all questions must be answered.' );
            return;
          }
          else
          {
            var end='is near';
            var s=qnum + cm + question + cm + answer;
            responses.push(s.split(cm));
          }
        }
      }

      google.script.run
          .withSuccessHandler(displayThanks)
          .recordData(responses);
    }
    function displayThanks()
    {
       var divs = document.getElementsByTagName("div");
      for(var i=0;i<divs.length;i++)
      {
        divs[i].style.cssText="display:none;text-align:center";
      }
      var elemDiv = document.createElement('div');
      elemDiv.innerHTML="<br /><h1>Thank You For Your Participation in This Survey</h1>";
      document.body.appendChild(elemDiv);
    }
    console.log('My Code Here');
    </script>
    <style>
    #reply{display:none;}
    #collect{display:block;}
    body  {
    background-image: url("");
    background-color: #ffffff;
    background-repeat: no-repeat;
}
    </style>
</head>  
<body>