Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

e.values in google forms skips empty answers, is there a workaround?

I have a Google form, which writes response data to a spreadsheet containing a script that is supposed mail the form-filler with his/her answers to the form.

I had success with e.values before, and the mail generated just fine. Now there seems to be some problems, as empty answers are skipped, meaning that e.values[9] for example becomes actually column 9, instead of 10, as it used to be (in the spring of 2014, at least). So if there are one or more fields left empty the following answers move backward in the array one or more steps, depending on the number of questions left empty. Behaviour like this wreaks havoc in carefully planned scripts!

I also tried to use namedValues, but it can't tolerate empty fields either!

Does anybody know of a work around? I'd appreciate ideas.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That open issue has been marked Working as intended. So no help there.

Here's a work-around. Also available in this gist.

Example

function onFormSubmit(e) {
  fixFormEvent( e );
  ...
}

fixFormEvent( e )

/**
 * Force blank reponses into event object's values property, so that the value's index
 * correctly reflects the question order. (With "new Sheets" + "new Forms", blank responses
 * are skipped in the event object.
 *
 * see http://stackoverflow.com/a/26975968/1677912
 *
 * @param {event} e   Event received as a Spreadsheet Form object. The event's value
 *                    property will be modified by this function.
 * @return {event}    The same event, for chaining
 */
function fixFormEvent( e ) {
  var ss = SpreadsheetApp.getActive();
  var formUrl = ss.getFormUrl();             // Use form attached to sheet
  var form = FormApp.openByUrl(formUrl);
  var items = form.getItems();

  var resp = [e.namedValues["Timestamp"]];

  for (var i=0; i<items.length; i++) {
    switch (items[i].getType()) {
      case FormApp.ItemType.IMAGE:
      case FormApp.ItemType.PAGE_BREAK:
      case FormApp.ItemType.SECTION_HEADER:
        // Item without a response - skip it
        break;

      case FormApp.ItemType.CHECKBOX:
      case FormApp.ItemType.DATE:
      case FormApp.ItemType.DATETIME:
      case FormApp.ItemType.DURATION:
      case FormApp.ItemType.GRID:
      case FormApp.ItemType.LIST:
      case FormApp.ItemType.MULTIPLE_CHOICE:
      case FormApp.ItemType.PARAGRAPH_TEXT:
      case FormApp.ItemType.SCALE:
      case FormApp.ItemType.TEXT:
      case FormApp.ItemType.TIME:
        // If item has a response, append it to array. If not, append blank.
        var itemTitle = items[i].getTitle();
        var type = items[i].getType();
        if (itemTitle === "") throw new Error( "Untitled item" );
        var itemResp = [];
        if (itemTitle in e.namedValues) {
          itemResp = e.namedValues[itemTitle];
        }
        resp.push( itemResp );
        break;

      default:
        Logger.log( "Unknown item type, index=" + items[i].getIndex() );
        break;
    }
  }
  e.values = resp;
  return e;  // For chaining
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...