thwippp
1/11/2018 - 6:23 PM

Prefill URLs BACKUP

This is the backup of the Prefill URLs for Version 2 (school bus) of the COREPAandHFWCode.

There were too many students for the code to complete during the allotted time, so I broke it up into 4 sub sections (around 113 students at a time). This should help it complete chunks before the time expires.

/*
* Getting a prefilled URL is like sending a bus out to get passengers
* The bus is the formResponse
* The passengers are the formItems (each of which has an ID and type)
* They get on the bus in a particular order
* Once on the bus, it drives to a parking space
* Once many buses arrive, it becomes a parking lot--- this is the testSheet

 |,----.,----.,----.,--.\
 ||sec2||sec1 | St || T \\
 |`----'`----'|----||----\`.
 [Form Response   -||- __| ]  --> Prefill URL (parking space) --> array of URLs (parking lot)
 [  ,--.      |____||.--.| ]
 =-(( `))-----------(( `))=|
jrei`--'             `--'

// http://www.chris.com/ascii/index.php?art=transportation/busses
*/

function prefillUrls(){
  // Sheet /////////////////////////////////////////////////////////////////////////////////
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var responseSheet = ss.getSheetByName("Form Responses 1");
  var requiredSectionSheet = ss.getSheetByName("requiredSectionSheet");
  var testSheet = ss.getSheetByName("testSheet");
  var testSheetHeaders = testSheet.getDataRange().getValues()[0];
  
  var requiredSectionSheetData = requiredSectionSheet.getDataRange().getValues(); // gets all the values of the dataRange-- change this if the data is coming from a different sheet
  var requiredSectionSheetHeaders = requiredSectionSheetData[0]; // gets the header row
  var requiredSectionSheetHeadersLength = requiredSectionSheetHeaders.length;
 
  ///// Sheet Arrays /////
  var prefilledResponseArray = []; // creates the array of responses to use as values for the prefill
  var requiredSectionSheetLastRow = requiredSectionSheet.getLastRow();

  var prefilledUrlArray = []; // creates an array of the prefilled URLs to paste onto the spreadsheet
  for (var d = 0; d < requiredSectionSheetLastRow; d++) { // change the source sheet here and data above
    prefilledResponseArray.push(requiredSectionSheetData[d]);
  }  // for d
  
  // Form  /////////////////////////////////////////////////////////////////////////////////
  var formUrl = SpreadsheetApp.getActiveSpreadsheet().getFormUrl(); // gets the form URL that is attached to the spreadsheet
  var formId = FormApp.openByUrl(formUrl).getId(); // gets the id of the form
  var form = FormApp.openById(formId); // opens the form based on its id
  
  // Response  /////////////////////////////////////////////////////////////////////////////////
  
  ///// Form Arrays /////
  var titleArray = [];
  var idArray = []; // creates an array of the ids for each item
  var asTypeArray = []; // creates an array of the type of each item
  var formLength = form.getItems().length;
  
  for (var x = 0; x < formLength; x++) {
     titleArray.push(form.getItems()[x].getTitle());
    idArray.push(form.getItems()[x].getId());
    asTypeArray.push(form.getItems()[x].getType());
   }
    
  var testSheetIndexOfPrefillUrls = testSheetHeaders.indexOf("Prefill URLs");
  for (var s = 0; s < formLength; s++) { // removes page breaks and section headers--these don't need to be prefilled
    if (asTypeArray[s] == 'PAGE_BREAK' || asTypeArray[s] == 'SECTION_HEADER') {
      titleArray.splice(s,1);
      idArray.splice(s, 1);
      asTypeArray.splice(s, 1);
    }
  }
  
  var formRequiredSectionIdArray = [];
  var newAsTypeArray = [];
  for (var x = 0; x < requiredSectionSheetHeadersLength; x++) {
    formRequiredSectionIdArray.push(idArray[titleArray.indexOf(requiredSectionSheetHeaders[x])]);  // needs to be an ID-- id index = index of header value in the title array
    newAsTypeArray.push(asTypeArray[titleArray.indexOf(requiredSectionSheetHeaders[x])]);  // needs to be a type-- type index = index of header value in the title array
  }
  var formRequiredSectionIdArrayLength = formRequiredSectionIdArray.length;
  var formResponse = form.createResponse(); // Create a form response object--this is the bus
  for (var x = 0; x < requiredSectionSheetLastRow - 1; x++) { // last row of where the array is-- minus 1 because of headers
    for (var y = 0; y < formRequiredSectionIdArrayLength; y++) { // finds the index of -Prefill URLs- and iterates that number of times
      // Creates a DYNAMIC itemType from the asTypeArray in the corresponding index
      switch (newAsTypeArray[y]) {  // needs a type
        case FormApp.ItemType.CHECKBOX:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asCheckboxItem();  // these need an ID
          break;
        case FormApp.ItemType.MULTIPLE_CHOICE:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asMultipleChoiceItem();
          break;
        case FormApp.ItemType.LIST:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asListItem();
          break;
        case FormApp.ItemType.PARAGRAPH_TEXT:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asParagraphTextItem();
          break;
        case FormApp.ItemType.TEXT:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asTextItem();
          break;
        case FormApp.ItemType.DATETIME:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asDateTimeItem();
          break;
        case FormApp.ItemType.DATE:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asDateItem();
          break;
        case FormApp.ItemType.TIME:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asTimeItem();
          break;
        case FormApp.ItemType.DURATION:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asDurationItem();
          break;
        case FormApp.ItemType.GRID:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asGridItem();
          break;
        case FormApp.ItemType.SCALE:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asScaleItem();
          break;
        case FormApp.ItemType.CHECKBOX_GRID:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asCheckboxGridItem();
          break;
        case FormApp.ItemType.IMAGE:
          var formItem = form.getItemById(formRequiredSectionIdArray[y]).asImageItem();
          break;
        default:
          break;
      }  // switch
      try { var response = formItem.createResponse([ prefilledResponseArray[x+1][y] ]); // creates a response for each row in given sheet--- make sure to wrap the createResponse in []
        formResponse.withItemResponse(response); /* adds it to the form response (has all the passengers get on the bus)*/} catch(err){/*Logger.log("Error at Row: " + x + "Column: " + y)*/;continue;}
      }  // for y
      prefilledUrlArray.push(formResponse.toPrefilledUrl()); // puts the response into a URL (parking spot)
    }  // for x
    var prefilledUrlArrayLength = prefilledUrlArray.length;
  for (var d = 0; d < prefilledUrlArrayLength; d++) {
      testSheet.getRange(2 + d, testSheetIndexOfPrefillUrls + 1).setValue(prefilledUrlArray[d]); // puts the array of urls into the given sheet (parking lot) 
    }  // for d
  }  // function