Import google spreadsheet data into google forms with app script

后端 未结 3 1874
忘掉有多难
忘掉有多难 2021-02-03 10:50

I searched the internet and I can\'t find a response to this nor the documentation for it. I need to dynamically generate Google forms questions with data from a Google spreadsh

相关标签:
3条回答
  • 2021-02-03 10:59

    In your spreadsheet select Tools > Script Editor and adapt this to your needs:

    /**
       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);
    
    };
    

    To debug, select the script in the combobox and click either "play" or "debug". The first time you will have to give it permissions to interact with your spreadsheet and form.

    Once you are satisfied with the result, in the editor select Resources > Triggers for the active project and add this method to be triggered with any modification on the spreadsheet (on change, not on edit).

    After this, your form options will be changed in real time after any change in your spreadsheet.

    0 讨论(0)
  • 2021-02-03 11:01

    It's pretty straightforward, see here: https://developers.google.com/apps-script/guides/sheets#reading

    You just need to open the sheet by its doc key, select the data and read the cells as a JS object.

    0 讨论(0)
  • 2021-02-03 11:09

    Here is an example which works for me, pls kindly check:

    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');
      });
    }

    You can get your sheet Id from url, for example:

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

    Let me know if you have any further questions.

    0 讨论(0)
提交回复
热议问题