Vec*_*tor 9 javascript google-sheets google-apps-script google-forms google-sheets-api
我搜索了互联网,我找不到对此的回复,也没有找到它的文档.我需要使用应用程序脚本使用Google电子表格中的数据动态生成Google表单问题,但我不知道如何引用和阅读电子表格.
Nac*_*oma 12
在电子表格中选择Tools > Script Editor并根据您的需求进行调整:
/**
After any change in the sheet, update the combobox options in the Form
*/
function onChange(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var range = sheet.getDataRange();
var values = range.getValues();
var comboValues = []; // <-- cheddar will go here
// in this example we are interested in column 0 and discarding row 1 (the titles)
for (var i = 1; i <= values.length; i++) {
var v = values[i] && values[i][0];
v && comboValues.push(v)
}
// Sort the values alphabetically, case-insensitive
comboValues.sort(
function(a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
}
);
Logger.log(comboValues);
// Use your form ID here. You can get it from the URL
var form = FormApp.openById('<my-form-id>');
/*
Uncomment this to display the item IDs
and pick the one that you want to modify
var items = form.getItems();
for (i = 0; i < items.length; i++) {
Logger.log("ID: " + items[i].getId(), ': ' + items[i].getType());
}
*/
form.getItemById(807137578).asListItem().setChoiceValues(comboValues);
};
Run Code Online (Sandbox Code Playgroud)
要进行调试,请在组合框中选择脚本,然后单击"播放"或"调试".您第一次必须授予其与电子表格和表单进行互动的权限.
对结果感到满意后,在编辑器中选择Resources > Triggers for the active project并添加此方法,以便在电子表格上进行任何修改(在更改时,而不是在编辑时).
在此之后,您的表单选项将在电子表格中的任何更改后实时更改.
这非常简单,请参见此处:https://developers.google.com/apps-script/guides/sheets#reading
您只需要通过其文档键打开工作表,选择数据并将单元格读取为 JS 对象。
| 归档时间: |
|
| 查看次数: |
37804 次 |
| 最近记录: |