# Spreadsheets, Sheets, and Ranges

## Access and Modify

### Rename the active spreadsheet

```javascript
function renameSpreadsheet() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  mySS.rename("2017 Avocado Prices in Portland, Seattle");
}
```

### Duplicate the active sheet

```javascript
function duplicateAndOrganizeActiveSheet() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var duplicateSheet = mySS.duplicateActiveSheet();
}
```

## [Format your sheet with the Sheet class](https://developers.google.com/codelabs/apps-script-fundamentals-2?hl=en\&continue=https%3A%2F%2Fcodelabs.developers.google.com%2F#4)

### Change the sheet's name <a href="#change-the-sheets-name" id="change-the-sheets-name"></a>

```javascript
function duplicateAndOrganizeActiveSheet() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var duplicateSheet = mySS.duplicateActiveSheet();

  // Change the name of the new sheet.
  duplicateSheet.setName("Sheet_" + duplicateSheet.getSheetId());
}
```

### Modify a sheet's columns and rows <a href="#modify-a-sheets-columns-and-rows" id="modify-a-sheets-columns-and-rows"></a>

```javascript
function duplicateAndOrganizeActiveSheet() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var duplicateSheet = mySS.duplicateActiveSheet();

  // Change the name of the new sheet.
  duplicateSheet.setName("Sheet_" + duplicateSheet.getSheetId());

  // Format the new sheet.
  duplicateSheet.autoResizeColumns(1, 5);
  duplicateSheet.setFrozenRows(2);
}
```

## [Rearrange data with the Range class](https://developers.google.com/codelabs/apps-script-fundamentals-2?hl=en\&continue=https%3A%2F%2Fcodelabs.developers.google.com%2F#5)

### Move ranges <a href="#move-ranges" id="move-ranges"></a>

```javascript
function duplicateAndOrganizeActiveSheet() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var duplicateSheet = mySS.duplicateActiveSheet();

  // Change the name of the new sheet.
  duplicateSheet.setName("Sheet_" + duplicateSheet.getSheetId());

  // Format the new sheet.
  duplicateSheet.autoResizeColumns(1, 5);
  duplicateSheet.setFrozenRows(2);

  // Move column F to column C.
  var myRange = duplicateSheet.getRange("F2:F");
  myRange.moveTo(duplicateSheet.getRange("C2"));
}
```

### Sort ranges <a href="#sort-ranges" id="sort-ranges"></a>

```javascript
function duplicateAndOrganizeActiveSheet() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var duplicateSheet = mySS.duplicateActiveSheet();

  // Change the name of the new sheet.
  duplicateSheet.setName("Sheet_" + duplicateSheet.getSheetId());

  // Format the new sheet.
  duplicateSheet.autoResizeColumns(1, 5);
  duplicateSheet.setFrozenRows(2);

  // Move column F to column C.
  var myRange = duplicateSheet.getRange("F2:F");
  myRange.moveTo(duplicateSheet.getRange("C2"));

  // Sort all the data using column C (Price information).
  myRange = duplicateSheet.getRange("A3:D55");
  myRange.sort(3);
}
```

> Reference : <https://developers.google.com/codelabs/apps-script-fundamentals-2?hl=en&continue=https%3A%2F%2Fcodelabs.developers.google.com%2F#0>
