1

I am writing a code to count the number of times a time duration in column B is repeated.After writing a code according to my logic I am still getting error or no output. Here's the app script code I have written. Help me out.

function count() {
    var data = SpreadsheetApp.getActive();
    var sheet = data.getSheetByName("Sheet4");
    var lastRow = sheet.getLastRow();
    var sh = sheet.getRange('B1:B' + lastRow);
    var cell = sh.getValues();
    var count = 0;
    for (var i = 0; i < lastRow; j++) {
        var column2 = cell[i][1];
        for (var j = 0; j < i; j++) {
            var column4 = cell[j][1];
            if (column4 == column2) {
                count++;
            }
        }
        sheet.getRange('F' + (i + 1).toString()).setValue(count);
    }
}

input data

This is the intended output instead of column F i put the sample output in column C sample output

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
Kumara Guru
  • 181
  • 1
  • 10
  • Can you provide any example data with your question? – mhovd Dec 21 '19 at 17:19
  • I have attached a image above check it – Kumara Guru Dec 22 '19 at 05:03
  • Does this answer your question? [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – TheMaster Dec 22 '19 at 05:52
  • I want to know how many times a value in column B has repeated and display it in column F – Kumara Guru Dec 22 '19 at 06:18
  • Can you more clearly specify what you intend the output to be? It's difficult to figure out what exactly you're trying to do. The most effective way of communicating this would be to provide a small snippet of input and expected output. This should be presented as text and not as images. Please see [the instructions on asking good questions](https://stackoverflow.com/help/how-to-ask). – chuckx Dec 22 '19 at 07:49
  • @KumaraGuru you are not providing a sample output, we need to see exactly what you want the output to look like, even if you have to hardcode type it. – CodeCamper Dec 22 '19 at 08:00
  • I am getting output as zero in column F1 only, remaining all cells in F has no values – Kumara Guru Dec 22 '19 at 09:07
  • *I want to know how many times a value in column B has repeated and display it in column F* If this is addressed to me(use @ to notify me or others like @TheMaster), Did you read the answer in the question I linked? – TheMaster Dec 22 '19 at 13:51

1 Answers1

0

Issue(s):

  • Outer loop's increment variable doesn't change.

      for (var i = 0; i < lastRow;/*i never changes =>*/ j++) 
    
  • Date/Time are retrieved as JavaScript objects and cannot be compared with ==:

      if (column4 == column2) {//Date objects cannot be compared
    
  • cell doesn't have a value at array index [1]

Solution:

  • Increment all variables in the for -loop
  • Compare value of date objects instead
  • Array indexes start at [0]

Snippet:

for (var i = 0; i < lastRow; ++i) {
var column2 = cell[i][0];
/*....*/
  var column4 = cell[j][0];
  if (column4 - column2 === 0) {//@see https://stackoverflow.com/questions/492994
Community
  • 1
  • 1
TheMaster
  • 45,448
  • 6
  • 62
  • 85