javascript - 关于谷歌表单中带部分导航的动态下拉列表应用脚本的问题
问题描述
我正在尝试在 Google 表单中创建动态下拉列表和部分导航。但是,我的脚本可以在达到配额时自动删除选项,该选项无法导航到相关页面以进行其他选择。
我正在为我的医院计划一次健康检查活动。由于乌鸦控制政策,它需要按时间段和日期预订。下面的链接是我的表单谷歌电子表格和我愚蠢的谷歌表单功能。
https://forms.gle/ZV9Djni8hyQGdAd86
https://docs.google.com/spreadsheets/d/1F1dpGCTSlpEOUMh5txsZouhx784JmJvh66IsiGfDTtg/edit?usp=sharing
参考:
- 如何使用应用脚本设置 Google 表单问题的转到部分
- https://www.pbainbridge.co.uk/2019/04/dynamically-remove-google-form-options.html
function appointmentSlots() {
var form = FormApp.openById("1VqFBKBD_-iKYk_3Ze40j2tvRIi093-alaoCDsXpFi8k");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var date1timelist = form.getItemById("2101588132").asListItem();
var optionsSheet = ss.getSheetByName('Date Options');
var dateoptions = optionsSheet.getRange('A2:A3').getValues();
var dateleft = optionsSheet.getRange('C2:C3').getValues();
var day1sheet = ss.getSheetByName('9/3');
var day1timeoptions = day1sheet.getRange('A2:A4').getValues();
var day1left = day1sheet.getRange('C2:C4').getValues();
var formFieldsArray = [
["9/3", 2061926149],
["10/3", 1632977105]
];
for(var h = 2; h < formFieldsArray.length; h++) {
var datelist = form.getItemById(formFieldsArray[h][2]).asListItem();
var avaibledateoptions = [];
var sectionday1timeslots = form.getItemById("2101588132").asPageBreakItem();
var sectionday2timeslots = form.getItemById("1630116063").asPageBreakItem();
var datechoice = datelist.getChoices();
var optionsDataLength = dateoptions.length;
for (var i=0; i<optionsDataLength; i++) {
var choice = dateoptions[i][0];
var left = dateleft[i][0];
if ((choice != '') && (left > 0) == formFieldsArray[h][2]) {
if (formFieldsArray[h]= "9/3") {
datechoice.push(datelist.createChoice(avaibledateoptions,sectionday1timeslots));
}
else {
datechoice.push(datelist.createChoice(avaibledateoptions,sectionday2timeslots));
datelist.setChoices(avaibledateoptions);
}
}
}
}
var day1avaibledateoptions = [];
var optionsday1Length = day1timeoptions.length;
for (var i=0; i<optionsday1Length; i++) {
var day1timechoice = day1timeoptions[i][0];
var day1timeleft = day1left[i][0];
if ((day1timechoice != '') && (day1timeleft > 0)) {
day1avaibledateoptions.push(day1timechoice);
}
}
date1timelist.setChoiceValues(day1avaibledateoptions)
}
//etc for day2 timeslots choice and day3 timeslots
}
}
}
解决方案
为了根据电子表格中不断变化的单元格值(由新表单提交引起)修改您的表单,您需要设置一个可安装的 onChange 触发器,该触发器基本上会在您的电子表格更改完成时运行您的函数(就像一个即将到来的从表单提交)。要创建这样的触发器,请访问您的触发器面板,然后单击Create trigger
并选择事件类型onChange
,将其分配给您将用于创建/删除表单项的功能。
一旦用户提交了一个新表单,并且您在电子表格上进行了某些计算以确定该时间段有多少空位,您可以获取该单元格的值,该单元格告诉您该时间段内有多少免费约会是免费的,如果那数字是0
您可以继续使用方法deleteItem()删除该问题元素。
如果您最终重置表单(因为您的时间段再次空闲或有人取消会议),您可以通过重新创建元素来撤消此操作。
下面这段代码是一个基本示例,说明如何根据电子表格单元格的更改删除和创建表单项。它有不言自明的评论:
function onChange() {
// Get the different sheets where you have all your left places in your time slots
var sheet = SpreadsheetApp.getActive().getSheetByName('A');
// Get your form
var form = FormApp.openById('FORMID');
// Here you would get each element that might depend on whether there are any
// appointments left or not
var element = form.getItems()[2];
// Get the cell value that tells you if the time slot is already full (full=0)
var value = sheet.getRange('C2').getValue();
// If the value is 0 it means that this time slot is all completed and nobody
// should be able to select it again
if (value == 0) {
// delete this item
form.deleteItem(element);
// if it is not full yet, it might be because your reset the time slot and therefore
// the element does not exist any more
} else {
// if the element exists dont do anything but if it doesnt and there are available
// apointments create it again
if (!element) {
form.addMultipleChoiceItem().setTitle('B').setChoiceValues(['Cats', 'Dogs']);
}
}
}
如果您想删除一个选项而不是一个项目,您可以查找该项目,将所有选项作为一个数组获取,然后从该数组中删除您不再需要的选项。最后,您可以使用更新的选项更新项目setChoices()
。这是有关如何实现此目的的代码示例:
function myFunction() {
// This is an example where I only have a single multiple choice item
var choiceItem = FormApp.getActiveForm().getItems(FormApp.ItemType.MULTIPLE_CHOICE)[0].asMultipleChoiceItem();
// Get current choices array
var choices = choiceItem.getChoices();
// Get choice you want to delete, this would be your times or dates obtained from
// the cell values
var choiceToBeRemoved = "A";
// remove choice from array
choices = choices.filter(function(choice){return choice.getValue() !== choiceToBeRemoved});
// Set updated choices
choiceItem.setChoices(choices);
}
参考
推荐阅读
- offline - Ray - RLlib - 自定义环境错误 - 连续动作空间 - DDPG - 离线体验培训?
- ios - 如何在表格视图单元格内的 TextView 中使用动态类型
- c# - 混合.Net Standard和.Net Framework时如何自动部署隐藏NuGet依赖的C# DLL?
- reactjs - 多选仅返回一个选项
- php - 如何在 api 请求中声明 json 的 base64?
- google-sheets - 试图突出显示与所选单元格相同的单元格
- c# - C# 在 System.Sockets 中接收数据包及示例
- c++ - 为什么我没有匹配到 operator>> 有什么原因吗?
- vue.js - Quasar 表未更新 (v1)
- java - scala 与 java 在“无限网格中的最小步数”问题中的性能(关闭)