简体   繁体   中英

Older date in Spreadsheets Google Script

I am trying to get information of newly created files in a G drive folder. Everytime I run my program the output should refer only to new files in the folder. The D column will contain the creation dates. So I read the last value of the D column, let's call it DX; DX would have the creation date of the newest file (because I sorted them in the last line of the program). Then I compare DX to the creation dates of all the files in my folder. Only the files.creationDate > DX are the ones I need. I've managed to come up with:

  var folder = DriveApp.getFolderById('1gsXZWKSPxAsFiyuaMMNTkg6n_4p9HOeU');  
  var contents = folder.getFiles();
//get value of D2
  var control_cell = SpreadsheetApp.getActiveSheet().getRange('D2');
//get last non blank value in the D column and assign it to variable lastTimeCreated
  var values = SpreadsheetApp.getActiveSheet().getRange('D:D').getValues().flat().filter(String);
  var [lastTimeCreated] = values.slice(-1)
//If D2 is empty assign an old date to lastTimeCreated
  if (control_cell.isBlank()){
    lastTimeCreated = '1/10/2020 12:00:00'
  }
  
  var varFile;
  var varTime;
  var dataRows = [];
  let i = 0;

  while(contents.hasNext()){
    varFile = contents.next();
    varTime = varFile.getDateCreated().toLocaleString();

    if(new Date(varTime).getTime() >= new Date(lastTimeCreated).getTime() && varOwner.includes(actualSheetName)){
      dataRows[i] = [varTime];
      i++;
    }
    else{
      continue
    }
  }
  const sortedRows = dataRows.sort((a, b) => new Date(a[3]) - new Date(b[3]));

The problem is the last if with the new Date(varTime).getTime() >= new Date(lastTimeCreated).getTime() statement. I think lastTimeCreated can't be interpreted as a Date and as a result they can't be compared. I know varTime is not the problem because the last line of this program works perfectly. Maybe the problem is the format date of both dates (?). I've also tried with Date.parse() for both variables but it hasn't worked.

My spreadsheet looks like this: 问题中提到的电子表格示例

Any help is widely appreciated.

Edit The date format is this: 1/10/2020 12:00:00

Edit 2 As suggested, provided more information and added some more code.

Get the last n days of rows of the list

Column D is date

function getLastNDaysOfRows(n=2) {
  const ss = SpreadsheetApp.getActive();
  const ash = ss.getActiveSheet();
  const vs = ash.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
  let v = vs.slice();
  v.sort((a,b) => {
    let dta = new Date(a[3])
    let vA = new Date(dta.getFullYear(),dta.getMonth(),dta.getDate()).valueOf();
    let dtb = new Date(b[3]);
    let vB = new Date(dtb.getFullYear(),dtb.getMonth(),dtb.getDate()).valueOf();
    return vB-vA;
  });
  dtn = new Date(v[0][3]);
  dtvt = new Date(dtn.getFullYear(),dtn.getMonth(),dtn.getDate() - n).valueOf();
  let ds = vs.map(r => {
    let d = new Date(r[3])
    let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf()
    if(dv >= dtvt) {
      return r;
    }
  })
  Logger.log(JSON.stringify(ds));
}

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