0

Question: What is causing the error in this script when replacing "asMultipleChoiceItem()" with "asCheckboxItem"? And is there an obvious way to correct it?

Short version: I am trying to implement a checkbox version of the solution (found here), by replacing "item.asMultipleChoiceItem()" with "item.asCheckboxItem()"; however, I'm encountering an error on debugging "Invalid conversion for item type: MULTIPLE_CHOICE." (debug image here). I'm having trouble troubleshooting to identify/understand, and therefore figure out how to correct, the error.

My code:

function DeptUpdate() {
  // User settings
  var OPTIONS_SPREADSHEET_ID = "1q21HxRkwXxVtiw7D5fuO-w0JCQtZRd-A35gRtmJUwKk";
  var OPTIONS_SHEET_NAME = "Present";
  var OPTIONS_RANGE = "A:A"; // We have the options, listed in column A
  var itemNumber = 1;         // which question on form does this apply to
  //
    var options2DArr = SpreadsheetApp.openById(OPTIONS_SPREADSHEET_ID).getSheetByName(OPTIONS_SHEET_NAME).getRange(OPTIONS_RANGE).getValues();
  var options = options2DArr.reduce(function(a, e) {
    if (e[0] != "") {
      return a.concat(e[0]);
    }
    return a;
  }, []);
  var form = FormApp.openById("1JHZoCdJrsRIltMwKqWGZizRQuy-2Ak2-XET83s04goc");
   var item = form.getItems()[itemNumber - 1];
  item.asCheckboxItem()
      .setTitle("SELECT NAME")
      .setChoiceValues(options)
      .showOtherOption(true);
}

Long version:

Goal: Google script in Google sheets, on trigger, updates targeted form's checklist options to reflect the items listed in the defined range (excluding blanks).

Purpose/Context: This is one part of a series of forms and spreadsheet that allow me to track arrivals, hall passes out/in, and departures from a study hall in which any 10-20 students from a pool of 120 possible may attend any given day. Spreadsheet is linked to forms to provide a "head's up display" of which students are present, and which are signed out to other locations (this all works fine). Restricting Hall Pass Out and Departure form answer choices (student names) to only those check in as "present" drastically cuts down on time and user errors in the logging system. Currently works with multiple choice, but students frequently arrive/leave in groups. Checkbox (multiple response) would further expedite the tracking process. Spreadsheet is otherwise set up to process multiple response entries; just need the form to appropriately update.

Process/Attempts: I've read of others who adjusted similar (different purpose) scripts to change from dropdown/multiple choice to checkbox without issue ("I just change this and it worked, great!" is the extent of what I've read), but as soon as I change to checkbox, I get the attached error for both the showOtherOption field, and (if that is removed), the setChoiceValues field. I'm thinking it could possibly be an issue with the checkbox item reading the array differently than the multiple choice item does? However, I haven't be able to find anything in the documentation or Q/A posts on a significant difference between the two functions parameters. At this point, I'm just a little flummoxed on what might be causing the issue.

Background: I have no formal (or significant informal) coding training, but have tweaked and adapted a variety of codes for about a decade. I understand the basic processes/concepts of code and programming logic, but lack a substantial/cohesive vocabulary.

I'm including a link to a dummy copy of the spreadsheet and the form, in case that's helpful.

Spreadsheet

Form

Thank you in advance for any insights!

Brandy

  • For Google Form, you want to replace "Multiple choice item" to "Checkbox item". If my understanding is correct, when I saw your shared Google Form, it seems that the item is "List item". This is not "Multiple choice item". How about this? If I misunderstood your question, I apologize. – Tanaike Jan 23 '20 at 01:58

1 Answers1

1

The problem is that you have a ListItem but try to convert it to a CheckboxItem

This is not directly possible. There is a feature request on Google's Public Issue Tracker for this feature. I recommend you to give it a "star" to increase visibility.

In the mean time, if you want to convert an item type, you need to do it manually by creating a new item, passing it the title and choice from the old one and deleting the old item:

Sample

function DeptUpdate() {
  // User settings
  var OPTIONS_SPREADSHEET_ID = "1wHE6b5ZuAKJTM4N7t6nlB5SdU9h24ueuxon4jnH_0sE";
  var OPTIONS_SHEET_NAME = "Present";
  var OPTIONS_RANGE = "A:A"; // We have the options, listed in column A
  var itemNumber = 1;         // which question on form does this apply to
  //
    var options2DArr = SpreadsheetApp.openById(OPTIONS_SPREADSHEET_ID).getSheetByName(OPTIONS_SHEET_NAME).getRange(OPTIONS_RANGE).getValues();
  var options = options2DArr.reduce(function(a, e) {
    if (e[0] != "") {
      return a.concat(e[0]);
    }
    return a;
  }, []);
  var form = FormApp.getActiveForm();
  var listItem = form.getItems()[itemNumber - 1];
  listItem
      .asListItem()
      .setTitle("SELECT NAME")
      .setChoiceValues(options)
  var title = listItem.getTitle();
  var choices = listItem.asListItem().getChoices();
  var checkboxItem = form.addCheckboxItem();
  checkboxItem.setTitle(title)
               .setChoices(choices)
               .showOtherOption(true);
  form.deleteItem(listItem);  
}
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • This is amazing and very useful! I just want to check: Since this is a conversion issue, does that mean if I start with a fresh form and create a checkbox item to begin with, then add the script as a checkbox item (my originally modified code), that would also work? (to check my understanding): i.e. the script can update a checkbox as a checkbox, the problem was updating an existing multiple choice question into a checkbox? Again, thank you so much! (Will mark this as answer as soon as I feel I understand this last bit). – Brandy Weaver Jan 23 '20 at 15:38
  • Yes, exactly. If you create an item as a checkbox item in the first place (or change the item type manually from the UI) you will not need to change it in Apps Script and you will not encounter problems with your code. – ziganotschka Jan 23 '20 at 15:50