有没有办法在Appscript中按日期过滤JSON数组?

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

我正在使用 URL fetch 提取数据,但我只需要 2010 年 1 月 1 日之后的数据。

  var response = UrlFetchApp.fetch(url, options); 
  var data = JSON.parse(response.getContentText()); 
    
  // Assuming the data is in an array  
  var observations = data; 
  var recentData = [["Date", "Type", "Value"]]; // Headers for the sheet 
    
  // Loop through observations and extract the necessary fields  
  observations.forEach(function(obs) { 
    recentData.push([obs.Date, obs['Type '], obs.Value]){

//Here's what I'm trying to change
    if(obs.Date > Date(2009, 12, 0)) {
      return date;
    }
  };
  }); 
 
  // Clear the sheet  
  newSheet.clearContents(); 
  Logger.log(sheetName + " cleared.") 
  
  // Write the data to the sheet  
  newSheet.getRange(1, 1, recentData.length, 3).setValues(recentData); 
  Logger.log("Written data for " + observations.length + " observations.");

我正在尝试向 forEach 循环添加日期过滤器,但出现错误。

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

尝试这样的事情:

  const dt = new Date(2010,0,1).valueOf();
  const data = JSON.parse(response.getContentText()).filter(r => new Date(r[0]).valueOf() > dt)
© www.soinside.com 2019 - 2024. All rights reserved.