【问题标题】:Import google spreadsheet data into google forms with app script使用应用脚本将谷歌电子表格数据导入谷歌表单
【发布时间】:2014-01-02 17:16:57
【问题描述】:

我搜索了互联网,但找不到对此的回复或相关文档。 我需要使用应用脚本使用 Google 电子表格中的数据动态生成 Google 表单问题,但我不知道如何引用和阅读电子表格。

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-sheets-api google-forms


    【解决方案1】:

    在您的电子表格中选择 Tools > Script Editor 并根据您的需要进行调整:

    /**
       After any change in the sheet, update the combobox options in the Form
    */
    function onChange(e) {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
      var range = sheet.getDataRange();
      var values = range.getValues();
      var comboValues = [];  // <-- cheddar will go here
    
      // in this example we are interested in column 0 and discarding row 1 (the titles)
      for (var i = 1; i <= values.length; i++) {
        var v = values[i] && values[i][0];
        v && comboValues.push(v)
      }
    
      // Sort the values alphabetically, case-insensitive
      comboValues.sort(
        function(a, b) {
          if (a.toLowerCase() < b.toLowerCase()) return -1;
          if (a.toLowerCase() > b.toLowerCase()) return 1;
          return 0;
        }
      );
      Logger.log(comboValues);
    
      // Use your form ID here. You can get it from the URL
      var form = FormApp.openById('<my-form-id>');
    
      /* 
        Uncomment this to display the item IDs
        and pick the one that you want to modify
    
      var items = form.getItems();
      for (i = 0; i < items.length; i++) {
        Logger.log("ID: " + items[i].getId(), ': ' + items[i].getType());
      }
      */
      form.getItemById(807137578).asListItem().setChoiceValues(comboValues);
    
    };
    

    要调试,请在组合框中选择脚本,然后单击“播放”或“调试”。第一次您必须授予它与电子表格和表单交互的权限。

    一旦您对结果感到满意,请在编辑器中选择 Resources &gt; Triggers for the active project 并添加此方法,以便在对电子表格进行任何修改时触发(更改时,而不是编辑时)。

    在此之后,您的表单选项将在您的电子表格发生任何更改后实时更改。

    【讨论】:

    • 非常有用,谢谢!我要补充一点,当您第一次打开脚本编辑器时,您可以选择多种项目类型。在这种情况下,需要“空白项目”。
    【解决方案2】:

    这很简单,请看这里:https://developers.google.com/apps-script/guides/sheets#reading

    您只需要通过其 doc 键打开工作表,选择数据并将单元格作为 JS 对象读取即可。

    【讨论】:

      【解决方案3】:

      这是一个适合我的例子,请检查:

      function getSpreadsheetData(sheetId) {
        // This function gives you an array of objects modeling a worksheet's tabular data, where the first items — column headers — become the property names.
        var arrayOfArrays = SpreadsheetApp.openById(sheetId).getDataRange().getValues();
        var headers = arrayOfArrays.shift();
        return arrayOfArrays.map(function (row) {
          return row.reduce(function (memo, value, index) {
            if (value) {
              memo[headers[index]] = value;
            }
            return memo;
          }, {});
        });
      }
      
      function makeOurForm() {
      
        var sheetId='input_your_sheet_id'
      
        getSpreadsheetData(sheetId).forEach(function (row) {
      // Set your form template as follows
        var formName=row.Name
      // Create your form programmatically, each row means one form
        var form = FormApp.create(formName)
      
        form.setDescription('xxx');
      
        var capitalizedName = row.Name.charAt(0).toUpperCase() + row.Name.slice(1);
      
        form.addSectionHeaderItem().setTitle(capitalizedName);
      
        var item = form.addMultipleChoiceItem();
        item.setTitle('xxx')
            .setChoices([
              item.createChoice('xxx'),
            ]);
      
        form.addParagraphTextItem().setTitle('xxx');
        });
      }

      您可以从 url 中获取您的工作表 ID,例如:

      https://docs.google.com/spreadsheets/d/YourSheetId/edit#gid=0

      如果您还有其他问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 2012-06-15
        相关资源
        最近更新 更多