简体   繁体   English

如果新日期是今天的日期,如何使用 Google Sheet 的应用脚本向用户输入的日期和 email 添加 90 天

[英]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:标准(基于图像)是:

  1. 90 Days after the date which was entered in column J在 J 栏中输入的日期之后的 90 天

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.

相关问题 Google 脚本:如果单元格包含比今天早 10 天的日期,如何隐藏一行 - Google script: How to hide a row if a cell contains a date 10 days older than today's date Go 到带有 Google Apps 脚本的工作表中包含今天日期的单元格 - Go to the cell that contains today's date in a Sheet with Google Apps Script 如果单元格与今天的日期匹配,如何通过电子邮件发送几行Google工作表 - How to send several rows of google sheet table via email if cell match today's date 在Google App脚本中获取今天的日期 - Getting today's date in google App Scripts 如何在Google应用脚本中定义今天的日期? - How to define today date in Google app script? 电子表格中的日期和应用脚本中的今天日期 - Date from spreadsheet and Today's Date in app script Google App Script Utilities.formatDate(new Date()今天增加了几天 - Google App Script Utilities.formatDate(new Date() adding days to today 根据今天的日期打开Goog​​le表格到特定地点 - Open a Google sheet to a specific place based on today's date Google表格脚本有条件地将空白单元格的值设置为今天的日期,但现有数据保持不变 - Google Sheet Script conditionally set value of blank cells to today's date but leave existing data the same Google Apps脚本 - 将包含今天日期的一张表格中的行复制到另一张表格中 - Google Apps Script - Copy rows from one sheet containing today's date into another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM