编写 Google Apps 脚本:按今天开始的日期过滤

问题描述 投票:0回答:1

从这种表中:

|日期 |对象| | 2024 年 2 月 23 日 |对象A | | 2024 年 2 月 23 日 |对象A | | 2024 年 2 月 24 日 |对象 B | | 2024 年 2 月 24 日 |对象C | | 15/03/2024 |对象C | | 15/03/2024 |对象 B | | 17/03/2024 |对象 A |

我想仅过滤从今天到未来 3 天发生的对象。 每次运行此脚本时,我都想从“今天”过滤到接下来的 3 天。

因此,我只能专注于今天、明天等重要的事物。

你能帮我吗? 谢谢!

我是脚本新手。我刚刚看了一些过去的文章,但找不到类似的内容

date google-apps-script filter
1个回答
0
投票

按日期过滤:您提供日期列。

function filterDates() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("SheetName");
  const dt = new Date();
  const dtl = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//low
  const dth = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 3).valueOf();//hi
  const col =2;//column of dates to filter
  const vs = sh.getDataRange().getValues().filter(r => {
    let d = new Date(r[col - 1]).valueOf();
    return d >= dtl && d <= dth;
  }).filter(e => e);
  return vs;
}
© www.soinside.com 2019 - 2024. All rights reserved.