[英]How to use Google Sheet's app script to add 90 days to user inputted date and email if new date is today's date
How can I get a date in google sheets to auto generate 90 days after user inputted date.如何在谷歌表格中获取日期以在用户输入日期后 90 天自动生成。 So, I have two columns, in column J, I would input a date when I first visited the patient.
所以,我有两列,在 J 列中,我将输入我第一次访问患者的日期。 Then in Column K the date should automatically populate for 90 days after the first visit.
然后在 K 列中,日期应在第一次访问后自动填充 90 天。 But this second date must auto populate and once we reached this date, the script should automatically send an email.
但是第二个日期必须自动填充,一旦我们到达这个日期,脚本应该自动发送 email。 I tried to go the easy way by using the google sheet itself but the problem is that once you delete a row, the formula for the entire column is deleted.
我尝试使用谷歌表本身以简单的方法 go 但问题是一旦删除一行,整个列的公式就会被删除。
Before asking this question, I tried to look at this other solution: How to add days to a date from Google Sheets?在问这个问题之前,我试着看看这个其他的解决方案: 如何从谷歌表格中添加日期到日期? but I am still kind of lost.
但我还是有点失落。
Now, I also know there is a function called TODAY() in google sheets itself and I tried that too.现在,我也知道谷歌表格本身中有一个名为 TODAY() 的 function,我也尝试过。 But here is the problem, what if someone/a user deletes a row, well then my TODAY() formula will be lost for rest of the rows.
但问题是,如果某人/用户删除了一行,那么我的 TODAY() 公式将丢失 rest 行。 This is why I need to do this in App script.
这就是为什么我需要在 App 脚本中执行此操作。 Can anyone please help me with this please?
任何人都可以请帮我吗? I have the following code so far.
到目前为止,我有以下代码。
function DateCompare() {
const ss = SpreadsheetApp.getActiveSheet();
const dataRange = ss.getDataRange();
const dataRangeVals = dataRange.getValues();
const headers = 2;
var getNewDate = new Date();
var getDate = new Date().getDate();
var getMonth = new Date().getMonth()+1;
var getYear = new Date().getFullYear();
var dateGlued = getMonth + "/" + getDate + "/" + getYear;
for(n=2;n<dataRangeVals.length;++n){
var cell = dataRangeVals[n][10]; // x is the index of the column starting from 0
var formattedDate = Utilities.formatDate(new Date(cell), "GMT", "MM/dd/YYYY");
if (dateGlued.valueOf() === formattedDate.valueOf()) {
Logger.log("Date Matched!")
}
}
}
function ifItsBeen90Days() {
const ss = SpreadsheetApp.getActiveSheet();
const dataRange = ss.getDataRange();
const dataRangeVals = dataRange.getValues();
const headers = 2;
dataValues
.filter(row => row[10] !== '' && row[11] !== "") //filtered data where row[9] is empty
.forEach(row => {
let message =
row[0] +
'\n' +
row[1] +
'\n' +
row[2] +
'\n' +
row[3] +
'\n' +
row[4] +
'\n' +
row[5];
let email = row[13];
let subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(email, subject, message);
Logger.log(`${subject}: ${message} sent to ${email}`);
});
}
Why use GAS when you can simply use a formula to check the criteria you required.当您可以简单地使用公式来检查所需的标准时,为什么还要使用 GAS。 The criteria (based on the image) is:
标准(基于图像)是:
So that formula equates to the following, just make sure to format the column as Date.因此,该公式等于以下公式,只需确保将列格式化为日期。
=IF(ISDATE(J2); J2+90; "")
So now we've got the date set up.所以现在我们已经确定了日期。
Then in order to automatically send E-Mails on that calculated date, you would need to run a trigger on a daily basis.然后,为了在该计算日期自动发送电子邮件,您需要每天运行触发器。 That trigger then runs a particular function, which would then check if any rows contain today's date.
然后,该触发器运行特定的 function,然后检查是否有任何行包含今天的日期。 If yes, then send out the email, if no, then do not.
如果是,则发送 email,如果不是,则不发送。 It is best-practice to log the activity to the sheet, so you have a visible confirmation that it has been sent.
将活动记录到工作表是最佳实践,这样您就可以看到活动已发送的确认信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.