Home > Net >  Appending a Google Sheet file with newest Google Drive .csv file located in a specific folder
Appending a Google Sheet file with newest Google Drive .csv file located in a specific folder

Time:09-28

I've got a plugin that works with Zapier to send a .csv (can have different format) file to a specific Google Drive folder with some order data from the day before, every 24h.

I would love to append my Google Sheet file using AppsScript with the data from the newest file in the folder.

Is such solution even possible? What sources would you recommend to try to write a code like this?

(I know I could do this with Zapier but that would require ridiculous plan to have it done :))

Thanks a lot.

CodePudding user response:

Something like this should help:

It gets the latest csv file in a folder and loads it in to the active sheet.

function getTheLatestFile() {
  const folder = DriveApp.getFolderById("folderid");
  const files = folder.getFilesByType(MimeType.CSV);
  const arr = [];
  while(files.hasNext) {
    let file = files.next();
    arr.push({name:file.getName(),id:file.getId(),date:file.getDateCreated()});
  }
  arr.sort((a,b)=> {
    return new Date(b.date).valueOf() - new Date(a.date).valueOf();
  })
  let vs = Utilities.parseCsv(DriveApp.getFileById(arr[0].id),getBlob().getDataAsString());
  let sh = SpreadsheetApp.getActiveSheet();
  sh.clearContents();
  sh.getRange(1,1,vs.length, vs[0].length).setValues(vs);
}
  • Related