Google Forms discrepancy - google-apps-script

When I add a list item to a Google Form I can set and get choices.
If I select a list item from an existing form I cannot.
items = form.getItems();
for (i=0;i<items.length;i++){
Logger.log(items[i].getType());
if (items[i].getTitle() == 'A List'){
choices = items.getChoices();
}
}
I can see the items are of type "List" in the log but getChoices throws an error
TypeError: Cannot find function getChoices in object item....
Is this a bug in Apps Script? Is there something I can do to cast the item and make sure it is of the right type?
Thanks
Jeremy

Please reffer to the documentation here. As you can see an item does not have a .getChoices() method. This method is available in specific types of items, like ListItem. So you need to specifically do something like
choices = items[i].asListItem().getChoices()
Remember that items is an array and you need to specify which item you are getting choices for. You then need to specify what type of item it is (i.e. a list item) and only then you can get the choices. If this is something you need to do for any type of item, then you will need to figure out how to check what kind of item it is and then get it as that item type.

Related

Need Validation on html dropdownlist with data from Viewbag

I know I'm not supposed to use the Viewbag and that I should build a menu using Html.DropDowlListFor() so that i can add attribute bound to the members of the model, BUT, that would involve a pretty extensive code rewrite....
I have a custom controller with a menu:
*.ASCX
<%: Html.DropDownList("CityIDs", new SelectList(ViewBag.cities, "Id", "Name"), "--Select--", new { style = "width:200px" })%>
<%= Html.ValidationMessage("CityIDs") %>
The List populates just fine and I can default to the top item to "--Select--"
The prbolem is that I want the validation error to occur on anything that is not from the viewbag.... how can I achieve this?
Validation for dropdown lists only ensures that something was posted. If you want to ensure that the value that was posted is actually one of a set of "allowed" values, then you'll have to manually do that in your post action:
var cityIds = db.Cities.Select(m => m.Id);
if (!cityIds.Contains(model.CityIDs))
{
ModelState.AddModelError("CityIDs", "You must select one of the available choices.");
}
if (ModelState.IsValid)
{
...
Two things:
Notice that I'm pulling the city ids straight from the database (the actual code you'd need here, of course, depends on your specific implementation). The important thing, though, is that ViewBag only survives a single request, so you can't look for them in ViewBag after posting.
Despite the pluralized name of CityIDs, using the DropDownList helper ensures that only a single selected value will exist. If this is actually supposed to be a multiselect, then you need to use ListBox instead, and update this conditional here to account for check for multiple values.
Populates the top item with Text = "--Select--" and Value = ""
which will post a blank value for CityIDs and cause an error in ModelState.

GAS error, listbox field displays "function addKeyPressHandler() {/* */}" instead of list

I just created this form and my first listbox works well, and notes box works well but second listbox is full of junk, non of which is found in the code?
starting with :
"function addKeyPressHandler() {/* */}"
then on to: mousedown, stylePrimary etc
any clue why?
var LOCATION = ['Select a Location','A Res','B Res','C Res','D Res','MNT','OSB','TWB','VP','VM','SITE DEV']
var ISSUETYPE = ['Select an ISSUE TYPE','Poor Workmanship','Lack of Training','Not Per Plans','Not Per Code','Not Per Spec','Plan Conflict']
Although it's hard to say exactly why without seeing more of your code, the "junk" you describe will show up when you are looping over and displaying properties of an object. You're seeing the functions attached to that object.
So check the code you are using to generate the select options on the problematic listbox, it's probably a typo, or perhaps you've inadvertently re-used a variable name and assigned an object too it, or maybe you have an un-quoted string that matches the name of some object.

Accessing ListBox selected item via UiApp.getActiveApplication().getElementByID()

Currently I am using UiService to create a form and I uses ListBox, from what I understand to pass a value via handler will be something like e.parameter.[Name of ListBox] to access the selected item.
Does anyone know is it possible to use like app.getElementById([Name of ListBox]) to access the selected item. The reason I am using this method is because my list of ListBox-es are dynamic.
I spent some time looking for this answer as well, but finally I tried one idea and it worked.
You can use e.parameter as an array so you can these two will give the same:
e.parameter.LIST_BOX_NAME
and
e.parameter['LIST_BOX_NAME']
So in the second sample any dynamic list box ID can be used. I use same handler for all added dropdown list and have this code to check what dropdown was changed and what value it has now:
if (e.parameter[e.parameter.source] == 'a'){
To change the content of the listBox you can use app.getElementById('ID of the listBox'), from there you can clear() and addItems again but you cannot read the listItems.
When I need to do this I usually store the list of items somewhere else, in a place that I can read anytime, for example the list of items can be stored as a string in the listBox tag itself so I have all items at hand to repopulate the listBox after I have clear it.

Validating input with jQuery autocomplete plugin

I'm using the standard autocomplete plugin here.
I have two text boxes on my form, item name and item id. The idea is that I want the user to search for the item by name, but on form submit I actually need the item id.
I am returning json from an ASP.NET MVC action which is used in the autocomplete.
The user starts typing an item name, the autocomplete list appears and is formatted to show the item name from the json object. The user can then click on a name from the list and the item id will be populated in the other textbox (which will actually be a hidden field once everything is working). I can then check that there is a value in this second textbox before submitting the form.
In the above scenario everything works great. But there are two huge bugs in what I've got so far:
1) The user has to actually click on the item in the list for the result function to fire. So if there is an item apples, and the user simply types apples directly into the textbox, the item id doesn't get populated.
2) The user could select apples from the list, which populates the item id. The user then changes his mind and goes back to the text box and types oranges. Again, if he doesn't actually click on the item in the list, the item id doesn't change and now when the form is submitted the wrong item id is submitted. Same thing if the user types something which isn't a valid selection, for example he changes the textbox to applesblahblahblah, the item id of apples is still going to be submitted even though a valid item choice wasn't made.
I've seen examples which suggest this can be solved by firing the search event, but I've tried that and it doesn't really seem to do much.
Does anyone know how to solve this problem?
My code so far is below, it's all pretty standard at the moment...
$('#ItemSearch').autocomplete("MyAction/FindItems", {
dataType: 'json',
parse: function(data) {
var parsed = [];
for (var i = 0; i < data.length; i++) {
parsed[parsed.length] = {
data: data[i],
value: data[i].Value,
result: data[i].Key
};
}
return parsed;
},
formatItem: function(row) {
return row.Value;
}
}).result(function(event, data, formatted) {
$(this).val(data.Value);
$('#ItemId').val(data.Key);
}).blur(function() {
// this is where I was trying to force an update of the item id textbox,
// but it doesn't work.
$(this).search();
});
I'd be greatful for any pointers. Thanks
Edit: If there is a better autocomplete which handles json and forced validation I'd be happy to hear suggestions as it might be easier than trying to get this one to work.
A better autocomplete may be the jQuery UI autocomplete, which does require the jQuery UI (pretty big library), but is more flexible. It has plenty of events that you can use to force validation.

Html.DropDownList select value not being set by Model

I have a drop down list like so:
<%= Html.DropDownList("SoldTo", Model.SellDealerList, "Select a Dealer")%>
I'm populating the list in my Controller with the following code:
SellDealerList = new SelectList(repository.GetDealerList(), "id", "name", vehicle.SoldTo);
When I debug through the code the selected value on the SellDealerList has a value that is being set but the HTML source shows no selected value in the list.
I'm baffled as to what I'm doing wrong, but being new to MVC I'm sure it is probably something very simple. The biggest difference from all the other questions and answers I've seen is that I'm not using the ViewData.
Thanks!
Check my answer to this question, maybe it helps.
Perhaps you have a property "SoldTo" in your model, ViewData or ModelState. The DropDown tries to override the selected value if some of these objects has a property or key with the same name as your field. Is kind of crazy if you don't know how it works internally because you see the selected property set to true on the right item of the SelectList and yet no option of the select is selected.