Google 앱 스크립트를 사용하여 프로그래밍 방식으로 Google 양식에서 답변 (셔플 옵션 순서)을 무작위로 지정하려면 어떻게해야합니까?
점수를 설정하고 양식에 필요한 옵션을 설정하는 방법이 있지만 답변을 섞을 수있는 방법이 있습니다.
내 샘플 코드는 다음과 같습니다.
var quest = SpreadsheetApp.getActiveSheet().getRange(1,4).getValue();
var ans1 = SpreadsheetApp.getActiveSheet().getRange(1,4).getValue();
var ans2 = SpreadsheetApp.getActiveSheet().getRange(1,5).getValue();
var ans3 = SpreadsheetApp.getActiveSheet().getRange(1,6).getValue();
var ans4 = SpreadsheetApp.getActiveSheet().getRange(1,9).getValue();
var item = form.addMultipleChoiceItem();
item.setChoices([
item.createChoice(ans1, false),
item.createChoice(ans2, false),
item.createChoice(ans3, false),
item.createChoice(ans4, true),
]);
item.setTitle(quest)
item.setRequired(true);
item.setPoints(1);
편집 : 구현 된 솔루션
나는 앞서 말한 질문에 대해 아래 논리로 구현했으며 작동했습니다. 다른 사람들에게도 도움이되기를 바랍니다. 아래는 코드 스 니펫입니다.
var quest = SpreadsheetApp.getActiveSheet().getRange(2,3).getValue();
var ans1 = SpreadsheetApp.getActiveSheet().getRange(2,4).getValue();
var ans2 = SpreadsheetApp.getActiveSheet().getRange(2,5).getValue();
var ans3 = SpreadsheetApp.getActiveSheet().getRange(2,6).getValue();
var ans4 = SpreadsheetApp.getActiveSheet().getRange(2,7).getValue();
var correctans = SpreadsheetApp.getActiveSheet().getRange(2,8).getValue();
var item = form.addMultipleChoiceItem();
item.setTitle(quest);
if (ans1 == correctans){
item.setChoices([item.createChoice(ans1, true),item.createChoice(ans2,false),item.createChoice(ans3,false),item.createChoice(ans4,false)]);};
if (ans2 == correctans){
item.setChoices([item.createChoice(ans1, false),item.createChoice(ans2,true),item.createChoice(ans3,false),item.createChoice(ans4,false)]);};
if (ans3 == correctans){
item.setChoices([item.createChoice(ans1, false),item.createChoice(ans2,false),item.createChoice(ans3,true),item.createChoice(ans4,false)]);};
if (ans4 == correctans){
item.setChoices([item.createChoice(ans1, false),item.createChoice(ans2,false),item.createChoice(ans3,false),item.createChoice(ans4,true)]);};
item.setPoints(1);
item.setRequired(true);
options
)를 만들고 가능한 모든 옵션을이 배열에 푸시 합니다.function addRandomizedOptions() {
var sheet = SpreadsheetApp.getActiveSheet();
var quest = sheet.getRange(1,4).getValue();
var options = []
options.push(sheet.getRange(1,4).getValue());
options.push(sheet.getRange(1,5).getValue());
options.push(sheet.getRange(1,6).getValue());
options.push(sheet.getRange(1,9).getValue());
shuffleArray(options);
var item = form.addMultipleChoiceItem();
item.setChoices([
item.createChoice(options[0], false),
item.createChoice(options[1], false),
item.createChoice(options[2], false),
item.createChoice(options[3], true),
]);
item.setTitle(quest)
item.setRequired(true);
item.setPoints(1);
}
/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
getValue()
사용 하는 대신에 대한 반복 호출 getValues()
과 SpreadsheetApp.getActiveSheet()
변수에 활성 시트를 저장하는 대신 여러 번 사용하는 것을 포함하여 코드에 상당한 양의 반복이 있으며 (코드 샘플에서 수정 됨) 선택 사항을 하나씩 생성합니다. . 효율성을 높이기 위해 약간의 재 작업을 시도하는 것이 좋습니다.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다