Google电子表格到Google表单 – 创建答案键时出错

huangapple go评论75阅读模式
英文:

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);
  });
}
  1. Two Questions

    Google电子表格到Google表单 – 创建答案键时出错

  2. Result (2- perfect)

    Google电子表格到Google表单 – 创建答案键时出错

  3. Three Questions

    Google电子表格到Google表单 – 创建答案键时出错

  4. Result (2- That's weird)

    Google电子表格到Google表单 – 创建答案键时出错

  5. Four Questions

    Google电子表格到Google表单 – 创建答案键时出错

  6. Result (2-That's weird)

    Google电子表格到Google表单 – 创建答案键时出错

  7. The previous answer has made the work extremely fast.
    Finally, when creating many problems, an error occurs and the question is posted again.

  8. 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.

  9. 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:

huangapple
  • 本文由 发表于 2023年7月10日 21:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定