2013-10-21 47 views
1

如何从Google电子表格中将信息重新导入Google表单以更新信息?将Google Spreadsheets信息转换为Google表单

我希望能够将我已经通过Google表单提交的信息重新提交到表单中以更新信息。我知道我可以去编辑电子表格,但为了我的目的,通过表单可以更容易地完成。我一直找不到能让我做到这一点的脚本。有没有人有什么建议?

回答

3

您可以在Apps脚本中使用FormApp访问表单对象进行更新。

下面是一个更新“从列表中选择”对象的示例。如果您想在每次提交后更新表单,则可以使用基于容器的触发器“On form submit”调用updateList函数。

function updateList(){ 
    var form = FormApp.openById(formId); 
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); 
    // get the range of values to update in the form 
    var data = sheet.getRange(2,1,sheet.getLastRow(),sheet.getLastColumn()).getValues(); 

    var listItemsToAdd = []; 
    for(s in data){ 
    if(logic to define if to add to the form){ 
     listItemsToAdd.push(data[s][0]); 
    } 
    } 

    // get all the list items in the form 
    var list = form.getItems(FormApp.ItemType.LIST); 
    // grab the first list item in the array of the form's list items 
    var item = list[0].asListItem(); 
    // set the options to the array just created 
    item.setChoiceValues(listItemsToAdd); 
} 
+1

但请不要使用'for(s in data)'循环一个Array。 –

+1

@Cyrus Loree:好的,但我想问你一个关于这个问题的问题;如何使用导航类型在for循环中设置值? ([像这个例子](https://developers.google.com/apps-script/reference/forms/choice)) –

+0

我认为这是一个不同的对话。如果您发布了一个新问题,我会很乐意详细介绍。简短的答案是您传递分页符项而不是PageNavigationType枚举。例如。 var anotherPage = form.addPageBreakItem()。setTitle('Another page to go to'); ... item.createChoice('My New Page',anotherPage),item.createChoice('Dogs',FormApp.PageNavigationType.RESTART) ]); –

相关问题