# migrate sheets into files

{% tabs %}
{% tab title="💈範例" %}
```javascript
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);
    };

  };
};
```
{% endtab %}

{% tab title="🗣 討論" %}
* [How to split a single spread sheet with 20 tabs into different files](https://stackoverflow.com/a/40557953/5409815)
{% endtab %}
{% endtabs %}
