变量未定义,导致“找不到具有给定ID的项目...”

时间:2019-03-08 20:25:24

标签: arrays loops google-apps-script directory

**更新:附加了示例Google表格。 https://docs.google.com/spreadsheets/d/196XWcynpe00rLpNRUR2wbq09KKLptEQfZAoMunKBMtw/edit#gid=1457586267

目标是: 教师1文件夹->期间1天1文件夹->学生1文件夹 老师2文件夹->期间3天2文件夹->学生一个文件夹

我已经在这段代码上工作了几周了,它一直给我同样的错误。我正在尝试为有两名不同老师的一名学生创建两个不同的文件夹。第一个完美地创建在正确的位置,但是第二个不断崩溃。它说我的变量:resourcePeriodFolder2和secondResourceTeacherName是未定义的,这导致最后一个变量:resourceFolderID2也未定义。

任何帮助将不胜感激!

非常感谢!

function createOneFolder() {

var studentFolderList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Student Folders");
var teacherList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Teacher Name and Folder List");

// Open the sheet used
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Student Folders");

var rowId = 3;
var rowIdTeacher = teacherList.getLastRow();

var studentFolderListArray = studentFolderList.getRange("A2:J").getValues();
var teacherFolderListArray = teacherList.getRange("A2:L").getValues();

var teacherName = teacherList.getRange("A" + rowId).getValues();

// get teacher name from student list
var firstStudentPeriod = studentFolderList.getRange("E" + rowId).getValues();
var firstTeacherNameOnStudentList = studentFolderList.getRange("F" + rowId).getValues();

// run loop to get teacher name from list  
for (var k=1; k<=rowIdTeacher; k++) {
  if (firstTeacherNameOnStudentList == teacherFolderListArray[k][0]) {
    var firstResourceTeacherName = teacherFolderListArray[k][0];     
    var resourcePeriodFolder1 = teacherFolderListArray[k][7]; 
  }; 

break;
}

// create folder for one student.
var resourceFolder = sheet.getRange("C" + rowId).getValue() + ", " + sheet.getRange("B" + rowId).getValue() + " ..... grade " + sheet.getRange("D" + rowId).getValue(); 
var resourceFolderID = DriveApp.getFolderById(resourcePeriodFolder1).createFolder(resourceFolder).getId();
sheet.getRange("G" + rowId).setValue(resourceFolderID);   

// get second teacher name from student list
var secondTeacherNameOnStudentList = studentFolderList.getRange("I" + rowId).getValues();
var secondStudentPeriod = studentFolderList.getRange("H" + rowId).getValues();

for (var h=1; h<=rowIdTeacher; h++) {  // Problem somewhere in here.
  if (secondTeacherNameOnStudentList === teacherFolderListArray[h][0]) {
    Logger.log('It works!  Victory!!!!')
    var resourcePeriodFolder2 = teacherFolderListArray[h][9]; // *****undefined even though value is in spreadsheet*****
    var secondResourceTeacherName = teacherFolderListArray[h][0]; // ***undefined even though value is in spreadsheet***           
  };

break;   
}

// create a second resource folder for same student.

var resourceFolder2 = sheet.getRange("C" + rowId).getValue() + ", " + sheet.getRange("B" + rowId).getValue() + " ..... grade " + sheet.getRange("D" + rowId).getValue(); 
var resourceFolderID2 = DriveApp.getFolderById(resourcePeriodFolder2).createFolder(resourceFolder2).getId();  **//CRASHES HERE**
sheet.getRange("J" + rowId).setValue(resourceFolderID2);

}

1 个答案:

答案 0 :(得分:0)

也许一个示例电子表格会有所帮助,但引用不是应该从学生到具有老师姓名的单元,而不是创建一个循环以查找任何内容吗? 您应该已经知道数据存在于何处并可以直接引用它,或者此电子表格是否在不断变化?