Google Apps脚本:将数据提交到指定列

时间:2014-01-30 15:07:02

标签: google-apps-script

我正在尝试找到一个脚本,或者开始编写一个脚本,它带有一个简单的Google表单,其中包含一个名称下拉列表(即Tom,Jane)和一个文本区域,并将日期和文本输入到基于所选名称的列(即Tom Date,Tom Comment)。这样我就可以制作一个快速的入门反馈表,为学生提供个性化的,基于日期的反馈,然后他们可以在以后访问。

我查看了GAS文档并查找了示例,但由于我是新手,我真的不知道从哪里开始。

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:0)

我认为我做了类似的事情,但我的是管理员观察老师。我只是在学习,所以我确信有更好的方法可以做到这一点,但它有效。我绝对应该把它分解成更多的功能。

我的表单有一个触发器,可以在提交时触发onClose()。 onClose()通过阅读包含表单数据的电子表格生成Google Doc,然后观察者可以编辑并与教师共享。我希望生成的Google Doc在文件名中显示教师的名字,我希望它与执行观察的管理员共享。 某些字段是下拉列表的事实并不重要,它只是所有响应列表中的itemResponse。

function onClose()
{

 var form = FormApp.openById(' whatever your form id is');

//the spreadsheet of the form responses 
 var formResponses = form.getResponses();

  var d = new Date();
  var currentTime = d.toLocaleTimeString();
  var date = d.toLocaleDateString();

  //Need to get the name of the teacher being observed
  var formResponse = formResponses[formResponses.length-1];

  var itemResponses = formResponse.getItemResponses();
  var itemResponse = itemResponses[0]; //the teacher name dropdown box
  var teacherName = itemResponse.getResponse() + '-' + itemResponses[1].getResponse();

  //create the new document
    var fileName = 'Observation-'+ teacherName + '-' + date + '-' + currentTime;
    var doc = DocumentApp.create(fileName);
    var activeDoc =  DocumentApp.getActiveDocument();

    var files = DriveApp.getFilesByName(fileName);

    while (files.hasNext()) {

            var file = files.next();
            if (file.getName().equals(fileName))
                {
                  //this is the last item on my form the name of the observer
                  var itemResponse21 = itemResponses[21];
                  var observer =  itemResponse21.getResponse();
                  // Logger.log('Person to share with is ' + observer);
                  // share this google doc with the observer
                  file.addEditor(observer);

                 }
      }


        //ommittd a bunch of styles   


//This would get all forms submitted, but I only need the last one
// so I just set the loop to get the last form submitted.
//leaving for loop just so I remember I can go through all forms again
//if I want to. 

 for (var i = formResponses.length-1; i < formResponses.length; i++) {

   var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();


   //get the individual responses within the form.addCheckboxItem()
   for (var j = 0; j < itemResponses.length; j++) {

     //pull the first item out again (even though I did for file name)    
      var itemResponse = itemResponses[j];  //teacher name from a dropbox
      var itemResponse2 = itemResponses[j+1]; //date
      var itemResponse3 = itemResponses[j+2]; //time


      if (j == 0)  //the first field on the form
      { 
        //put the headings in 
        par3 = doc.appendParagraph(' SCHOOL NAME');
        par3 = doc.appendParagraph('WALK IN OBSERVATION');
        par3 = doc.appendParagraph('2013-2014');

      //THIS is the teacher being observed and the date and time --- all on same line

       var headingLine = itemResponse.getItem().getTitle() + '\t\t' + itemResponse2.getItem().getTitle() + ' / ' + itemResponse3.getItem().getTitle();
       par1 = doc.appendParagraph(headingLine);
       var answerLine = itemResponse.getResponse() + '\t\t\t\t\t' + itemResponse2.getResponse() + ' / ' + itemResponse3.getResponse();
       par2 =  doc.appendParagraph(answerLine);
       j++;  //do this to skip over date and time
       j++;

      } //end of j = 0;
      else
// then I have a bunch of if statements for some of the
// specific fields I need to do something special with.
// After the last if, I just have an else to handle all other
// form responses that I don't do anything special for other than display.
//my last else is:
      else
   //THIS ELSE IS HANDLING ALL NON CHECK BOXES AND JUST DISPLAYING THE TITLE IN BOLD FONT, THE COMMENTS IN REGULAR FONT
      {
        par1 = doc.appendParagraph(itemResponse.getItem().getTitle());
        par1.setAttributes(style);
        par2 =  doc.appendParagraph( itemResponse.getResponse());
        par2.setAttributes(style);
     } //END OF ELSE 


   } //end of for going through each cell in a row of the repsonses
  } //end of for going through each row -- only had it set to do the very last row