Skip to content

Instantly share code, notes, and snippets.

@gamemaker1
Created November 19, 2022 15:44
Show Gist options
  • Save gamemaker1/7fd49fb3bcc4892649fd21171a35ce6c to your computer and use it in GitHub Desktop.
Save gamemaker1/7fd49fb3bcc4892649fd21171a35ce6c to your computer and use it in GitHub Desktop.
Negative Marking Form Extension
/**
* Negative Marking Forms Extension
* Created by Vedant K <github.com/gamemaker1>
* ISC License • Copyright Vedant K 2022
*
* This extension allows you to add negative marking to a quiz in Google Forms.
*
* The settings are as follows:
* - Single Correct Questions (SCQs) = -1 [upon wrong answer]
* - Multiple Correct Questions (MCQs) = -2 [upon wrong answer, no partial marking]
* - Integer Type Questions = NaN [no negative marking]
*
* It is not possible at the time to change the score of Matrix type questions
* (https://issuetracker.google.com/issues/142531514#comment7), so negative marking
* on those questions will not be done.
**/
const ADDON_TITLE = 'Negative Marking Extension';
const onInstall = onOpen
const onOpen = () => {
const form = FormApp.getActiveForm();
const triggers = ScriptApp.getUserTriggers(form);
// Create a new trigger only if required.
let existingTrigger = undefined;
for (const trigger of triggers) {
if (trigger.getEventType() === ScriptApp.EventType.ON_FORM_SUBMIT) {
existingTrigger = trigger;
break;
}
}
if (!existingTrigger) {
ScriptApp.newTrigger('applyNegativeMarking')
.forForm(form)
.onFormSubmit()
.create();
}
}
const applyNegativeMarking = () => {
// Open the form in question.
const form = FormApp.getActiveForm()
// Retrieve all the questions and responses.
const responses = form.getResponses()
const questions = form.getItems()
// Go through each form response.
for (const response of responses) {
// Go through each question for each response.
for (const question of questions) {
// Fetch the response the user gave for the question in question.
const answer = response.getGradableResponseForItem(question)
if (!answer) continue
// If the response is blank, the answer is correct, or the score has already been
// modified, let the score be as is.
if (!answer.getResponse() ||
(question.getType() !== FormApp.ItemType.CHECKBOX_GRID && answer.getScore() !== 0)
) continue
// Apply negative marking as per the type of question.
if (question.getType() === FormApp.ItemType.MULTIPLE_CHOICE) {
answer.setScore(-1)
response.withItemGrade(answer)
continue
} else if (question.getType() === FormApp.ItemType.CHECKBOX) {
answer.setScore(-2)
response.withItemGrade(answer)
continue
} else if (question.getType() === FormApp.ItemType.TEXT) {
answer.setScore(0)
response.withItemGrade(answer)
continue
} else {}
}
}
// Upload the new scores.
form.submitGrades(responses)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment