A student self-checking, self-grading assignment provides students with immediate feedback to encourage additional effort and accuracy. When the Google Slides Presentation is copied (through Google Classroom or Drive), the Apps Script attached to the presentation is copied along with it. This is a three part project. This part explains the script that compares student responses with answer key answers and scores the assignment accordingly.
function checkResponses() {
var presentation = SlidesApp.getActivePresentation();
var slides = presentation.getSlides();
if (slides[0].getTables().length > 0) {
for (let i = 0; i < slides[0].getTables().length; i++) {
if (slides[0].getTables()[i].getDescription() == "ak") {
var answerKey = slides[0].getTables()[i];
break;
}
}
}
if(answerKey){
var lastRow = answerKey.getNumRows();
var answers = [];
var responses = [];
var incorrect = [];
var possible = 0;
var correct = 0;
var vSlack = 10;
var hSlack = 10;
for (let j = 0; j < lastRow; j++) {
let answer = new Answer(answerKey.getCell(j, 0).getText().asString(), answerKey.getCell(j, 1).getText().asString().trim(), parseInt(answerKey.getCell(j, 2).getText().asString()), parseInt(answerKey.getCell(j, 3).getText().asString()));
answers.push(answer);
possible++;
}
slides.forEach(function getBoxes(slide, index) {
slideNumber = index;
slide.getPageElements().forEach(function getContent(shape) {
if (shape.getPageElementType() == SlidesApp.PageElementType.SHAPE && shape.asShape().getTitle() == "response") {
shape.asShape().getFill().setSolidFill(200, 220, 255);
let response = new Answer(shape.asShape().getDescription(), shape.asShape().getText().asString().toLowerCase().replace(/ /g, "").trim(), 0, 0);
responses[response.index] = response;
} else if (shape.getPageElementType() == SlidesApp.PageElementType.SHAPE && shape.asShape().getTitle() == "position") {
shape.asShape().getFill().setSolidFill(200, 220, 255);
let response = new Answer(shape.asShape().getDescription(), 0, parseInt(shape.asShape().getLeft()), parseInt(shape.asShape().getTop()));
responses[response.index] = response;
}
});
});
answers.forEach(function (aKanswer, index) {
if (aKanswer.text != 0) {
let answerArray = aKanswer.text.split(";");
let checkCorrect = 0;
for (let i = 0; i < answerArray.length; i++) {
if (responses[index] && responses[index].text.indexOf(answerArray[i]) != -1) {
checkCorrect++;
i = answerArray.length;
}
}
if (checkCorrect > 0) {
correct++;
} else {
incorrect.push(index);
}
} else {
if (responses[index] && responses[index].left > aKanswer.left - hSlack && responses[index].left < aKanswer.left + hSlack && responses[index].top > aKanswer.top - vSlack && responses[index].top < aKanswer.top + vSlack) {
correct++;
} else {
incorrect.push(index);
}
}
});
incorrect.forEach(function (slideDescription) {
slides.forEach(function (slide) {
slide.getPageElements().forEach(function (element) {
if (element.getPageElementType() == SlidesApp.PageElementType.SHAPE) {
if (parseFloat(element.asShape().getDescription()) === slideDescription) {
element.asShape().getFill().setSolidFill(255, 190, 0);
}
}
});
});
});
var percentCorrect = correct / possible;
var scoreBox = slides[0].insertTextBox("approximate score: " + (Math.round(percentCorrect * 100)) + "%", 10, 10, 200, 80);
score = scoreBox.getObjectId();
scoreBox.getFill().setSolidFill(255, 250, 240, 0.8);
scoreBox.getBorder().setWeight(4).getLineFill().setSolidFill(140, 100, 70);
scoreBox.getText().getTextStyle().setFontSize(24).setBold(true);
scoreBox.getText().getTextStyle().setForegroundColor(70, 50, 0);
}else{
SlidesApp.getUi().alert("No answer key found.");
}
}