英文:
Google Spreadsheet to Google form - Error when creating answer key
问题
function getDataFromGoogleSheets() {
const correctColor = "#ff0000"; // 请根据您的实际情况设置这个值。
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('test1');
const range = sheet.getDataRange();
const ar = range.getDisplayValues();
const choices = ar[0].map((_, col) => ar.map((row) => row[col])).map(([h, ...v]) => [h, v]);
const correct = range.getTextStyles().reduce((ar, r, i) => {
r.forEach((c, j) => {
if (c.getForegroundColorObject().asRgbColor().asHexString() == correctColor) {
ar.push([i - 1, j]);
}
});
return ar;
}, []);
//Logger.log(correct);
return { choices, correct };
}
function myFunction1() {
const { choices, correct } = getDataFromGoogleSheets();
const form = FormApp.create('新表单');
form.setIsQuiz(true);
choices.forEach(([h, v], j) => {
const q = form.addMultipleChoiceItem().setTitle(h).showOtherOption(true);
const cv = v.map((e, i) => q.createChoice(e, correct[j][0] == i ? true : false));
q.setChoices(cv);
});
}
英文:
function getDataFromGoogleSheets() {
const correctColor = "#ff0000"; // Please set this for your actual situation.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('test1');
const range = sheet.getDataRange();
const ar = range.getDisplayValues();
const choices = ar[0].map((_, col) => ar.map((row) => row[col])).map(([h, ...v]) => [h, v]);
const correct = range.getTextStyles().reduce((ar, r, i) => {
r.forEach((c, j) => {
if (c.getForegroundColorObject().asRgbColor().asHexString() == correctColor) {
ar.push([i - 1, j]);
}
});
return ar;
}, []);
//Logger.log(correct);
return { choices, correct };
}
function myFunction1() {
const { choices, correct } = getDataFromGoogleSheets();
const form = FormApp.create('New Form');
form.setIsQuiz(true);
choices.forEach(([h, v], j) => {
const q = form.addMultipleChoiceItem().setTitle(h).showOtherOption(true);
const cv = v.map((e, i) => q.createChoice(e, correct[j][0] == i ? true : false));
q.setChoices(cv);
});
}
-
Two Questions
-
Result (2- perfect)
-
Three Questions
-
Result (2- That's weird)
-
Four Questions
-
Result (2-That's weird)
-
The previous answer has made the work extremely fast.
Finally, when creating many problems, an error occurs and the question is posted again. -
The errors are as follows.
There are no errors in the two questions.
When creating more than two questions, an error occurs in the correct answer key. -
Thank you to Tanaike for helping with the above code.
答案1
得分: 0
尽管我不确定我是否能正确理解您当前的问题,但以下修改如何?请将您的 getDataFromGoogleSheets()
修改如下。
从:
return { choices, correct };
到:
return { choices, correct: correct.sort((a, b) => a[1] > b[1] ? 1 : -1) };
参考链接:
英文:
Although I'm not sure whether I could correctly understand your current issue, how about the following modification? Please modify your getDataFromGoogleSheets()
as follows.
From:
return { choices, correct };
To:
return { choices, correct: correct.sort((a, b) => a[1] > b[1] ? 1 : -1) };
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论