13

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

4

3 回答 3

17

在您的电子表格中选择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 > Triggers for the active project并添加此方法,以便在对电子表格进行任何修改时触发(更改时,而不是编辑时)。

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

于 2014-05-30T06:30:45.520 回答
4

这很简单,请参见此处:https ://developers.google.com/apps-script/guides/sheets#reading

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

于 2014-01-03T09:43:42.063 回答
2

这是一个适用于我的示例,请检查:

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

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

于 2017-08-12T15:23:38.757 回答