migrate sheets into files

function migrateSheetsToFiles() {
  var mySheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var toFolderName = '';
  var i;
  for(i in mySheets){
    var currentSheet = mySheets[i];
    var oldData = currentSheet.getDataRange().getValues();
    var newFile = SpreadsheetApp.create(currentSheet.getName());
    var newId = newFile.getId();
    var newSheet = newFile.getSheets()[0]

    newSheet.getRange(1,1,oldData.length,oldData[0].length).setValues(oldData);
    newSheet.setName(newFile.getName());

    if(toFolderName != ''){
      var fileInDrive = DriveApp.getFileById(newId);
      fileInDrive.makeCopy(fileInDrive.getName(),DriveApp.getFoldersByName(toFolderName).next());
      fileInDrive.setTrashed(true);
    };

  };
};

Last updated