且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Google表单下拉列表中删除选定的项目

更新时间:2023-02-11 19:42:57

我试图完成完全相同的操作(列出要选择的产品),但是我无法使其与您的最终代码示例一起使用.这是我的,带有详细说明.仅适用于登陆此页面并正在寻找有效代码的任何人.

I tried to accomplish exactly the same (list with products to select from), but I couldn't make it work with your final code example. Here's mine, with detailed instructions. Just for anybody who is landing on this page and is looking for a working code.

(菜单名称可能与您的菜单名称有所不同,因为我使用的是非英语的Google表单,而我只是在这里猜测翻译的意思.)

(Menu names might differ from yours, because I'm using a non-English Google Forms, and I'm just guessing the translations here.)

1)创建一个新表单,并创建一个新的基于单选按钮的问题(多项选择)(在本示例中,我使用问题名称:选择产品").不要添加任何选项.保存.

1) Create a new form, and create a new radio button based question (multiple choice) (In this example, I use the question name: "Select a product"). Don't add any options to it. Save it.

2)打开要在其中存储响应的电子表格,并在其中添加新的工作表(名称:库存")

2) Open the spreadsheet where the responses are going to be stored, and add a new sheet in it (name: "inventory")

3)修复清单的第一行(A),并在A1中输入选择产品"

3) Fix the first row (A) of the inventory sheet, and put in A1: "Select a product"

4)在A列中输入要显示在表单中的所有产品

4) Put in column A all the products you want to appear in the form

5)再次打开表单编辑器,然后转到工具>脚本编辑器

5) Open the form editor again, and go to tools > script editor

6)将此代码粘贴到编辑器中,放入表单和电子表格ID的(3x)并保存.

6) Paste this code in the editor, put in your form and spreadsheet ID's (3x) and save it.

var LIST_DATA = [{title:"Select a product", sheet:"inventory"}];

function updateLists() {
  //var form = FormApp.getActiveForm();
  var form = FormApp.openById("paste_ID_of_your_FORM_here");
  var items = form.getItems();
  for (var i = 0; i < items.length; i += 1){
    for (var j = 0; j < LIST_DATA.length; j+=1) {
      var item = items[i];

      if (item.getTitle() === LIST_DATA[0].title){
        updateListChoices(item.asMultipleChoiceItem(), LIST_DATA[0].sheet);
        break;
    }
    }
  }
}

function updateListChoices(item, sheetName){
  var inventory = (SpreadsheetApp.openById("paste_ID_of_your_RESPONSE_SHEET_here")
              .getSheetByName("inventory")
              .getDataRange()
              .getValues());
  var selected = (SpreadsheetApp.openById("paste_ID_of_your_RESPONSE_SHEET_here")
              .getSheetByName("responses")
              .getDataRange()
              .getValues());

  var choices = [];
  var selectedReal = [];
  for (var i = 0; i< selected.length; i+=1){
     selectedReal.push(selected[i][1]) 
  }
  for (var i = 1; i< inventory.length; i+=1){
    if(selectedReal.indexOf(inventory[i][0])=== -1){
      choices.push(item.createChoice(inventory[i][0]));}
  }
  if (choices.length < 1) {
    var form = FormApp.getActiveForm();
    form.setAcceptingResponses(false); 
  } else {
  item.setChoices(choices); 
  }
}

7)在代码编辑器打开的情况下,转到资源">创建触发器"并创建这两个触发器.它们需要按以下顺序出现:

7) With the code editor open, go to Resources > Create triggers and create these two triggers. They need to appear in this order:

  1. updateLists-从表单-发送
  2. updateLists-从表单-打开

现在你很好.如果打开表单编辑器,则库存表中添加的产品将作为选项出现.

Now you're good to go. If you open the form editor, the products added in the inventory sheet will appear as options.

每次选择产品时,它都会从表格中消失.要重置所有选择的产品,请转到表单编辑器,然后选择响应">删除所有响应".您可能还需要手动从响应"表中删除所有响应(不知道为什么,但这发生在我身上).之后,您需要在代码编辑器中手动运行updateLists脚本.

Every time a product is chosen, it will disappear from the form. To reset all the chosen products, go to the form editor, and choose Responses > Remove all responses. You might need to remove all responses from the responses sheet manually as well (Don't know why, but that happened to me). After that, you need to manually run the updateLists script in the code editor.