NiphitAe
NiphitAe

Reputation: 405

Add future date in Google script and Goggle Sheet

I use Google sheet and Cryptofinance.ai to retreive cryptocurrency prices. I have one script that works well : it appends data periodically from one row in tab A to save them in a tab B so I can make graphs and charts.

Now I'd like to add date + 1 year in the next row. Idealy, each time the script is triggered, it make two new rows : The one with the data as it is now and the one with the date + 1 year.

If you curious and want to know why I want to do that is is to make projection price using this formula in another tab : =TREND(filter(B2:B,B2:B<>""),filter(A2:A,B2:B<>""),filter(A2:A,(N(B2:B)=0)*(A2:A>0)))

Here is my script now:

    // [START modifiable parameters]
    var rangeToLog = 'Portefeuille!A28:L28';
    var sheetToLogTo = 'Archive BTC/USD KRAKEN';
    // [END modifiable parameters]
    ////////////////////////////////
    
    /**
    * Appends a range of values to the end of an archive sheet.
    * A timestamp is inserted in column A of each row on the archive sheet.
    * All values in rangeToLog go to one row on the archive sheet.
    *
    * @OnlyCurrentDoc
    */
    function appendValuesToArchiveBTCUSDKRAKENSheet() {
      // version 1.4, written by --Hyde, 30 January 2020
      //  - use Array.prototype.some() to skip empty rows when concating
      //  - see https://support.google.com/docs/thread/27095918?msgid=27148911
      // version 1.3, written by --Hyde, 26 January 2020
      //  - see https://support.google.com/docs/thread/26760916
      var ss = SpreadsheetApp.getActive();
      var valuesToLog = ss.getRange(rangeToLog).getValues();
      var logSheet = ss.getSheetByName(sheetToLogTo);
      if (!logSheet) {
        logSheet = ss.insertSheet(sheetToLogTo);
        logSheet.appendRow(['Date time', 'Data']);
      }
      var rowToAppend = [new Date()].concat(
        valuesToLog.reduce(function concatArrays_(left, right) {
          var arrayContainsData = right.some(function isNonBlanky_(element, index, array) {
            return element !== null && element !== undefined && element !== '';
          });
          return arrayContainsData ? left.concat(right) : left;
        })
      );
      logSheet.appendRow(rowToAppend);
    }

NOW :

Each time the script is triggered, it adds a new line.

What I want to do:

Same but it also appends a date in the future

Upvotes: 0

Views: 498

Answers (2)

NiphitAe
NiphitAe

Reputation: 405

Finally, after some research I came to this :

function expCalc(){
 delLastNRows();
 appendValuesToArchiveBTCUSDKRAKENSheet();
}



function delLastNRows(n){
  var n=n || 1;//allows you to delete last three rows without passing function a parameter.
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Archive BTC/USD KRAKEN');
  var lr=sh.getLastRow();
  if(lr>=n){
    for(var i=0;i<n;i++){
      sh.deleteRow(sh.getLastRow());
    }
  }
}


// [START modifiable parameters]
var rangeToLog = 'Portefeuille!A28:L28';
var sheetToLogTo = 'Archive BTC/USD KRAKEN';
// [END modifiable parameters]
////////////////////////////////

/**
* Appends a range of values to the end of an archive sheet.
* A timestamp is inserted in column A of each row on the archive sheet.
* All values in rangeToLog go to one row on the archive sheet.
*
* @OnlyCurrentDoc
*/

function appendValuesToArchiveBTCUSDKRAKENSheet() {
  // version 1.4, written by --Hyde, 30 January 2020
  //  - use Array.prototype.some() to skip empty rows when concating
  //  - see https://support.google.com/docs/thread/27095918?msgid=27148911
  // version 1.3, written by --Hyde, 26 January 2020
  //  - see https://support.google.com/docs/thread/26760916
  var ss = SpreadsheetApp.getActive();
  var valuesToLog = ss.getRange(rangeToLog).getValues();
  var logSheet = ss.getSheetByName(sheetToLogTo);
  var sheet = ss.getSheets()[0]
  if (!logSheet) {
    logSheet = ss.insertSheet(sheetToLogTo);
    logSheet.appendRow(['Date time', 'Data']);
  }
  var rowToAppend = [new Date()].concat(
    valuesToLog.reduce(function concatArrays_(left, right) {
      var arrayContainsData = right.some(function isNonBlanky_(element, index, array) {
        return element !== null && element !== undefined && element !== '';
      });
      return arrayContainsData ? left.concat(right) : left;
    })
  );
  logSheet.appendRow(rowToAppend);

  logSheet.appendRow([new Date(new Date().setFullYear(new Date().getFullYear() + 1))]);
}

Works like a charm !

Upvotes: 0

Diego
Diego

Reputation: 9571

The easy fix is to simply add another appendRow() call at the end of your function with the one year from now value.

function appendValuesToArchiveBTCUSDKRAKENSheet() {
  // ... 
  logSheet.appendRow(rowToAppend);
  logSheet.appendRow([new Date(new Date().setFullYear(new Date().getFullYear() + 1))]);
}

A more complex solution, but with better execution time, would have you print both rows in a single setValues() call. This follows the best practice of using batch operations, but I suspect that the easier solution above is adequate for your purpose. I do encourage you, however, to try implementing the batch operation if you want to improve your apps script skills.

Upvotes: 2

Related Questions