title: Dynamically changing the Image file in a form
body: <p>I hope someone can help me whether what I am doing is the right way here.
  I need to have a single form for conducting online questions (only one question in a form) in my google site. The form has user, email, Question Id, An Image (Image is basically the question),Multichoice answer. Since the questions are too many I prefer to keep just one form for getting the answer.
  I plan to share (view only) spreadsheet to each user which has the URL for each test.
  My problem is how to change the image file on the fly when a user is clicking on the shared URL.</p>
  
  <p>What I did was I change the image based on a script  run, and the  ask the user to click on the URL to take test. Changing the image is done by creating a pilot form which will read question id from sheet and pick up image corresponding to it from g.drive. But I face an issue that the user has to refresh the page to get the right url( url changes only for question id, hence if not refreshed it picks up old question id).</p>
  
  <p>Is there a way it can be done with s single click from the user? Is there a way I can avoid refresh ? 
  The script is given below. Any help will be highly apprecialble.
   //This script is executed to create URL for pilot form based on the Question ID.</p>
  
  <pre><code>function BuildPilotUrls()  
  {
    var form = FormApp.openById("1fe3cuSwx2YgW6ruKTXFNbHcXSz3e0MztlSJqVb2pNkw"); //Pilot form
    var items = form.getItems();
    var ss = SpreadsheetApp.openById("1XNRrLPcWMNV8fORJn7jvfpc5WsM1i_rArCtZKD6b9pc");
  
    sheet = ss.getSheetByName("Questions");
  
    // var sheet = ss.getActiveSheet();
    var data = sheet.getDataRange().getValues();  // Data for pre-fill
  
    //var formUrl = ss.getFormUrl();             // Use form attached to sheet
  
    var formResponse = form.createResponse();
  
    // Skip headers, then build URLs for each row in Sheet1.
    for (var i = 1; i &lt;data.length; i++ ) {
      // Create a form response object, and prefill it
  
  
      // Prefill Name
      var formItem = items[0].asTextItem();
      var response = formItem.createResponse(data[i][5]);
      formResponse.withItemResponse(response);
  
  
      // Get prefilled form URL
      var url = formResponse.toPrefilledUrl();
      Logger.log(url);  
      sheet.getRange(i+1,10).setValue(url);
  
      url='HYPERLINK("'+url+'","click here")';
      sheet.getRange(i+1,9).setFormula(url);
  
    }
  };
  
  // This is triggered onSubmit of Pilot form with Question Id.
  function setImage()
  {
    //first open the pilot form. Get response for Qid and create file name.
    //check the file name in the folder and insert that image into the test form as actual question.
    var form = FormApp.openById("1fe3cuSwx2YgW6ruKTXFNbHcXSz3e0MztlSJqVb2pNkw"); ////CBSE-X-Maths-Pilot form
    var formResponses = form.getResponses();
  
    var formResponse = formResponses[formResponses.length-1];
    var itemResponses = formResponse.getItemResponses();
  
    var itemResponse = itemResponses[0];
    // Logger.log('Response #%s to the question "%s" was "%s"',
    //    (i + 1).toString(),
    //    itemResponse.getItem().getTitle(),
    var Qid=    itemResponse.getResponse();
    Logger.log('Response QID "%s" ',Qid);
    var array1 = [{}];
  
    array1 = Qid.split("-"); //prase based on -
    var str="C-X-M-";
    var ChNo="000"+array1[2];
    var QNo="000"+array1[3];
    ChNo=ChNo.slice(-3);
    QNo=QNo.slice(-3);
    var fileName= str+ChNo+"-"+QNo+".jpg";
    var myFolder = DriveApp.getFolderById('0B5AzqjFpdDjkTEYxQXFmSk43dEk'); //CBSE-X-Maths folder
  
    var files = myFolder.getFilesByName(fileName);
  
    while (files.hasNext()) {
      var file = files.next();
      Logger.log(file);}
    var form = FormApp.openById("1FUFb23eH8hrxbwJBrb1ezV0-W6k-IbQri2zXmGYZ0LQ"); //CBSE-X-Maths-Test Form
    var items = form.getItems();
    formItem = items[3].asImageItem();
    formItem.setImage(file.getBlob());
    //create URL
    var ss = SpreadsheetApp.openById("1XNRrLPcWMNV8fORJn7jvfpc5WsM1i_rArCtZKD6b9pc");
    sheet = ss.getSheetByName("UserUrl");
    var data = sheet.getDataRange().getValues(); 
    var formResponse = form.createResponse();
    for (var i = 1; i &lt;data.length; i++ ) {
  
      var formItem = items[0].asTextItem();
      var response = formItem.createResponse(data[i][2]);
      formResponse.withItemResponse(response);
      formItem= items[1].asTextItem();
      response = formItem.createResponse(data[i][3]);
      formResponse.withItemResponse(response);
      formItem= items[2].asTextItem();
      response = formItem.createResponse(Qid);
      formResponse.withItemResponse(response);
      var url = formResponse.toPrefilledUrl();
  
  
      url='HYPERLINK("'+url+'","click here")';
      sheet.getRange(i+1,5).setFormula(url);
    }
  };
  </code></pre>