由于没有定义变量,Google脚本会自动使用条件逻辑填充表单(即使它在我看来是定义的)

问题描述 投票:0回答:1

下面是由Jordan Jordan编写并通过github提供的脚本。

我正在尝试遵循其逻辑,以便我可以理解如何自动处理带有条件逻辑的问题,这些问题具有条件逻辑流向Google表单的不同部分。

代码在第35行“ ReferenceError:未定义表单(第35行,文件“ RosterMaker””)处中断]

Line 35 = var classSection = form.addPageBreakItem().setTitle(className).setGoToPage(FormApp.PageNavigationType.SUBMIT);

我确定这是一个简单的错误,但我无法弄清楚。

var ssID = "something";
var formID = "something"
//SpreadsheetApp.openById(ssID).getSheetByName("db_type");

function rosterMaker() {
  //spreadsheet id of the rosters
  var SHEET_ID = SpreadsheetApp.getActive();

  var ss = SpreadsheetApp.openById(ssID);
  var form = FormApp.openById((formID));


  //get only the sheets with 'Roster' in the title
  var sheets = ss.getSheets()
    .filter(function(sheet) {return sheet.getName().match(/Roster/gi);});


  //add multiple choice item
  var classSelect = form.addMultipleChoiceItem()
    .setTitle('Choose a class');
  Logger.log(classSelect);

  //get the class choices for the multiple choice item
  var classChoices = getClasses(sheets);

  //assign the choices to the classSelect variable
  classSelect.setChoices(classChoices);
}

function getClasses(sheets) {
  var classChoices = [];
  for(var i = 0; i < sheets.length; i++) {
    var className = sheets[i].getName();

    var classSection = form.addPageBreakItem()
      .setTitle(className)
      .setGoToPage(FormApp.PageNavigationType.SUBMIT);

    var students = getStudents(sheets[i]);

    var studentSelect = form.addCheckboxItem()
      .setTitle(className + ' absent')
      .setHelpText('Select the students who are absent from this class');

    var studentChoices = [];
    for(var j = 0; j < students.length; j++) {
      studentChoices.push(studentSelect.createChoice(students[j]));
    }

    studentSelect.setChoices(studentChoices);

    classChoices.push(classSelect.createChoice(className, classSection));
  }

  return classChoices;
}

function getStudents(sheet) {
  var studentValues = sheet.getDataRange().getValues();

  var students = [];
  for(var i = 1; i < studentValues.length; i++) {
    students.push(studentValues[i].join(' '));
  }
  return students;
}
google-apps-script google-sheets google-form
1个回答
1
投票

测试此:

var ssID = "something";
var formID = "something";

function rosterMaker() {
  var SHEET_ID = SpreadsheetApp.getActive();
  var ss = SpreadsheetApp.openById(ssID);
  var form = FormApp.openById((formID));
  var sheets = ss.getSheets().filter(function(sheet) {return sheet.getName().match(/Roster/gi);});
  var classSelect = form.addMultipleChoiceItem().setTitle('Choose a class');
  var classChoices = getClasses(sheets,form);//modified
  classSelect.setChoices(classChoices);
}

function getClasses(sheets,form) {//modified
  var classChoices = [];
  for(var i = 0; i < sheets.length; i++) {
    var className = sheets[i].getName();
    var classSection = form.addPageBreakItem().setTitle(className).setGoToPage(FormApp.PageNavigationType.SUBMIT);
    var students = getStudents(sheets[i]);
    var studentSelect = form.addCheckboxItem().setTitle(className + ' absent').setHelpText('Select the students who are absent from this class');
    var studentChoices = [];
    for(var j = 0; j < students.length; j++) {
      studentChoices.push(studentSelect.createChoice(students[j]));
    }
    studentSelect.setChoices(studentChoices);
    classChoices.push(classSelect.createChoice(className, classSection));
  }
  return classChoices;
}

function getStudents(sheet) {
  var studentValues = sheet.getDataRange().getValues();
  var students = [];
  for(var i = 1; i < studentValues.length; i++) {
    students.push(studentValues[i].join(' '));
  }
  return students;
}
© www.soinside.com 2019 - 2024. All rights reserved.