问题
I want to make a script that will add a lot of(about 1500) CV and candidate info as a response to a form. I have the information as a google spreadsheet. I linked it to form but i can only manage responses. Even if i change the spreadsheet(or add), no update will happen in the form. I am adding lines but no responses added. Is that possible? I saw 2-3 questions about that but they were old and i am still trying to get answers. This is my internship project and i have not much time left. Thanks in advance.
回答1:
For example you can upload your answers to a spreadsheet, get them in array and submit through FormResponce object (for that you need to use Form#createResponse ).
I didn't test this code, but as far as I understand something like this should work for you.
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();
}
}
Just be careful, some items require Integer as a response while others expect String etc.
回答2:
Thanks to you guys, i could add an item as response. It is the great start for me! Here is the code i have written:
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();
}
I did as Rudolf says. Thanks for the help. If anyone knows about adding more that one responses, i'd be very grateful for that.
来源:https://stackoverflow.com/questions/45049484/adding-a-lot-of-responses-with-google-apps-script