按谷歌应用脚​​本按日期过滤数组 [英] Filter array by date with google app script

查看:106
本文介绍了按谷歌应用脚​​本按日期过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个谷歌电子表格,它是一列2列的列A日期(我敢肯定是日期,所有的日期函数在所有的列上工作正常)和B列文本。 p>

使用Google应用程序脚本,我可以获得范围的所有值:

  var sheet = SpreadsheetApp.openById(.........); 
var ss = sheet.getSheetByName(.......);
var range = ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
var data = range.getValues();

现在我应该有一个2d数组,数据[0]上的日期和数据上的文本[1]



我可以用数据过滤数组[1]:

  var filter = data.filter(function(dataRow){
return dataRow [1] =='sometext';
});

我需要的是按某个日期值过滤数据[0](如年,月或一段时间)



我试过了:

  var filter = data.filter(function(dataRow){
return dataRow [0] .getFullYear == 2016;
});

但是任何类型的日期函数都会给我一个错误,指出找不到函数

我做错了什么?



感谢任何帮助

祝你好运

文本,其次是日期)和以下脚本:

  function myFunction(){
var sheet = SpreadsheetApp。 openById( ...);
var ss = sheet.getSheetByName(testing);
var range = ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
var data = range.getValues();

var filtered = data.filter(function(row){
return row [1] .getFullYear()=== 2016;
});

Logger.log(已过滤);
}

只要第二列是Date类型(格式→编号→日期)。如果将类型更改为文本,我会得到您所描述的错误。如果代码中没有错误(例如 .getFullYear 而不是 .getFullYear(),因为它是一个函数,而不是一个属性),我认为数据(不是变量,但电子表格的数据)不是日期类型。


i've a google spreadsheet that is a 2 columns table that has on column A dates (i'm sure that are dates, and all the date functions works fine on all the column) and on column B text.

With google app script i've get all the values of the range with:

var sheet = SpreadsheetApp.openById(".........");
var ss = sheet.getSheetByName(".......");
var range =ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
var data =  range.getValues();

now i should have a 2d array with dates on data[0] and text on data[1]

i can filter the array on data[1] with:

var filter = data.filter(function (dataRow) {
return dataRow[1] == 'sometext';
});

what i need is to filter data[0] by some date value (like year, or month or a period)

i've tried:

var filter = data.filter(function (dataRow) {
return dataRow[0].getFullYear == 2016;
});

but any kind of date function gets me an error that states that is impossible to find the function (like getFullYear()) in the object.

What am i doing wrong?

Thank for any help

Best regards

解决方案

I've created a similar spreadsheet (first column with text, second with dates) and the following script:

function myFunction() {
  var sheet =  SpreadsheetApp.openById("…");
  var ss    = sheet.getSheetByName("testing");
  var range = ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
  var data  = range.getValues();

  var filtered = data.filter(function (row) {
    return row[1].getFullYear() === 2016;
  });

  Logger.log(filtered);
}

And it works as long as the second column is of Date type (Format→Number→Date). I get the error you've described if I change the type to Text. If there are no errors in your code (like .getFullYear instead of .getFullYear() as it's a function, not a property), I think the data (not the variable, but the spreadsheet's data) is not of Date type.

这篇关于按谷歌应用脚​​本按日期过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆