Google Forms + App Script. Script will not validate answers for GRID

I have a script that runs on Google Forms. It is to look for multiple choice grids and transform them to multiple choice questions if rows or columns are > 3.

The form is set to one answer per row and an answer key is set.

Upon running the script, I get both “An error occurred: gridItem.getValidation is not a function” and “An error occurred: Invalid data updating form.”

I want to script to transform grid items to multiple choice, retrieve correct answers from the answer key, limit choices to 3 (1 of 3 should be correct answer), and limit to 3 choices.

Logs:
Jun 18, 2024, 10:50:15 AM Info Error updating grid item: gridItem.getValidation is not a function
Jun 18, 2024, 10:50:15 AM Info Error: gridItem.getValidation is not a function

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> // Handle GRID items
try {
var gridItem = item.asGridItem();
// Get rows and columns from the grid item
var rows = gridItem.getRows();
var columns = gridItem.getColumns();
// Check if the grid item has 3 or fewer rows or columns
if (rows.length <= 3 || columns.length <= 3) {
continue; // Skip this grid item if it has 3 or fewer rows or columns
}
// Iterate over each column in the grid
for (var c = 0; c < columns.length; c++) {
// Create a new MultipleChoiceItem for each column
var newMultipleChoiceItem = modifiedForm.addMultipleChoiceItem();
newMultipleChoiceItem.setTitle(columns[c]);
// Prepare choices based on the grid options for the current column
var choices = [];
for (var r = 0; r < rows.length; r++) {
var answerText = gridItem.getValidation().getValidationForCell(r, c).getHelpText(); // Assuming the answer is stored in the help text
var choiceText = rows[r] + ' - ' + answerText;
var isCorrect = answerText === 'correct'; // Adjust as per your validation logic
choices.push(newMultipleChoiceItem.createChoice(choiceText, isCorrect));
}
// Shuffle the choices
choices = shuffleArray(choices);
// Limit to 3 choices
choices = choices.slice(0, 3);
// Log choices before setting
Logger.log('New multiple-choice item choices: ' + JSON.stringify(choices));
// Set choices for the new MultipleChoiceItem
newMultipleChoiceItem.setChoices(choices);
Logger.log('New multiple-choice item created with choices: ' + JSON.stringify(choices));
}
// Remove the original GridItem from the form
modifiedForm.deleteItem(item);
} catch (e) {
Logger.log('Error updating grid item: ' + e.message);
throw e;
}
}
}`
```
I want to script to transform grid items to multiple choice, retrieve correct answers from the answer key, limit choices to 3 (1 of 3 should be correct answer), and limit to 3 choices.
Adequate multiple choice questions are populating with 3 answer choices, but 1 of the 3 choices is usually not the correct answer.
</code>
<code> // Handle GRID items try { var gridItem = item.asGridItem(); // Get rows and columns from the grid item var rows = gridItem.getRows(); var columns = gridItem.getColumns(); // Check if the grid item has 3 or fewer rows or columns if (rows.length <= 3 || columns.length <= 3) { continue; // Skip this grid item if it has 3 or fewer rows or columns } // Iterate over each column in the grid for (var c = 0; c < columns.length; c++) { // Create a new MultipleChoiceItem for each column var newMultipleChoiceItem = modifiedForm.addMultipleChoiceItem(); newMultipleChoiceItem.setTitle(columns[c]); // Prepare choices based on the grid options for the current column var choices = []; for (var r = 0; r < rows.length; r++) { var answerText = gridItem.getValidation().getValidationForCell(r, c).getHelpText(); // Assuming the answer is stored in the help text var choiceText = rows[r] + ' - ' + answerText; var isCorrect = answerText === 'correct'; // Adjust as per your validation logic choices.push(newMultipleChoiceItem.createChoice(choiceText, isCorrect)); } // Shuffle the choices choices = shuffleArray(choices); // Limit to 3 choices choices = choices.slice(0, 3); // Log choices before setting Logger.log('New multiple-choice item choices: ' + JSON.stringify(choices)); // Set choices for the new MultipleChoiceItem newMultipleChoiceItem.setChoices(choices); Logger.log('New multiple-choice item created with choices: ' + JSON.stringify(choices)); } // Remove the original GridItem from the form modifiedForm.deleteItem(item); } catch (e) { Logger.log('Error updating grid item: ' + e.message); throw e; } } }` ``` I want to script to transform grid items to multiple choice, retrieve correct answers from the answer key, limit choices to 3 (1 of 3 should be correct answer), and limit to 3 choices. Adequate multiple choice questions are populating with 3 answer choices, but 1 of the 3 choices is usually not the correct answer. </code>
        // Handle GRID items
        try {
          var gridItem = item.asGridItem();
          
          // Get rows and columns from the grid item
          var rows = gridItem.getRows();
          var columns = gridItem.getColumns();
          
          // Check if the grid item has 3 or fewer rows or columns
          if (rows.length <= 3 || columns.length <= 3) {
            continue; // Skip this grid item if it has 3 or fewer rows or columns
          }
          
          // Iterate over each column in the grid
          for (var c = 0; c < columns.length; c++) {
            // Create a new MultipleChoiceItem for each column
            var newMultipleChoiceItem = modifiedForm.addMultipleChoiceItem();
            newMultipleChoiceItem.setTitle(columns[c]);
            
            // Prepare choices based on the grid options for the current column
            var choices = [];
            
            for (var r = 0; r < rows.length; r++) {
              var answerText = gridItem.getValidation().getValidationForCell(r, c).getHelpText(); // Assuming the answer is stored in the help text
              var choiceText = rows[r] + ' - ' + answerText;
              var isCorrect = answerText === 'correct'; // Adjust as per your validation logic
              choices.push(newMultipleChoiceItem.createChoice(choiceText, isCorrect));
            }
            
            // Shuffle the choices
            choices = shuffleArray(choices);
            
            // Limit to 3 choices
            choices = choices.slice(0, 3);
            
            // Log choices before setting
            Logger.log('New multiple-choice item choices: ' + JSON.stringify(choices));
            
            // Set choices for the new MultipleChoiceItem
            newMultipleChoiceItem.setChoices(choices);
            Logger.log('New multiple-choice item created with choices: ' + JSON.stringify(choices));
          }
          
          // Remove the original GridItem from the form
          modifiedForm.deleteItem(item);
        } catch (e) {
          Logger.log('Error updating grid item: ' + e.message);
          throw e;
        }
      }
    }`
```


I want to script to transform grid items to multiple choice, retrieve correct answers from the answer key, limit choices to 3 (1 of 3 should be correct answer), and limit to 3 choices. 

Adequate multiple choice questions are populating with 3 answer choices, but 1 of the 3 choices is usually not the correct answer.

New contributor

Sam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật