[英]How to extract unique data from a column in google sheets with corresponding values using google apps script?
[英]How do I populate Google Form questions using column A data from different Google Sheets Tabs using Apps Script?
我正在尝试使用 Apps 脚本从 Google Sheet 工作簿中填充 Google Form 问题。 我遇到的问题是表单没有填充,并且我收到错误 TypeError: Cannot read property '0' of undefined specific on line googleSheetsQuestions[0]。 如何仅使用两个表格中的 A 列值填充我的 Google 表单上的 2 个问题?
function openForm(e)
{
populateQuestions();
}
function populateQuestions() {
var form = FormApp.getActiveForm();
var googleSheetsQuestions = getQuestionValues();
var itemsArray = form.getItems();
itemsArray.forEach(function(item){
googleSheetsQuestions[0].forEach(function(header_value, header_index) {
if(header_value == item.getTitle())
{
var choiceArray = [];
for(j = 1; j < googleSheetsQuestions.length; j++)
{
(googleSheetsQuestions[j][header_index] != '') ? choiceArray.push(googleSheetsQuestions[j][header_index]) : null;
}
item.asCheckboxItem().setChoiceValues(choiceArray);
}
});
});
}
function getQuestionValues() {
var ss= SpreadsheetApp.openById('1234567890');
["Sheet1", "Sheet2"].forEach(function (s) {
var questionSheet = ss.getSheetByName(s);
var returnData = questionSheet.getDataRange().getValues();
debugger;
return returnData;
})
}
在您的情况下,以下示例脚本怎么样?
function populateQuestions() {
// Retrieve values from Google Spreadsheet.
var ss = SpreadsheetApp.openById('1234567890');
var obj = ["Sheet1", "Sheet2"].reduce((o, s) => {
var sheet = ss.getSheetByName(s);
var [h, ...values] = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
o[h] = values;
return o;
}, {});
// Put values to the Google Form.
var form = FormApp.getActiveForm();
var itemsArray = form.getItems();
itemsArray.forEach(e => {
var item = e.asCheckboxItem();
var title = item.getTitle();
if (obj[title]) {
item.setChoiceValues(obj[title]);
}
});
}
运行此脚本时,将从 2 张 Google 电子表格中检索值,并将检索到的值放入 Google 表单中。
在您的显示脚本中, googleSheetsQuestions
of var googleSheetsQuestions = getQuestionValues();
总是undefined
的。 根据您的目标,在这种情况下,我使用了一个对象来搜索问题的标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.