Google Sheets同步数据到Google Form时如何过滤排除周日相关选项
过滤周日日期选项实现方案
你现有代码只需在读取表格数据的过滤逻辑中增加星期判断即可,核心是利用JavaScript的Date.getDay()方法:该方法返回值为0时对应周日,1~6分别对应周一到周六。
你使用的对应表格见下图:
修改后的完整代码如下:
function isSunday(dateStr) { const date = new Date(dateStr); // 非日期格式的内容直接返回false,不做过滤 if (isNaN(date.getTime())) return false; return date.getDay() === 0; } function getDataFromSpreadSheet(){ var [header, ...data] = timeStore.getDataRange().getDisplayValues(); var choices = {}; header.forEach(function(title, index){ choices[title] = data.map(row => row[index]).filter(e => e !== "" && !isSunday(e)); }); Logger.log(choices); return choices; } function populateTimeOptions(){ var items = googleForm.getItems(); var choices = getDataFromSpreadSheet(); items.forEach(function(item){ var itemTitle = item.getTitle(); Logger.log(itemTitle); if(itemTitle in choices){ var itemType = item.getType(); Logger.log(itemType + " " + itemTitle); switch(itemType){ case FormApp.ItemType.LIST: item.asListItem().setChoiceValues(choices[itemTitle]); default: Logger.log("Ignore question", itemTitle); } } }); }
注意事项
- 确保你的Google Sheets中日期列为标准可被JS解析的日期格式,若自定义格式无法识别,调整为系统默认日期格式即可正常判断
- 非日期类型的选项不会被该过滤逻辑影响,可正常使用
内容的提问来源于stack exchange,提问作者Glamerstone
相关产品推荐
相关产品推荐

