设置Google Forms测验分数时出错,寻求修改响应分数方案
Fixing Quiz Score Update Errors in Google Apps Script for Google Forms
Let's break down the issues in your code and walk through a complete, working solution to update quiz scores for specific responses:
Key Issues in Your Original Code
- Inconsistent Form References: You opened the form with
FormApp.openById()but then usedgetActiveForm()for subsequent calls. This can cause errors if the active form in your session isn't the one you're targeting—stick to theformvariable you initialized. - Incomplete Logic: Your code cuts off before retrieving item responses or implementing the score-setting logic, which is why you're seeing errors when trying to update grades.
- Missing Grade Submission: Even if you set a score, you need to explicitly submit the grade updates to save changes to the form.
Working Solution Code
Here's a revised script that targets a specific multiple-choice question, maps responses to scores, and updates each response's grade correctly:
function updateQuizResponseScores() { // Replace with your actual Form ID const targetFormId = 'formID123456'; const form = FormApp.openById(targetFormId); // Define your score mapping: option text → points const responseToScore = { "1": 10, "2": 20, "3": 30, "4": 40 }; // Get all responses from the form const allResponses = form.getResponses(); // Loop through each response to update grades allResponses.forEach(response => { const itemResponses = response.getItemResponses(); // Find the target question (adjust the condition to match your question) itemResponses.forEach(itemResponse => { const question = itemResponse.getItem(); // Match by question title OR ID for precision const isTargetQuestion = question.getType() === FormApp.ItemType.MULTIPLE_CHOICE && question.getTitle() === "Your Target Question Title"; if (isTargetQuestion) { const userAnswer = itemResponse.getResponse(); // Get the corresponding score (default to 0 if no match) const assignedScore = responseToScore[userAnswer] || 0; // Create a grade update object for this question const gradeUpdate = FormApp.createItemGradeUpdate() .setItemId(question.getId()) .setScore(assignedScore); // Submit the grade update to save changes response.withItemGradeUpdates([gradeUpdate]).submitGrades(); } }); }); }
Critical Notes for Success
- Ensure Quiz Mode is Enabled: Your Google Form must be set to Quiz mode (under Settings → Make this a quiz) to allow score modifications.
- Precision in Question Targeting: Using
question.getTitle()is straightforward, but for unchanging references, usequestion.getId()(you can get the ID viaform.getItems()if needed). - Permissions: The account running the script needs edit access to the form.
- Large Response Sets: If you have hundreds of responses, add batch processing or a time check to avoid hitting Google Apps Script's 6-minute execution limit.
内容的提问来源于stack exchange,提问作者Matthew Eng




