Как подсчитать строки с уникальными датами в ForEach L oop? - PullRequest
1 голос
/ 11 февраля 2020

В настоящее время я пытаюсь l oop просмотреть следующие данные и подсчитать количество строк с уникальной датой. т. е. ответ на приведенный ниже набор данных должен быть 8.

Таблица данных

Моя попытка приведена ниже, однако оператор if, который я использую только когда-либо, возвращает истинное значение - не знаю, почему оно не вернет false, когда date будет равна dateCheck [i -1]. Когда я печатаю значения в журналах, они отличаются, но оператор if все еще возвращает true?

function rowCount() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var data = sheet.getRange(2,1,lastRow - 1,17).getValues();   
  var dateCheck = []
  var uniqueDates = 0
  data.forEach(function(row,i) {
    var date = row[0];
    dateCheck.push(date);
    if (date != dateCheck[i - 1]) {
    Logger.log('new date');
    uniqueDates += 1  
    }
    else { 
    Logger.log('same day');
    }     
  Logger.log(uniqueDates);  
  });
}

Ответы [ 2 ]

2 голосов
/ 11 февраля 2020

Когда вы выбираете значения даты из таблицы Google, они предоставляются как Javascript Объекты даты. Вы не можете напрямую сравнивать значения двух объектов, так как они никогда не будут равны.

Например, если вы создаете две одинаковые даты и сравниваете их напрямую, например:

var date1 = new Date(2020,01,01);
var date2 = new Date(2020,01,01);
if(date1 == date2){
   console.log('dates are equal');
}else{
   console.log('dates are not equal');
}

это выведет "даты не равны"

Вместо этого вам нужно сохранить представление дат, с которыми вы можете сравнить. Лучше всего использовать Date.getTime () , который возвращает отметку времени в миллисекундах, которая представляет дату.

  data.forEach(function(row,i) {
    var date = row[0];
    dateCheck.push(date.getTime());
    if (date.getTime() != dateCheck[i - 1]) {
       Logger.log('new date');
       uniqueDates += 1  
    }
    else { 
       Logger.log('same day');
    }     
  }

Вы упоминаете в своем вопросе, что ищете уникальные даты, но в вашем коде вы действительно определяете изменения в датах между строками. Я предполагаю, что ваши данные отсортированы по дате, и в этом случае ваш текущий код будет работать. Однако, если вы не хотите сортировать свои данные или хотите гарантировать уникальные даты, вы можете использовать indexOf следующим образом:

    if (dateCheck.indexOf(date.getTime()) > -1) {
       Logger.log('new date');
       uniqueDates += 1  
    }

Это проверяет любой экземпляр текущей даты в любом месте массива dateCheck независимо от сортировки. Обратите внимание, что мы проверяем,>> -1 ли это, потому что у вас может быть совпадение с индексом 0 массива.

0 голосов
/ 12 февраля 2020

Попробуйте это:

function countUniqueDates() {
  const sr=4;
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,sh.getLastColumn());
  const v=rg.getValues();
  var uA=[];
  v.forEach(function(r,i){
    var ds=Utilities.formatDate(new Date(r[0]),Session.getScriptTimeZone(),"E MMM dd, yyyy");
    if(uA.indexOf(ds)==-1) {
      uA.push(ds);
    }              
  });
  SpreadsheetApp.getUi().alert(`There are ${uA.length} dates in this sheet.`);
}

Вот мои данные:

1,2,3,4,5,6
0,1,2,3,4,5
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6
Tue Feb 11 2020 06:00:00 GMT-0700 (Mountain Standard Time),1,2,3,4,5
Tue Feb 11 2020 06:20:00 GMT-0700 (Mountain Standard Time),2,3,4,5,6
Tue Feb 11 2020 06:40:00 GMT-0700 (Mountain Standard Time),3,4,5,6,7
Tue Feb 11 2020 07:00:00 GMT-0700 (Mountain Standard Time),4,5,6,7,8
Tue Feb 11 2020 07:20:00 GMT-0700 (Mountain Standard Time),5,6,7,8,9
Tue Feb 11 2020 07:40:00 GMT-0700 (Mountain Standard Time),6,7,8,9,10
Tue Feb 11 2020 08:00:00 GMT-0700 (Mountain Standard Time),7,8,9,10,11
Tue Feb 11 2020 08:20:00 GMT-0700 (Mountain Standard Time),8,9,10,11,12
Tue Feb 11 2020 08:40:00 GMT-0700 (Mountain Standard Time),9,10,11,12,13
Tue Feb 11 2020 09:00:00 GMT-0700 (Mountain Standard Time),10,11,12,13,14
Tue Feb 11 2020 09:20:00 GMT-0700 (Mountain Standard Time),11,12,13,14,15
Tue Feb 11 2020 09:40:00 GMT-0700 (Mountain Standard Time),12,13,14,15,16
Tue Feb 11 2020 10:00:00 GMT-0700 (Mountain Standard Time),13,14,15,16,17
Tue Feb 11 2020 10:20:00 GMT-0700 (Mountain Standard Time),14,15,16,17,18
Tue Feb 11 2020 10:40:00 GMT-0700 (Mountain Standard Time),15,16,17,18,19
Tue Feb 11 2020 11:00:00 GMT-0700 (Mountain Standard Time),16,17,18,19,20
Tue Feb 11 2020 11:20:00 GMT-0700 (Mountain Standard Time),17,18,19,20,21
Tue Feb 11 2020 11:40:00 GMT-0700 (Mountain Standard Time),18,19,20,21,22
Tue Feb 11 2020 12:00:00 GMT-0700 (Mountain Standard Time),19,20,21,22,23
Tue Feb 11 2020 12:20:00 GMT-0700 (Mountain Standard Time),20,21,22,23,24
Tue Feb 11 2020 12:40:00 GMT-0700 (Mountain Standard Time),21,22,23,24,25
Tue Feb 11 2020 13:00:00 GMT-0700 (Mountain Standard Time),22,23,24,25,26
Tue Feb 11 2020 13:20:00 GMT-0700 (Mountain Standard Time),23,24,25,26,27
Tue Feb 11 2020 13:40:00 GMT-0700 (Mountain Standard Time),24,25,26,27,28
Tue Feb 11 2020 14:00:00 GMT-0700 (Mountain Standard Time),25,26,27,28,29
Tue Feb 11 2020 14:20:00 GMT-0700 (Mountain Standard Time),26,27,28,29,30
Tue Feb 11 2020 14:40:00 GMT-0700 (Mountain Standard Time),27,28,29,30,31
Tue Feb 11 2020 15:00:00 GMT-0700 (Mountain Standard Time),28,29,30,31,32
Tue Feb 11 2020 15:20:00 GMT-0700 (Mountain Standard Time),29,30,31,32,33
Tue Feb 11 2020 15:40:00 GMT-0700 (Mountain Standard Time),30,31,32,33,34
Tue Feb 11 2020 16:00:00 GMT-0700 (Mountain Standard Time),31,32,33,34,35
Tue Feb 11 2020 16:20:00 GMT-0700 (Mountain Standard Time),32,33,34,35,36
Tue Feb 11 2020 16:40:00 GMT-0700 (Mountain Standard Time),33,34,35,36,37
Tue Feb 11 2020 17:00:00 GMT-0700 (Mountain Standard Time),34,35,36,37,38
Tue Feb 11 2020 17:20:00 GMT-0700 (Mountain Standard Time),35,36,37,38,39
Tue Feb 11 2020 17:40:00 GMT-0700 (Mountain Standard Time),36,37,38,39,40
Tue Feb 11 2020 18:00:00 GMT-0700 (Mountain Standard Time),37,38,39,40,41
Tue Feb 11 2020 18:20:00 GMT-0700 (Mountain Standard Time),38,39,40,41,42
Tue Feb 11 2020 18:40:00 GMT-0700 (Mountain Standard Time),39,40,41,42,43
Tue Feb 11 2020 19:00:00 GMT-0700 (Mountain Standard Time),40,41,42,43,44
Tue Feb 11 2020 19:20:00 GMT-0700 (Mountain Standard Time),41,42,43,44,45
Tue Feb 11 2020 19:40:00 GMT-0700 (Mountain Standard Time),42,43,44,45,46
Tue Feb 11 2020 20:00:00 GMT-0700 (Mountain Standard Time),43,44,45,46,47
Tue Feb 11 2020 20:20:00 GMT-0700 (Mountain Standard Time),44,45,46,47,48
Tue Feb 11 2020 20:40:00 GMT-0700 (Mountain Standard Time),45,46,47,48,49
Tue Feb 11 2020 21:00:00 GMT-0700 (Mountain Standard Time),46,47,48,49,50
Tue Feb 11 2020 21:20:00 GMT-0700 (Mountain Standard Time),47,48,49,50,51
Tue Feb 11 2020 21:40:00 GMT-0700 (Mountain Standard Time),48,49,50,51,52
Tue Feb 11 2020 22:00:00 GMT-0700 (Mountain Standard Time),49,50,51,52,53
Tue Feb 11 2020 22:20:00 GMT-0700 (Mountain Standard Time),50,51,52,53,54
Tue Feb 11 2020 22:40:00 GMT-0700 (Mountain Standard Time),51,52,53,54,55
Tue Feb 11 2020 23:00:00 GMT-0700 (Mountain Standard Time),52,53,54,55,56
Tue Feb 11 2020 23:20:00 GMT-0700 (Mountain Standard Time),53,54,55,56,57
Tue Feb 11 2020 23:40:00 GMT-0700 (Mountain Standard Time),54,55,56,57,58
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),55,56,57,58,59
Wed Feb 12 2020 00:20:00 GMT-0700 (Mountain Standard Time),56,57,58,59,60
Wed Feb 12 2020 00:40:00 GMT-0700 (Mountain Standard Time),57,58,59,60,61
Wed Feb 12 2020 01:00:00 GMT-0700 (Mountain Standard Time),58,59,60,61,62
Wed Feb 12 2020 01:20:00 GMT-0700 (Mountain Standard Time),59,60,61,62,63
Wed Feb 12 2020 01:40:00 GMT-0700 (Mountain Standard Time),60,61,62,63,64
Wed Feb 12 2020 02:00:00 GMT-0700 (Mountain Standard Time),61,62,63,64,65
Wed Feb 12 2020 02:20:00 GMT-0700 (Mountain Standard Time),62,63,64,65,66
Wed Feb 12 2020 02:40:00 GMT-0700 (Mountain Standard Time),63,64,65,66,67
Wed Feb 12 2020 03:00:00 GMT-0700 (Mountain Standard Time),64,65,66,67,68
Wed Feb 12 2020 03:20:00 GMT-0700 (Mountain Standard Time),65,66,67,68,69
Wed Feb 12 2020 03:40:00 GMT-0700 (Mountain Standard Time),66,67,68,69,70
Wed Feb 12 2020 04:00:00 GMT-0700 (Mountain Standard Time),67,68,69,70,71
Wed Feb 12 2020 04:20:00 GMT-0700 (Mountain Standard Time),68,69,70,71,72
Wed Feb 12 2020 04:40:00 GMT-0700 (Mountain Standard Time),69,70,71,72,73
Wed Feb 12 2020 05:00:00 GMT-0700 (Mountain Standard Time),70,71,72,73,74
Wed Feb 12 2020 05:20:00 GMT-0700 (Mountain Standard Time),71,72,73,74,75
Wed Feb 12 2020 05:40:00 GMT-0700 (Mountain Standard Time),72,73,74,75,76
Wed Feb 12 2020 06:00:00 GMT-0700 (Mountain Standard Time),73,74,75,76,77
Wed Feb 12 2020 06:20:00 GMT-0700 (Mountain Standard Time),74,75,76,77,78
Wed Feb 12 2020 06:40:00 GMT-0700 (Mountain Standard Time),75,76,77,78,79
Wed Feb 12 2020 07:00:00 GMT-0700 (Mountain Standard Time),76,77,78,79,80
Wed Feb 12 2020 07:20:00 GMT-0700 (Mountain Standard Time),77,78,79,80,81
Wed Feb 12 2020 07:40:00 GMT-0700 (Mountain Standard Time),78,79,80,81,82
Wed Feb 12 2020 08:00:00 GMT-0700 (Mountain Standard Time),79,80,81,82,83
Wed Feb 12 2020 08:20:00 GMT-0700 (Mountain Standard Time),80,81,82,83,84
Wed Feb 12 2020 08:40:00 GMT-0700 (Mountain Standard Time),81,82,83,84,85
Wed Feb 12 2020 09:00:00 GMT-0700 (Mountain Standard Time),82,83,84,85,86
Wed Feb 12 2020 09:20:00 GMT-0700 (Mountain Standard Time),83,84,85,86,87
Wed Feb 12 2020 09:40:00 GMT-0700 (Mountain Standard Time),84,85,86,87,88
Wed Feb 12 2020 10:00:00 GMT-0700 (Mountain Standard Time),85,86,87,88,89
Wed Feb 12 2020 10:20:00 GMT-0700 (Mountain Standard Time),86,87,88,89,90
Wed Feb 12 2020 10:40:00 GMT-0700 (Mountain Standard Time),87,88,89,90,91
Wed Feb 12 2020 11:00:00 GMT-0700 (Mountain Standard Time),88,89,90,91,92
Wed Feb 12 2020 11:20:00 GMT-0700 (Mountain Standard Time),89,90,91,92,93
Wed Feb 12 2020 11:40:00 GMT-0700 (Mountain Standard Time),90,91,92,93,94
Wed Feb 12 2020 12:00:00 GMT-0700 (Mountain Standard Time),91,92,93,94,95
Wed Feb 12 2020 12:20:00 GMT-0700 (Mountain Standard Time),92,93,94,95,96
Wed Feb 12 2020 12:40:00 GMT-0700 (Mountain Standard Time),93,94,95,96,97
Wed Feb 12 2020 13:00:00 GMT-0700 (Mountain Standard Time),94,95,96,97,98
Wed Feb 12 2020 13:20:00 GMT-0700 (Mountain Standard Time),95,96,97,98,99
Wed Feb 12 2020 13:40:00 GMT-0700 (Mountain Standard Time),96,97,98,99,100
Wed Feb 12 2020 14:00:00 GMT-0700 (Mountain Standard Time),97,98,99,100,101
Wed Feb 12 2020 14:20:00 GMT-0700 (Mountain Standard Time),98,99,100,101,102
Wed Feb 12 2020 14:40:00 GMT-0700 (Mountain Standard Time),99,100,101,102,103
Wed Feb 12 2020 15:00:00 GMT-0700 (Mountain Standard Time),100,101,102,103,104
Wed Feb 12 2020 15:20:00 GMT-0700 (Mountain Standard Time),101,102,103,104,105
Wed Feb 12 2020 15:40:00 GMT-0700 (Mountain Standard Time),102,103,104,105,106
Wed Feb 12 2020 16:00:00 GMT-0700 (Mountain Standard Time),103,104,105,106,107
Wed Feb 12 2020 16:20:00 GMT-0700 (Mountain Standard Time),104,105,106,107,108
Wed Feb 12 2020 16:40:00 GMT-0700 (Mountain Standard Time),105,106,107,108,109
Wed Feb 12 2020 17:00:00 GMT-0700 (Mountain Standard Time),106,107,108,109,110
Wed Feb 12 2020 17:20:00 GMT-0700 (Mountain Standard Time),107,108,109,110,111
Wed Feb 12 2020 17:40:00 GMT-0700 (Mountain Standard Time),108,109,110,111,112
Wed Feb 12 2020 18:00:00 GMT-0700 (Mountain Standard Time),109,110,111,112,113
Wed Feb 12 2020 18:20:00 GMT-0700 (Mountain Standard Time),110,111,112,113,114
Wed Feb 12 2020 18:40:00 GMT-0700 (Mountain Standard Time),111,112,113,114,115
Wed Feb 12 2020 19:00:00 GMT-0700 (Mountain Standard Time),112,113,114,115,116
Wed Feb 12 2020 19:20:00 GMT-0700 (Mountain Standard Time),113,114,115,116,117
Wed Feb 12 2020 19:40:00 GMT-0700 (Mountain Standard Time),114,115,116,117,118
Wed Feb 12 2020 20:00:00 GMT-0700 (Mountain Standard Time),115,116,117,118,119
Wed Feb 12 2020 20:20:00 GMT-0700 (Mountain Standard Time),116,117,118,119,120
Wed Feb 12 2020 20:40:00 GMT-0700 (Mountain Standard Time),117,118,119,120,121
Wed Feb 12 2020 21:00:00 GMT-0700 (Mountain Standard Time),118,119,120,121,122
Wed Feb 12 2020 21:20:00 GMT-0700 (Mountain Standard Time),119,120,121,122,123
Wed Feb 12 2020 21:40:00 GMT-0700 (Mountain Standard Time),120,121,122,123,124
Wed Feb 12 2020 22:00:00 GMT-0700 (Mountain Standard Time),121,122,123,124,125
Wed Feb 12 2020 22:20:00 GMT-0700 (Mountain Standard Time),122,123,124,125,126
Wed Feb 12 2020 22:40:00 GMT-0700 (Mountain Standard Time),123,124,125,126,127
Wed Feb 12 2020 23:00:00 GMT-0700 (Mountain Standard Time),124,125,126,127,128
Wed Feb 12 2020 23:20:00 GMT-0700 (Mountain Standard Time),125,126,127,128,129
Wed Feb 12 2020 23:40:00 GMT-0700 (Mountain Standard Time),126,127,128,129,130
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),127,128,129,130,131
Thu Feb 13 2020 00:20:00 GMT-0700 (Mountain Standard Time),128,129,130,131,132
Thu Feb 13 2020 00:40:00 GMT-0700 (Mountain Standard Time),129,130,131,132,133
Thu Feb 13 2020 01:00:00 GMT-0700 (Mountain Standard Time),130,131,132,133,134
Thu Feb 13 2020 01:20:00 GMT-0700 (Mountain Standard Time),131,132,133,134,135
Thu Feb 13 2020 01:40:00 GMT-0700 (Mountain Standard Time),132,133,134,135,136
Thu Feb 13 2020 02:00:00 GMT-0700 (Mountain Standard Time),133,134,135,136,137
Thu Feb 13 2020 02:20:00 GMT-0700 (Mountain Standard Time),134,135,136,137,138
Thu Feb 13 2020 02:40:00 GMT-0700 (Mountain Standard Time),135,136,137,138,139
Thu Feb 13 2020 03:00:00 GMT-0700 (Mountain Standard Time),136,137,138,139,140
Thu Feb 13 2020 03:20:00 GMT-0700 (Mountain Standard Time),137,138,139,140,141
Thu Feb 13 2020 03:40:00 GMT-0700 (Mountain Standard Time),138,139,140,141,142
Thu Feb 13 2020 04:00:00 GMT-0700 (Mountain Standard Time),139,140,141,142,143
Thu Feb 13 2020 04:20:00 GMT-0700 (Mountain Standard Time),140,141,142,143,144
Thu Feb 13 2020 04:40:00 GMT-0700 (Mountain Standard Time),141,142,143,144,145
Thu Feb 13 2020 05:00:00 GMT-0700 (Mountain Standard Time),142,143,144,145,146
Thu Feb 13 2020 05:20:00 GMT-0700 (Mountain Standard Time),143,144,145,146,147
Thu Feb 13 2020 05:40:00 GMT-0700 (Mountain Standard Time),144,145,146,147,148
Thu Feb 13 2020 06:00:00 GMT-0700 (Mountain Standard Time),145,146,147,148,149
Thu Feb 13 2020 06:20:00 GMT-0700 (Mountain Standard Time),146,147,148,149,150
Thu Feb 13 2020 06:40:00 GMT-0700 (Mountain Standard Time),147,148,149,150,151
Thu Feb 13 2020 07:00:00 GMT-0700 (Mountain Standard Time),148,149,150,151,152
Thu Feb 13 2020 07:20:00 GMT-0700 (Mountain Standard Time),149,150,151,152,153
Thu Feb 13 2020 07:40:00 GMT-0700 (Mountain Standard Time),150,151,152,153,154
Thu Feb 13 2020 08:00:00 GMT-0700 (Mountain Standard Time),151,152,153,154,155
Thu Feb 13 2020 08:20:00 GMT-0700 (Mountain Standard Time),152,153,154,155,156
Thu Feb 13 2020 08:40:00 GMT-0700 (Mountain Standard Time),153,154,155,156,157
Thu Feb 13 2020 09:00:00 GMT-0700 (Mountain Standard Time),154,155,156,157,158
Thu Feb 13 2020 09:20:00 GMT-0700 (Mountain Standard Time),155,156,157,158,159
Thu Feb 13 2020 09:40:00 GMT-0700 (Mountain Standard Time),156,157,158,159,160
Thu Feb 13 2020 10:00:00 GMT-0700 (Mountain Standard Time),157,158,159,160,161
Thu Feb 13 2020 10:20:00 GMT-0700 (Mountain Standard Time),158,159,160,161,162
Thu Feb 13 2020 10:40:00 GMT-0700 (Mountain Standard Time),159,160,161,162,163
Thu Feb 13 2020 11:00:00 GMT-0700 (Mountain Standard Time),160,161,162,163,164
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...