【问题标题】:Using Apps Script to create multiple Forms from a Spreadsheet使用 Apps 脚本从电子表格创建多个表单
【发布时间】:2021-09-01 22:30:24
【问题描述】:

我想使用电子表格中的列表为不同的课程创建多个表单。我有为每个类构建一个新表单的简单代码,但我想为每个新表单添加一些表单元素。我知道如何使用脚本中想要的元素构建单个表单,并且知道如何使用脚本从表单中提取表单名称来构建一堆空表单。但我正在努力使用该脚本 forEach 部分的语法来将表单项添加到每个新表单。

function createForms() {

  const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet5');
  const range = ss.getDataRange().getValues();

  range.forEach(item => FormApp.create(item));

这段代码为工作表 5 中的每个项目创建了一个新表单。现在,当它遍历这些新表单时,我想为每个表单添加一些项目。我不确定如何让 forEach 语句遍历表单并向每个表单添加项目。

我们将不胜感激。

我已经试过了,但它不起作用。

function createForms() {

  const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet5');
  const range = ss.getDataRange().getValues();
  
  range.forEach(item => FormApp.create(item))
    
    const form = FormApp.create(item);
    form.addTextItem()
    .setTitle('Name')

}

收到答复后(谢谢),我修改了脚本,它正在创建表格,如电子表格中的列表所示。最后,我希望在父文件夹中创建表单,并为每个表单创建一个新文件夹。

function createForms() {
  
  //use this to force email text box to be email address
  const emailValidation = FormApp.createTextValidation()
  .setHelpText('Input must be email address.')
  .requireTextIsEmail()
  .build();

  const parentFolder = DriveApp.getFolderById('PUT YOUR FOLDER ID HERE');

  let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('courses');
  let values = ss.getDataRange().getValues();
  for (let i = 0; i < values.length; i++) {
      let form = FormApp.create(values[i]);
      form.addTextItem().setTitle("Name").setHelpText('Please enter your full name').setRequired(true);
      form.addTextItem().setTitle("Email Address").setRequired(true).setValidation(emailValidation).setHelpText('An email address is required');
      form.addParagraphTextItem().setTitle('Details').setHelpText('Please provide the details you need to explain').setRequired(true);
      form.addMultipleChoiceItem().setTitle("Day Choice").setHelpText('Choose the best day for you').setRequired(true)
      .setChoiceValues(['Monday', 'Tuesday','Wednesday','Thursday','Friday']);
  }
}

【问题讨论】:

  • 不是一个直接的答案,因为我只为每个电子表格创建一个表单,但我的代码中可能还有其他有用的想法。 docs.google.com/spreadsheets/d/… 复制一份。我仍在努力,会接受建议或更正。

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


【解决方案1】:

这里的主要问题在于您检索表单名称列表的方式。

getDataRange 方法将返回与存在数据的维度相对应的范围。然而,getValues 最终会返回一个[][] 类型的二维数组。

虽然forEach 的语法是正确的,但您实际上只是以一维方式遍历数组。

同样重要的是要注意,在向表单添加项目时,您必须声明您希望表单元素成为的类型的项目。

因此,假设电子表格如下所示,并且您希望每个表单包含 3 个元素,脚本应如下所示:

电子表格

脚本

function createForms() {
  let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet5');
  let values = ss.getDataRange().getValues();
  for (let i = 0; i < values.length; i++) {
    for (let j = 0; j < values[i].length; j++) {
      let form = FormApp.create(values[i][j]);
      form.addTextItem().setTitle("Text Item " + values[i][j]);
      form.addGridItem().setTitle("Grid Item " + values[i][j]);
      form.addMultipleChoiceItem().setTitle("Multiple Choice " + values[i][j]);
    }
  }
}

注意

如果您对forEach 的工作原理有疑问,我建议您先使用简单的for 循环。

但是,您可以在上面的代码中轻松地从 for 切换到 forEach

参考

【讨论】:

  • 这正是我想要的,我不确定我们为什么要添加“let j”。我已经删除了它,它正在按照我喜欢的方式工作,因为我在工作表中只有一列课程名称,我想为其创建不同的表单。我要添加的最后一件事是能够通过 id 而不是驱动器的根目录在单个文件夹中创建这些表单,或者更好的是,在包含所有课程的文件夹中,为每个新课程添加一个新文件夹和将新表格放在它自己的课程文件夹中。
【解决方案2】:

在 ale13 的帮助下,我已经能够弄清楚我想要做什么。这是最终的脚本。它将为您指定的工作表中的每一行数据创建一个新表单,即父文件夹中的一个新文件夹。它专为单列名称而设计,为每个类或组创建一个单独的表单,并将它们组织到父文件夹中的新文件夹中。可以在脚本中修改表单类型和问题。

    /* This script will create a new form for each line in column A of the sheet by name */

function createForms() {
  
  //use this to force email text box to be email address
  const emailValidation = FormApp.createTextValidation()
  .setHelpText('Input must be email address.')
  .requireTextIsEmail()
  .build();

//we will want to save these new forms into a parent google drive folder
//next we will want to create a new folder for each course in this parent folder
// and save the new forms in these child folders with the name of the course

  const parentFolder = DriveApp.getFolderById('PUT YOUR FOLDER ID HERE');

  let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PUT YOUR SHEET NAME HERE');
  let values = ss.getDataRange().getValues();
  for (let i = 0; i < values.length; i++) {
      let folder = parentFolder.createFolder(values[i]);
      let form = FormApp.create(values[i]);
      form.addTextItem().setTitle("Name").setHelpText('Please enter your full name').setRequired(true);
      form.addTextItem().setTitle("Email Address").setRequired(true).setValidation(emailValidation).setHelpText('An email address is required');
      form.addParagraphTextItem().setTitle('Details').setHelpText('Please provide the details you need to explain').setRequired(true);
      form.addMultipleChoiceItem().setTitle("Day Choice").setHelpText('Choose the best day for you').setRequired(true)
      .setChoiceValues(['Monday', 'Tuesday','Wednesday','Thursday','Friday']);
      form.addListItem().setTitle('Rooms').setHelpText('Select your room')
      .setChoiceValues(['Room A','Room B','Room C']);
      let formFile = DriveApp.getFileById(form.getId());
      formFile.moveTo(folder);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多