简体   繁体   中英

JavaScript, Google Sheets script - problem with date comparing

I am new in Apps Script, and had never a chance previously to write in JavaScript. So, I have a dataset in one sheet in Google about personal information of clients (ID, name, mail, contact). I have another dataset in another Sheet about clients payment (date of payment of the first installment, date of payment of the second installment and amount. The last sheet that is in focus is the third one relating to unpaid bills. Now, what I want is to copy columns about personal information from the first sheet into the third one. Here condition would be to copy only information about clients where difference between date of payment of the second installment and today() is greater than 45. Here is what I have tried:


function Neplacena2Novi() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetFrom=ss.getSheetByName("Podaci o polaznicima"); //Data entry Sheet
var sheetTo=ss.getSheetByName("Neplaceni racuni"); //Data Sheet
var condition=ss.getSheetByName("Uplate novoupisani");
var range1=ss.getRangeByName("Prva rata");
var range2=ss.getRangeByName("Druga rata");


var startRow=3;
var endRow=ss.getLastRow();

function Neplacena2Novi(){
  for var(i=startRow; i<(endRow+1)< i++) {
  var startDate=new Date(condition.getRange(range1+1).getValue());
  var endDate= new Date(condition.getRange(range2+1).getValues());
  
  var difference=parseInt(endDate-startDate)/1000); //Dates are in format dd.mm.yyyy.
      if(difference>45){

      var values=sheetFrom.getRange(2, 1, sheetFrom.getLastRow(), 4).getValues();

      sheetTo.getRange(3,1,1184,4).setValues(values);
      }
}
var ui = SpreadsheetApp.getUi();
var potvrda = ui.alert("Odredili ste lica koja treba da se pozovu.");
}


function DugmeDodajKlijenta (){
var ui = SpreadsheetApp.getUi();

}

This code works but I didn't get anything in sheet. And I didn't now how to include today() function in formula for difference. But what I really want is to make a condition where difference would be today()-startDate().

Can someone please help me?

The code is not working because endDate is not a primitive value but a 2D array. Use Range.getValue() instead of Range.getValues() , like this:

  const startDate = new Date(condition.getRange(row, startDateColumn).getValue());
  const endDate = new Date(condition.getRange(row, endDateColumn).getValue());

...but that is not enough to make the code work. There is a syntactical error in the for loop line, and the i variable is not referenced in the loop. The whole thing is very inefficient because it reads and writes data row by row instead of doing just one read and just one write.

The new Date() calls are superfluous unless the "dates" in the spreadsheet are not numeric dates but text strings that just look like dates. The parseInt() call is also superfluous (plus, it is missing the radix parameter.)

JavaScript dates internally contain the number of milliseconds that have elapsed since 1 January 1970 0:00 UTC. To get the difference between two dates in days, try (endDate-startDate) / 1000 / 60 / 60 / 24 . Note that there are many caveats — see Date .

The whole loop could be replaced with Range.getValues().filter() . It looks like the code should best be totally rewritten.

It is unclear why you would need a script in the first place, since the same can be done with a spreadsheet formula using eg filter() or query() . The personal info can be added to the missing payments data with a lookup eg arrayformula(vlookup()) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM