使用谷歌应用脚​​本添加大量响应

时间:2017-07-12 05:57:02

标签: google-apps-script google-sheets google-form

我想创建一个脚本,它将添加大量(约1500个)CV和候选信息作为对表单的响应。我将这些信息作为谷歌电子表格。我将它链接到表单但我只能管理响应。即使我更改了电子表格(或添加),也不会在表单中进行更新。我正在添加行但没有添加任何响应。那可能吗?我看到了2-3个问题,但他们已经老了,我仍在努力寻找答案。这是我的实习项目,我没有多少时间了。提前谢谢。

2 个答案:

答案 0 :(得分:1)

例如,您可以将答案上传到电子表格,将其放入数组并通过FormResponce对象提交(为此您需要使用Form#createResponse)。

我没有测试过这段代码,但据我所知,这样的代码应该对你有用。

var ANSWER_SHEET_ID = "xxx";
var FORM_ID = "yyy";

function myFunction() {
  const answers = SpreadsheetApp.openById(ANSWERS_SHEET_ID)
                       .getSheetByName('answers')  //Let's say that sheet is named
                       .getRange(answerSheet.getLastRow(), answerSheet.getLastColumn())
                       .getValues() //get it as the Array
                       .slice(1); //slice the header row if it exists in your answer_sheet

  const form = FormApp.openById(FORM_ID);

  const formResponse = form.createResponse();
  const items = form.getItems();

  for(var i=0, len=answers.length; i<len; ++i){
    //you have to define types of your Items like this
    formResponse.withItemResponse(items[0].asTextItem().createResponse(answers[i][0])) //for Text Item
                .withItemResponse(items[1].asMultipleChoiceItem().createResponse(answers[i][1])) //Multiple Choice Item
                .withItemResponse(items[2].asScaleItem().createResponse(parseInt(answers[i][2]))) //for Scale Item !! requires integer as a response
                .submit();
  }
}

请注意,有些项目需要Integer作为响应,而其他项目则需要String等。

答案 1 :(得分:1)

感谢你们,我可以添加一个项目作为回应。这对我来说是一个很好的开始!这是我写的代码:

const answers = sheet.getSheetValues(2, 2, sheet.getLastRow()-2, sheet.getLastColumn()-2);


var formResponse = new Array(answers.length);
for(var i =0; i < answers.length; ++i)
formResponse[i] = form.createResponse();

const items = form.getItems();

for(var i=0, len=answers.length; i<len; ++i){

   formResponse[i]
   .withItemResponse(items[1].asDateItem().createResponse(answers[i][0]))
   .withItemResponse(items[2].asTextItem().createResponse(answers[i][1]))
   .withItemResponse(items[3].asTextItem().createResponse(answers[i][2]))
   .withItemResponse(items[4].asTextItem().createResponse(answers[i][3]))
   .withItemResponse(items[5].asTextItem().createResponse(answers[i][4]))
   .withItemResponse(items[6].asDateItem().createResponse(answers[i][5]))
   .withItemResponse(items[7].asParagraphTextItem().createResponse(answers[i][6]))
   .withItemResponse(items[8].asTextItem().createResponse(answers[i][7]))
   .withItemResponse(items[9].asTextItem().createResponse(answers[i][8]))
   .withItemResponse(items[10].asListItem().createResponse(answers[i][9]))
   .withItemResponse(items[11].asListItem().createResponse(answers[i][10]))

   .withItemResponse(items[12].asParagraphTextItem().createResponse(answers[i][11]))
   .withItemResponse(items[13].asParagraphTextItem().createResponse(answers[i][12]))
   .withItemResponse(items[14].asParagraphTextItem().createResponse(answers[i][13]))
   .withItemResponse(items[15].asParagraphTextItem().createResponse(answers[i][14]))
   .withItemResponse(items[16].asParagraphTextItem().createResponse(answers[i][15]))

   .withItemResponse(items[18].asTextItem().createResponse(answers[i][16]))

   //.withItemResponse(items[19].asCheckboxItem().createResponse(answers[i][17]))

   .withItemResponse(items[21].asTextItem().createResponse(answers[i][18]))
   .withItemResponse(items[22].asListItem().createResponse(answers[i][19]))
   .withItemResponse(items[23].asTextItem().createResponse(answers[i][20]))
   .withItemResponse(items[24].asListItem().createResponse(answers[i][21]))
   .withItemResponse(items[25].asTextItem().createResponse(answers[i][22]))
   .withItemResponse(items[26].asListItem().createResponse(answers[i][23]))

   .withItemResponse(items[28].asTextItem().createResponse(answers[i][24]))
   .withItemResponse(items[29].asTextItem().createResponse(answers[i][25]))
   .withItemResponse(items[30].asTextItem().createResponse(answers[i][26]))
   .withItemResponse(items[31].asTextItem().createResponse(answers[i][27]))
   .withItemResponse(items[32].asTextItem().createResponse(answers[i][28]))
   .withItemResponse(items[33].asTextItem().createResponse(answers[i][29]))

   .withItemResponse(items[35].asTextItem().createResponse(answers[i][30]))
   .withItemResponse(items[36].asParagraphTextItem().createResponse(answers[i][31]))       
   .withItemResponse(items[37].asParagraphTextItem().createResponse(answers[i][32]))
   .withItemResponse(items[38].asParagraphTextItem().createResponse(answers[i][33]))



   .withItemResponse(items[40].asTextItem().createResponse(answers[i][34]))
   .withItemResponse(items[41].asListItem().createResponse(answers[i][35]))
   .withItemResponse(items[42].asParagraphTextItem().createResponse(answers[i][36]))

   .withItemResponse(items[44].asDateItem().createResponse(answers[i][37]))
   .withItemResponse(items[45].asTextItem().createResponse(answers[i][38]))
   .withItemResponse(items[46].asParagraphTextItem().createResponse(answers[i][39]))

   .withItemResponse(items[48].asDateItem().createResponse(answers[i][40]))
   .withItemResponse(items[49].asTextItem().createResponse(answers[i][41]))
   .withItemResponse(items[50].asParagraphTextItem().createResponse(answers[i][42]))

   .withItemResponse(items[52].asDateItem().createResponse(answers[i][43]))
   .withItemResponse(items[53].asTextItem().createResponse(answers[i][44]))
   .withItemResponse(items[54].asParagraphTextItem().createResponse(answers[i][45]))

   .withItemResponse(items[55].asTextItem().createResponse(answers[i][47]))
   .withItemResponse(items[56].asParagraphTextItem().createResponse(answers[i][48]))
   .withItemResponse(items[57].asParagraphTextItem().createResponse(answers[i][49]))
   .withItemResponse(items[58].asTextItem().createResponse(answers[i][50]))
   .withItemResponse(items[59].asTextItem().createResponse(answers[i][51]))
   .withItemResponse(items[60].asTextItem().createResponse(answers[i][52])) 
   .submit();

}

我按照鲁道夫的说法做了。谢谢您的帮助。如果有人知道添加更多的回复,我会非常感激。