loops - Google Script - isDate 函数和优化 - 寻找与 for 循环不同的方法
问题描述
我有几列,每行(人)都有不同类型的数据。例如,第 1 列 (A) 包含名称,第 2 列 (B) 包含点数,第 3 列包含用户设置的日期(如果没有,则为空)。
我遇到的第一个问题是验证输入以在工作表中设置日期。现在它只是在那里粘贴任何东西,所以我做了一个解决方法:
var input = (request.parameter.input || "");
ss.getRange("I1").setValue(input); //input given by user using text
var isdate = ss.getRange("J1").getDisplayValue(); //J1 contains a formula "=isDATE(I1)"
if(isdate == "TRUE"){ ss.getRange(position + 2, 3, 1, 1).setValue(input);}
然而,这并不是真正有效的。但我似乎无法让它发挥作用。
第二个问题是我想遍历所有行以查看该日期是否与今天的日期相对应(所以如果这将是他们的生日)并将它们推送到字符串上,然后最后返回字符串。
var input = (request.parameter.input || ""); var output = ContentService.createTextOutput; var users = ss.getRange("A2:A").getValues().filter(String); var points = ss.getRange("B2:B").getValues().filter(String); var birthdays = ss.getRange("C2:C").getValues(); var list = []; function isBirthdayToday(birthday) { if(typeof birthday === "string"){ birthday = new Date(birthday);} var today = new Date(); if((today.getDate() === birthday.getDate()) && (today.getMonth() === birthday.getMonth())) { return true; } else { return false; } } for (var i = 0; i < users.length; i++) { var date = ss.getRange(i + 2, 3, 1, 1).getValue(); if(isBirthdayToday(date)){ var name = ss.getRange(i + 2, 1, 1, 1).getValue(); list.push(name); } } return output(list) //return all names
我确实相信必须有一种更有效的方法来做到这一点。
解决方案
您可以使用以下代码搜索生日男孩/女孩
function getBirthNames(){
let sheet = SpreadsheetApp.getActive().getSheetByName(-- here is your sheet name --),
values = sheet.getDataRange().getValues(),
today = new Date();
return values.reduce((resArr,element)=>{
let elementDate = new Date(element[2]); // column C with birthdays
if (elementDate.getDate()==today.getDate() && elementDate.getMonth()==today.getMonth()){
resArr.push(element[0]) // column A with names
};
return resArr},[])
}
推荐阅读
- json - Kubernetes 服务未启动
- reactjs - 如何在reactjs中的循环内编写write if语句
- python - 使用 cppyy 从 Python 解析 C (C++) 调用方法
- ruby-on-rails - 如何自动将数据从url保存到数据库
- c# - 并行性问题
- r - 提取特定行并列出 R 中的行
- ag-grid - 我可以阻止主行更改关闭详细信息吗?
- android - cordova-icon 实用程序抛出错误“spawn convert ENOENT”
- python - 使用python从图像中提取补丁
- python - Ansible - python 解释器的包装器