[英]Google forms - Autofill a multiple choice survey
我在谷歌表格上創建了這個心理學調查。 除了通常的電子郵件、姓名等之外,它還有大約 20 個選擇題。所以你會看到沒有任何正確/錯誤的答案。
我如何偽造結果,或者創建 50 個表單提交? 請原諒我的無知,但結果會出現在 Google 電子表格中嗎? 那么我應該在 Google 電子表格上填寫 50 個詳細信息嗎?
@Mogsdad 的答案引用了一個基於 url 的解決方案,其中您的腳本不知道可以為給定問題(或多少問題等)做出什么樣的答案,這在某些用例中可能會出現問題。
您可以使用Forms Service以編程方式為 Google 表單創建響應,該服務允許您從可用選項中隨機選擇可能的答案等(例如引用預定義的“書面”答案庫,例如文本/段落答案)。
舉個例子,假設您有一個僅限多項選擇的測試,每個問題的選擇數量非常多:
function foo() {
const form = FormApp.openById("some form id");
const randomSubs = [], nSubs = 50;
while (randomSubs.length < nSubs)
randomSubs.push(createRandomSubmission_(form).submit());
// doAwesomeAnalysis(randomSubs); // etc.
}
// Constructs a random response for the given form, and returns it to the caller (e.g. for submission, etc).
function createRandomSubmission_(form) {
const resp = form.createResponse();
const questions = form.getItems().filter(isAnswerable_);
questions.forEach(function (question) {
var answer = getRandomAnswer_(question);
resp.withItemResponse(answer);
});
return resp;
}
var iTypes = FormApp.ItemType;
function isAnswerable_(item, index, allItems) {
const iType = item.getType();
switch (iType) {
case iTypes.MULTIPLE_CHOICE:
case iTypes.CHECKBOX:
/** add more type cases here as you implement the relevant answer generator */
return true;
default:
return false;
}
}
// Uses the item type to call the appropriate answer generator.
function getRandomAnswer_(q) {
const qType = q.getType();
switch (qType) {
case iTypes.MULTIPLE_CHOICE:
return getRandomMultipleChoiceAnswer_(q.asMultipleChoiceItem());
/** add more type cases + handlers here as you implement the relevant answer generator */
default:
throw new TypeError("Answering questions of type '" + qType + "' is not yet implemented");
}
}
// Uniformly samples possible choices (including the "other" option, if enabled).
// Returns the item's ItemResponse
function getRandomMultipleChoiceAnswer_(mcItem) {
const choices = mcItem.getChoices();
const i = Math.floor( Math.random() * (choices.length + mcItem.hasOtherOption()) );
return mcItem.createResponse( (i < choices.length) ?
choices[i] : getRandomMCOtherOption_(mcItem)
);
}
function getRandomMCOtherOption_(mcItem) {
// This function will be highly dependent on your situation.
// It's your choice how you identify the MC item to determine what an "other" option could
// be for a given question. getTitle() and getIndex() may be useful too.
switch (mcItem.getId()) {
default:
throw new Error("Not Implemented Yet");
}
}
我如何偽造結果,或者創建 50 個表單提交?
請參閱使用應用腳本打開表單並進行選擇。
請原諒我的無知,但結果會出現在 Google 電子表格中嗎? 那么我應該在 Google 電子表格上填寫 50 個詳細信息嗎?
回復將記錄在兩個地方; 在表單本身(您可以在其中查看回復摘要)以及電子表格中。 如果您的目的是驗證提交響應的過程,那么您應該模擬提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.