![](/img/trans.png)
[英]Is it possible to export questions and multiple choice options from a Google Form to a Google Sheet?
[英]How can I populate multiple choice questions in a Google Form from data within a Google Sheet
簡而言之:我是 Google App Script 的新手,作為一個更大項目的一部分,我希望能夠根據存儲在 Google 表格中的信息為 Google 表單中的多項選擇項填充選項。 該表存儲了我工作的幾名員工的姓名和聯系信息。 我希望表單列出工作表中的個人姓名,並根據該選擇訪問其他聯系信息,以便稍后進行其他工作。
工作表的示例條目可能是(盡管工作表應該能夠包含任意數量的條目(顯然從第 2 行開始計數,以忽略標題)):
LNAME FNAME ADDRESS PHONE
Smith John 123 Sesame Street (123) 456-7890
Piper Peter 12 Shore Lane (098) 765-4321
至於表單,我已經用初始項目填充它,其中第一個是多項選擇項,我希望工作表中的每個名稱都有不同的條目(在這種情況下,我希望有 2 個條目列出“John Smith”和“彼得·派珀”)。
將加載這些名稱的代碼放在表單的 onload() 函數中似乎是合乎邏輯的,因為每次使用時表單都會更新。 至於代碼,我最初嘗試了以下內容:
function onOpen() {
// Get a handle for the form itself.
var form = FormApp.getActiveForm();
// Who is completing the form?
var swSheet = SpreadsheetApp.openByUrl("SHEET URL"));
var sheet = swSheet.getSheetByName("Staff");
var staff= form.getItems()[0];
var mcitem = staff.asMultipleChoiceItem();
mcitem.setChoices([
mcitem.createChoice(
mcitem.createChoice(sheet.getRange(2, 2) + " " + sheet.getRange(2, 3)),
mcitem.createChoice(sheet.getRange(3, 2) + " " + sheet.getRange(3, 3)),
mcitem.createChoice(sheet.getRange(4, 2) + " " + sheet.getRange(4, 3))
]);
}
這樣做的問題是我在調用 openByUrl() 時遇到錯誤(在線搜索顯示出於安全原因已棄用)。 有一個 IMPORTRANGE() 函數可以為我提取正確的信息,但這似乎只能從工作表內訪問,而不是在 Google App 腳本中。 我也在網上環顧四周,似乎找不到我可以考慮的任何其他想法。
注意:我意識到這是前 3 個條目的硬編碼,而不是無限數量,但這是最終目標。 目前我只是想弄清楚如何將信息從工作表中提取到 Google App 腳本中。
// will be automatically run every time the user opens the URL of the web deployment
function doGet(){
var form = FormApp.openById("FORM ID");
var swSheet = SpreadsheetApp.openByUrl("SHEET URL"));
var sheet = swSheet.getSheetByName("Staff");
var staff= form.getItems()[0];
var mcitem = staff.asMultipleChoiceItem();
mcitem.setChoices([
mcitem.createChoice(sheet.getRange(2, 2) + " " + sheet.getRange(2, 3)),
mcitem.createChoice(sheet.getRange(3, 2) + " " + sheet.getRange(3, 3)),
mcitem.createChoice(sheet.getRange(4, 2) + " " + sheet.getRange(4, 3))
]);
// get the link to the form
var URL=form.getPublishedUrl();
// redirect to the prefilled form URL dynamically created above
return HtmlService.createHtmlOutput("<script>window.top.location.href=\"" + URL + "\"</script>");
}
這是我對此的建議。 這個對我有用; 從 Google 工作表中的行和列范圍中獲取值,並將它們放在 Google 表單多項選擇網格問題中:
var form = FormApp.openById('MyForm');
var PtjGridList = form.getItemById(MyFormItemId).asGridItem();
var ss = SpreadsheetApp.openById("MyGoogleSheet");
var PtjNombre = ss.getSheetByName("MyDataSheet");
var RowValues = PtjNombre.getRange(2, 1, PtjNombre.getLastRow() - 1).getValues();
var ValuesRow = [];
for(var i = 0; i < RowValues.length; i++)
if(RowValues[i][0] != "")
ValuesRow[i] = RowValues[i][0];
PtjGridList.setRows(ValuesRow)
var ColumnValues = PtjNombre.getRange(2, 2, PtjNombre.getLastRow() - 1).getValues();
var ValuesColumn = [];
for(var i = 0; i < ColumnValues.length; i++)
if(ColumnValues[i][0] != "")
ValuesColumn[i] = ColumnValues[i][0];
PtjGridList.setColumns(ValuesColumn)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.