title: How to create a counter in google sheets that would count how many times a calculated cell has changed value?
body: <p>I am a beginner and need to write a script that will count the number of times that a cell, the value in which is formula-calculated (no user input) changed value over a day and then at midnight reset the count back to 0</p>
  
  <p>I have tried to use the onEdit code, but it only works with user input changes and doesn't take into account when a formula changes the value of a cell, based on other values</p>
  
  <p>Here is a link to a sample spreadsheet - <a href="https://docs.google.com/spreadsheets/d/1QfM764_cPbtN46oDSbGBFgVNYZ1R6dwxIs744cyRsS4/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1QfM764_cPbtN46oDSbGBFgVNYZ1R6dwxIs744cyRsS4/edit?usp=sharing</a></p>
  
  <p>The column H has the Signal (Buy or Sell) which is calculated based on the prices and can change throughout the day. 
  The code should put the number of times the signal was on Buy or Sell throughout the day, and then reset at days end.</p>
  
  <p>Any help would be really appreciated since I am very stuck on this.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have come up with this code, which works on one row of data:</p>
  
  <pre><code>function Counter(e) {  
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var s = ss.getSheetByName("Counter");
    var r = s.getRange("H2");
    var v= r.getValue();
    var h = s.getRange("N2").getValue();
      if(v !== h) {
        var sCounter = s.getRange("I2");
        var timestamprange = s.getRange("J2")
        var formattedDate = Utilities.formatDate(new Date(), "GMT+5", "yyyy-MM-dd HH:mm:ss");
  
        var counter = sCounter.getValue();
  
        if(counter === 0) {
            counter = 1;
            } else {
              counter ++;
            }
            sCounter.setValue(counter);
            timestamprange.setValue(formattedDate)
        ;}
   s.getRange(2, 14).setValue(v)
  }
  </code></pre>
  
  <p>How can I adjust the code to make the same for the ranges "H2:H51", "I2:I51", "J2:J51" ? </p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I tried on my own and came up with this code, which doesn´t work:</p>
  
  <pre><code>function Test() {
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var s = ss.getSheetByName("Counter");
    var newvalues = s.getRange("H2:H51").getValues();
    var oldvalues = s.getRange("N2:N51").getValues();
  
    for(n=0;n&lt;newvalues.length;++n){
      var nv = newvalues[n][0] ; 
      var o = oldvalues[n][0] ;
      if(nv !== o) {
        var sCounter = s.getRange("I2:I51");
        var timestamprange = s.getRange("J2:J51");
        var formattedDate = Utilities.formatDate(new Date(), "GMT+5", "yyyy-MM-dd HH:mm:ss");
        var counter = sCounter.getValues();
        if(counter[n][0] === 0) {
          counter[n][0] = 1;
          } else {
              counter[n][0] ++;
            }
        sCounter.setValues(counter[n][0]);
        timestamprange.setValue(formattedDate);
      }
  
  }
    s.getRange("N2:N51").setValues(newvalues)
  }
  </code></pre>
  
  <p>Any ideas would be appreciated!</p>