# 간트 시트 가져오기/내보내기

## Content

간트시트를 JSON, SJS, Excel 등 다양한 형식으로 가져오고 내보낼 수 있습니다. SpreadJS는 인쇄 및 PDF 형식 내보내기도 지원합니다.
간트시트 IO 기능(JSON 직렬화/역직렬화 제외)을 지원하려면, IO 작업에 따라 특정 스크립트 파일을 참조에 추가해야 합니다. 예:

* \<script src="scripts/gc.spread.sheets.tablesheet.x.x.x.min.js">\</script>
* \<script src="scripts/gc.spread.sheets.ganttsheet.x.x.x.min.js">\</script>
* \<script src="scripts/gc.spread.sheets.print.x.x.x.min.js">\</script>
* \<script src="scripts/gc.spread.sheets.pdf.x.x.x.min.js">\</script>
* \<script src="scripts/gc.spread.sheets.io.x.x.x.min.js">\</script>

## 열기 및 가져오기

간트시트 JSON 직렬화 및 역직렬화에는 **Workbook** 클래스의 **toJSON** 및 **fromJSON** 메서드를 사용해야 합니다. 간트시트 JSON 데이터는 **workbookJSONData.sheetTabs**에 저장되며, 간트시트의 지정된 속성은 **ganttSheetTabData.gantt**에 저장됩니다. 또한 **sheetType** 속성은 **fromJSON** 과정에서 테이블 시트와 간트 시트를 구분하는 데 필요합니다.
여러 파일 형식을 열고 가져올 수 있습니다. SJS 파일 형식을 열려면 `spread.open()` 메서드와 **openOptions**를 사용합니다. 파일 형식이 SSJson 또는 Excel일 경우 `spread.import()` 메서드를 사용하고, 각각 **importSSJsonOptions** 또는 **importXlsxOptions**를 지정합니다.
아래 코드는 Spread 컨트롤에서 파일을 열고 가져오는 방법을 보여줍니다.

```javascript
function openFile(e) {
    var fileNameItem = document.getElementById("file-name");
    var fileInput = document.getElementById("file-input");
    fileNameItem.value = 'Loading file...';
    var file = fileInput.files[0];
    if (!file) {
        alert("Upload a file first.");
        return;
    }

    const fileName = file.name;
    const extensionName = fileName.substring(fileName.lastIndexOf(".") + 1);

    // SJS 파일 열기 옵션 정의
    const openOptions = {
        "openMode": GC.Spread.Sheets.OpenMode.normal,
        "includeStyles": true,
        "includeFormulas": true,
        "fullRecalc": false,
        "dynamicReferences": true,
        "calcOnDemand": false,
        "includeUnusedStyles": true
    }

    // SSJson 파일 가져오기 옵션 정의
    const importSSJsonOptions = {
        "includeStyles": true,
        "includeFormulas": true,
        "frozenColumnsAsRowHeaders": false,
        "frozenRowsAsColumnHeaders": false,
        "fullRecalc": false,
        "incrementalLoading": false
    }

    // Excel 파일 가져오기 옵션 정의
    const importXlsxOptions = {
        "openMode": GC.Spread.Sheets.OpenMode.normal,
        "includeStyles": true,
        "includeFormulas": true,
        "frozenColumnsAsRowHeaders": false,
        "frozenRowsAsColumnHeaders": false,
        "fullRecalc": false,
        "dynamicReferences": true,
        "calcOnDemand": false,
        "includeUnusedStyles": true,
        "password": ""
    }

    // SJS 파일 가져오기
    if (extensionName === 'sjs') {
        spread.open(file, function () {
            fileNameItem.value = fileName;
            fileInput.value = '';
        }, function () { }, openOptions);
    }

    // SSJson 파일 가져오기
    else if (extensionName === 'ssjson') {
        spread.import(file, function () {
            fileNameItem.value = fileName;
            fileInput.value = '';
        }, function () { }, importSSJsonOptions);
    }

    // Excel 파일 가져오기
    else if (extensionName === 'xlsx') {
        spread.import(file, function () {
            fileNameItem.value = fileName;
            fileInput.value = '';
        }, function () { }, importXlsxOptions);
    }
}
```

## 저장 및 내보내기

간트시트 데이터를 SJS 형식으로 저장하려면 `spread.save()` 메서드를 사용하고 필요한 **saveOptions**를 지정해야 합니다. 파일 형식이 SSJson 또는 Excel일 경우, `spread.export()` 메서드를 사용하고 **exportSSJsonOptions** 또는 **exportXlsxOptions**를 지정합니다.

> **참고**:
>
> * 내보내기 시 직렬화 옵션 **includeBindingSource**가 true로 설정되어 있는지 확인하세요.
> * 내보내기 시 간트 시트 데이터 열은 테이블로, 간트 차트 열은 Excel에서 그림으로 내보내집니다.

아래 코드는 Spread 컨트롤에서 파일을 저장하고 내보내는 방법을 보여줍니다.

```javascript
function exportFile(fileName) {
    const extensionName = fileName.substring(fileName.lastIndexOf(".") + 1);

    // SSJson 파일 내보내기 옵션 정의
    const exportSSJsonOptions = {
        "includeBindingSource": true,
        "includeStyles": true,
        "includeFormulas": true,
        "saveAsView": false,
        "rowHeadersAsFrozenColumns": false,
        "columnHeadersAsFrozenRows": false,
        "includeAutoMergedCells": false,
        "fileType": GC.Spread.Sheets.FileType.ssjson
    }

    // SJS 파일 저장 옵션 정의
    const saveOptions = {
        "includeBindingSource": true,
        "includeStyles": true,
        "includeFormulas": true,
        "saveAsView": false,
        "includeAutoMergedCells": false,
        "includeCalcModelCache": false,
        "includeUnusedNames": true,
        "includeEmptyRegionCells": true
    }

    // Excel 파일 내보내기 옵션 정의
    const exportXlsxOptions = {
        "includeBindingSource": true,
        "includeStyles": true,
        "includeFormulas": true,
        "saveAsView": false,
        "rowHeadersAsFrozenColumns": false,
        "columnHeadersAsFrozenRows": false,
        "includeAutoMergedCells": false,
        "includeCalcModelCache": false,
        "includeUnusedNames": true,
        "includeEmptyRegionCells": true,
        "fileType": GC.Spread.Sheets.FileType.excel
    }

    // SJS 파일 내보내기
    if (extensionName === 'sjs') {
        spread.save(function (blob) { saveAs(blob, fileName); }, function () { }, saveOptions);
    }

    // SSJson 파일 내보내기
    else if (extensionName === 'ssjson') {
        spread.export(function (blob) { saveAs(blob, fileName); }, function () { }, exportSSJsonOptions);
    }

    // Excel 파일 내보내기
    else if (extensionName === 'xlsx') {
        spread.export(function (blob) { saveAs(blob, fileName); }, function () { }, exportXlsxOptions);
    }
}
```

## PDF 저장

워크북의 `savePDF()` 메서드를 사용해 간트 시트를 PDF로 내보낼 수 있습니다.
아래 코드는 Spread 컨트롤에서 PDF 파일을 내보내는 방법을 보여줍니다.

```javascript
function ExportToPDF(spread) {
    spread.savePDF(function (blob) {
        var url = URL.createObjectURL(blob);
        pwin = window.open(url);
    });
}
```

## 간트 시트 인쇄

간트 시트 인쇄는 **Workbook** 클래스의 `print()` 메서드로 수행합니다. 일반적으로 간트 차트 열은 크기가 크기 때문에 한 페이지에 인쇄되지 않고, 마지막 일반 열 근처에 출력됩니다.
아래 코드는 Spread 컨트롤에서 간트 시트를 인쇄하는 방법을 보여줍니다.

```javascript
function functionPrint(spread) {
    spread.print();
}
```