我有一个包含多个部分的谷歌表单,每个部分都有一个下拉列表。我希望从具有匹配名称的电子表格中提取下拉列表的数据。
这是我运行的脚本,但它似乎不起作用。
function getDataFromGoogleSheets() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("DATA");
const [header, ...data] = sheet.getDataRange().getDisplayValues();
const choices = {}
header.forEach(function(title, index) {
choices[title] = data.map(row => row[index]).filter(e => e !== "");
});
return choices;
}
function populateGoogleForms() {
const GOOGLE_FORM_ID = "1nsDQ6MtdCci-g5XgLxJ-4XNJ19E9sDz42G6DoFLwiFE";
const googleForm = FormApp.openById(GOOGLE_FORM_ID);
const items = googleForm.getItems();
const choices = getDataFromGoogleSheets();
items.forEach(function(item) {
const itemTitle = item.getTitle();
if (itemTitle in choices) {
const itemType = item.getType();
switch (itemType) {
case FormApp.ItemType.CHECKBOX:
item.asCheckboxItem().setChoiceValues(choices[itemTitle]);
break;
case FormApp.ItemType.LIST:
item.asListItem().setChoiceValues(choices[itemTitle]);
break;
case FormApp.ItemType.MULTIPLE_CHOICE:
item.asMultipleChoiceItem().setChoiceValues(choices[itemTitle]);
break;
default:
Logger.log("Ignore question", itemTitle);
}
}
});
}
这是数据的副本: https://docs.google.com/spreadsheets/d/1jfzuVF64QoMIauyFy5Plxv0nQwukf8sMnFXIAyzyK0s/edit#gid=0
这是谷歌表单的副本: https://docs.google.com/forms/d/1nsDQ6MtdCci-g5XgLxJ-4XNJ19E9sDz42G6DoFLwiFE/edit
请帮忙!
答案 0 :(得分:1)
我已经有了多项选择表,所以我只是重命名了标题并生成了一些数据(见下表)。我玩弄了你的代码,因为我从未见过像第四行那样的声明。很酷谢谢。我尝试在我手动创建的表单上编写代码,令我惊讶的是它第一次就成功了。
function getDataFromGoogleSheets() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Sheet1");
const [hA, ...rows] = sheet.getDataRange().getDisplayValues();
const cols = {};//just made some minor changes to fit my personal likes in labeling
const col={};
const idx={};
hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]);col[h]=i+1;idx[h]=i; });
return cols;
}
function populateGoogleForms() {
const GOOGLE_FORM_ID = getGlobal('formid');//Have the id stored in a spreaddsheet. Other than that though it's exactly the same code
const googleForm = FormApp.openById(GOOGLE_FORM_ID);
const items = googleForm.getItems();
const choices = getDataFromGoogleSheets();
items.forEach(function (item) {
const itemTitle = item.getTitle();
if (itemTitle in choices) {
const itemType = item.getType();
switch (itemType) {
case FormApp.ItemType.CHECKBOX:
item.asCheckboxItem().setChoiceValues(choices[itemTitle]);
break;
case FormApp.ItemType.LIST:
item.asListItem().setChoiceValues(choices[itemTitle]);
break;
case FormApp.ItemType.MULTIPLE_CHOICE:
item.asMultipleChoiceItem().setChoiceValues(choices[itemTitle]);
break;
default:
Logger.log("Ignore question", itemTitle);
}
}
});
}
数据表:
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | COL7 | COL8 | COL9 | COL10 |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 1 | 2 | 0 | 1 | 2 | 1 | 0 | 0 |
2 | 2 | 2 | 1 | 1 | 2 | 2 | 2 | 2 | 2 |
0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
已填充表单的图像:
我惊讶地发现,您提供的选择将与您提供的选择一样多。感谢您的代码。
<块引用>你的最后一个问题涉及这一行,我昨晚睡觉前正在考虑这个问题,我终于意识到这个额外的过滤器是做什么用的。它适用于没有选择最多的列。我原本不明白,所以我在构建这一行时将其删除:
hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]);col[h]=i+1;idx[h]=i; });
但应该这样做:
hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]).filter(e=>e!=''); });col[h]=i+1;idx[h]=i; });
该过滤器会删除较短列末尾的所有空白。
为了清楚起见,这是最终的解决方案:
代码:
function getDataFromGoogleSheets() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Sheet1");
const [hA, ...rows] = sheet.getDataRange().getDisplayValues();
const cols = {};
const col={};
const idx={};
hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]).filter(e=>e);col[h]=i+1;idx[h]=i; });
return cols;
}
function populateGoogleForms() {
const GOOGLE_FORM_ID = getGlobal('formid');
const googleForm = FormApp.openById(GOOGLE_FORM_ID);
const items = googleForm.getItems();
const choices = getDataFromGoogleSheets();
items.forEach(function (item) {
const itemTitle = item.getTitle();
if (itemTitle in choices) {
const itemType = item.getType();
switch (itemType) {
case FormApp.ItemType.CHECKBOX:
item.asCheckboxItem().setChoiceValues(choices[itemTitle]);
break;
case FormApp.ItemType.LIST:
item.asListItem().setChoiceValues(choices[itemTitle]);
break;
case FormApp.ItemType.MULTIPLE_CHOICE:
item.asMultipleChoiceItem().setChoiceValues(choices[itemTitle]);
break;
default:
Logger.log("Ignore question", itemTitle);
}
}
});
}
这是我这次使用的数据:
COL1 | COL2 | COL3 |
---|---|---|
10 | 9 | 17 |
18 | 19 | 13 |
14 | 14 | 14 |
3 | 13 | |
4 | 7 | |
6 | ||
1 | ||
8 |
这是表格: