Home > Enterprise >  Import Google Sheet from GDrive
Import Google Sheet from GDrive

Time:08-05

I'm trying to import the latest Google Sheet into my Master Google Sheet using the following code:

function sortfiles() {
  const folder = DriveApp.getFolderById("Folderid");
  const files = folder.getFilesByName("Filename");
  let fA = [];
  while (files.hasNext()) {
    let file = files.next();
    fA.push({ id: file.getId(), dv: file.getDateCreated() })
  }
  fA.sort((a, b) => {
    let vA = new Date(a.dv).valueOf();
    let vB = new Date(b.dv).valueOf();
    return vB-vA;
  });
  Logger.log(DriveApp.getFileById(fA[0].id).getBlob().getDataAsString());
}

Which is sorting which file is the latest one, but i'm stuck on the code to write it to my master sheet?

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Sheet1");
var data = (DriveApp.getFileById(fA[0].id).getBlob().getDataAsString())
var csvData = Utilities.parseCsv(data, ',');

CodePudding user response:

Try

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Sheet1");
var data = (DriveApp.getFileById(fA[0].id).getBlob().getDataAsString())
var csvData = Utilities.parseCsv(data, ',');
sh.getRange(1,1,csvData.length,csvData[0].length).setValues(csvData)

edit : if your file is a google sheet file

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("Sheet1");
  var data = SpreadsheetApp.openById(fA[0].id).getSheets()[0].getDataRange().getValues();
  sh.getRange(1, 1, data.length, data[0].length).setValues(data);
  • Related