【发布时间】:2016-09-02 22:38:01
【问题描述】:
我是一名教师,我经常需要抄写空白评分标准,班级中每个学生一份。量规是谷歌表格。我拼凑了一个脚本,它将 (1) 访问另一个包含班级名册的工作表,(2) 从名册中读取学生的姓名,以及 (3) 为每个学生创建一个新的电子表格并将其与学生的姓名一起保存为文件名。所有这些都有效(由于某种原因它非常慢,但它有效)。
现在我正在尝试将学生的姓名写入该学生的量规副本上的单元格中。我的想法是将学生的姓名写到原始评分标准的相关单元格中,然后将其副本保存在学生姓名下。但是那部分不起作用;单元格中的名称与文件名不匹配。相反,单元格中的姓名与先前学生的姓名相匹配。代码如下:
function onerubricperstudent(){
//"roster" is the roster spreadsheet. Change the value in quotation marks to whatever your roster's ID is.
var roster = SpreadsheetApp.openById("1SN2x6bKzga6bRwwvt5diuaETBiXeHqY7bLJMY5If9js");
//"sheet" is sheet to be copied
var sheet = SpreadsheetApp.getActiveSpreadsheet();
//range is the range on the roster sheet containing the student names
var range = roster.getRange('Sheet1!A1:A17');
//do this as many times as there are students in the class
for (var i = 1; i < 18; i++) {
//get the next student name
var studentname = range.getCell(i, 1);
//log the name (for troubleshooting if the script fails)
Logger.log(studentname.getValue());
//Write the student's name to the name cell on the sheet
sheet.getRange('B2').setValue(studentname.getValue());
//Make a copy of the sheet and save it with the student's name
DriveApp.getFileById(sheet.getId()).makeCopy(studentname.getValue());
}
}
没有按预期工作的行是:
sheet.getRange('B2').setValue(studentname.getValue());
我做错了什么?
注意:作为一种解决方法,我添加了这个 OnOpen 脚本,当我打开评分量规为学生的作业评分时,它会将工作表名称复制到学生姓名单元格:
function onOpen() {
var sheetname = SpreadsheetApp.getActive().getName();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.getRange('B2').setValue(sheetname);
}
这完成了工作,但我仍然想知道原始代码有什么问题。
谢谢!
【问题讨论】:
标签: google-apps-script google-sheets