1

I'm trying to make a script for Google Sheets that uses a spreadsheet to create a form. I'm passing the values in the columns (loaded into the object 'dataObject') of the sheet into an Array of objects as shown.

  for (var i = 0; i < dataObject.length; i++)
  {
      dataObjectArray[i] = {
          question:    dataObject[i][3],
          type:        dataObject[i][4],
          help:        dataObject[i][5],
          choices:     dataObject[i][6],
          options:     dataObject[i][7],
          validation:  dataObject[i][8]
      }
  }

My goal is to then use the "question" property of each object to make a new item in my Google form. The problem I'm having is that depending on the question type, a completely different method is required to make the form.

The first thing I tried was making a dictionary of functions like so:

 var form = FormApp.create('Foo'); 
 var formDictionary = {
    "Text": function(){form.addTextItem();},
    "Paragraph Text": function(){form.addParagraphTextItem();},
    "Multiple Choice": function(){form.addMultipleChoiceItem();},
    "Checkboxes" : function() {form.addCheckboxItem();},
    "Choose from a list": function(){form.addListItem();},
    "Scale": function(){form.addScaleItem();},
    "Grid": function(){form.addGridItem();},
    "Date": function(){form.addDateItem();},
    "Time": function(){form.addDateTimeItem();}
  };

And then I would call the form creation function like so:

   for (var i = 1; i < dataObjectArray.length; i++){
    formDictionary[dataObjectArray[i].question]();
  }

At the moment, I'm receiving the error: TypeError: Cannot find function undefined in object [object Object]. (line 113, file "Code")

I know that there are probably more than a few problems with what I have tried (I have caught a few of them myself), but I can't find a better solution.

I'd like to avoid switch statments if possible, though I'll use them if they're my only choice.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Use `Logger.log()` statements to print information to the LOG. Then choose VIEW, LOGS from the menu. Check if values are what you expected. We need to know what you expected compared to what you are getting. – Alan Wells Jan 27 '15 at 23:51
  • @SandyGood I've actually been doing that but haven't gotten any useful information out of it. I can tell you that the the `dataObjectArray` in the first for loop is loaded correctly. The script was running just fine before I added the final `for` loop. Also, I've added the error description into the question. – Christian Wimber Jan 28 '15 at 11:34
  • The error msg is stating that the function is undefined. The function is `question()`, which is also a property in an object. So the problem is probably with the `dataObjectArray` object. If `dataObjectArray` isn't a global variable, and the function calling `dataObjectArray` is in a different function, the data in `dataObjectArray` may have been lost. Are you calling the `dataObjectArray` object in the same function where it was defined? – Alan Wells Jan 28 '15 at 14:53

1 Answers1

1

Use javascript prototype.

function MyFunction(){
  return
}

MyFunction.prototype = {
  test1:(function(){return "Hello"}),
  test2:(function(){return "World"}),
}
var foo =  new MyFunction();
Logger.log(foo.test1() +" "+ foo.test2())
  • What am I prototyping in? The dictionary? The function that invokes the dictionary? How does one prototype the `Form` object? I haven't found any documentation. – Christian Wimber Jan 28 '15 at 11:35
  • Here is some documentation from Mozilla: [Mozilla Documentation - Function Prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype) – Alan Wells Jan 28 '15 at 14:39
  • Here is a StackOverflow answer about prototype: [SO - Why use Prototype](http://stackoverflow.com/a/8433493/2946873) – Alan Wells Jan 28 '15 at 14:42