检索Google表单响应数据

时间:2018-09-01 06:14:18

标签: google-apps-script google-form

我有一个Google表单,我需要在其中使用商品ID并获取该商品的所有回复。

我有以下脚本,如果表单的响应数超过3000,则会超时,因为它的效率很低

我如何优化它以在短时间内检索所有项目

fO.items = ["ItemID1","ItemID2","ItemID3"...];

    for (var i = 0; i < responses.length; i++) {
        var response = responses[i];
        var otherItems = '';
        var flag = true;
        for (var j = 0; j < fO.items.length; j++) {
            var item = form.getItemById(parseInt(fO.items[j]));
            if (response.getResponseForItem(item))
                var otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
            else
                flag = false;
        }
        if (flag) {
            columnData.push(otherItems);
            responseIds.push(response.getId());
        }
    }

1 个答案:

答案 0 :(得分:1)

当前,您的代码正在获取带有以下行的item对象:

var item = form.getItemById(parseInt(fO.items[j]));

因此,该行代码正在多次读取Form。 您可以尝试一次获取item对象,将它们放入JSON对象,然后根据需要进行检索。

我还没有测试过此代码,也不知道它是否可以工作,或者它是否可以工作得更快。但是以为我会同意这个想法。

function getSomeAnswers() {
  var form,flag,i,item,itemID,itemList,itemsObject,
      k,L,L_items,otherItems,responses,response,thisAnswer;

  itemList = ["ItemID1","ItemID2","ItemID3"];

  form = FormApp.getActiveForm();
  responses = FormApp.getActiveForm().getResponses();

  itemsObject = {};

  L_items = itemList.length;

  for (i = 0; i < L; i++) {//Compile a list of item objects
    itemID = parseInt(itemList[i]);
    itemsObject[itemID] = form.getItemById(itemID);
  }

  L = responses.length;

  for (i = 0; i < L; i++) {
    response = responses[i];
    otherItems = '';
    flag = true;     

    for (k in itemsObject) {//Loop through every item to get

      item = itemsObject[k];
      thisAnswer = response.getResponseForItem(item);

      Logger.log(thisAnswer)

      if (thisAnswer)
        otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
      else
        flag = false;
    }

    /*
    if (flag) {
      columnData.push(otherItems);
      responseIds.push(response.getId());
    }
    */
  }
}