问题
I have a google form with multiple sections, each section with a dropdown list. I wish to pull the data for the dropdown lists from spreadsheet with matching name.
This is the script i run but it doesn't seems to be working.
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);
}
}
});
}
This is a copy of the data: https://docs.google.com/spreadsheets/d/1jfzuVF64QoMIauyFy5Plxv0nQwukf8sMnFXIAyzyK0s/edit#gid=0
And here is a copy of the google form: https://docs.google.com/forms/d/1nsDQ6MtdCci-g5XgLxJ-4XNJ19E9sDz42G6DoFLwiFE/edit
Please help!
回答1:
I already had multiple choice form and so I just renamed the headings and generated some data (see table below). I played around with your code because I'd never seen a declaration like in the fourth line. Pretty cool thanks. I tried you code on my form which I created manually and to my surprise it worked the first time.
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);
}
}
});
}
Data Sheet:
| 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 |
Image of the Populated form:
I was surprised to find that it will take as many choices as you provide. Thanks for the code.
Your last question involves this line and I was thinking about this last night before I went to sleep and I finally realized what that additional filter was for. It's for the columns that don't have as many choices as the one with the most. I originally did not understand that so I removed it when I constructed this line:
hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]);col[h]=i+1;idx[h]=i; });
but should have done it this way:
hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]).filter(e=>e!=''); });col[h]=i+1;idx[h]=i; });
And that filter removes all of the blanks at the end of the shorter columns.
So just to be clear here's the final solution:
The code:
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);
}
}
});
}
And here's the data that I used this time:
| COL1 | COL2 | COL3 |
|---|---|---|
| 10 | 9 | 17 |
| 18 | 19 | 13 |
| 14 | 14 | 14 |
| 3 | 13 | |
| 4 | 7 | |
| 6 | ||
| 1 | ||
| 8 |
And here's the form:
来源:https://stackoverflow.com/questions/65849834/populate-bulk-data-from-google-spreadsheet-into-google-form-dropdown-list