<commit_before># Google Apps Script Snippets
## Gists
- [The Modal Dialog][1] works longer than the Prompt.
- [Visibility Browser API][2] for sidebars & add-ons
- [Set content to file, get content from file][3]
- [Sort Active Range][4] of Spreadsheet

[1]: https://gist.github.com/oshliaer/9d9dd61ccebf4ea0070c
[2]: https://gist.github.com/oshliaer/4c3c926ab0a5bc9630fb
[3]: https://gist.github.com/oshliaer/25d4fca21e8745df4de9
[4]: https://gist.github.com/oshliaer/0801f151fb57a4bee7f2

## Spreadsheet
### getSheetByName(spreadsheet, sheetName)
Always returns a sheet
```javascript
function getSheetByName(spreadsheet, sheetName){
  var sheet = spreadsheet.getSheetByName(sheetName);
  return sheet || spreadsheet.insertSheet(sheetName);
}
```
#### Example
```javascript
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var settingsSheet = getSheetByName(spreadsheet, 'Settings');
```
### cropSheetBySize(sheet, leftRows, topCols)
Prettify the given sheet cropping it
```javascript
function cropSheetBySize(sheet, leftRows, topCols){
  (leftRows && sheet.getMaxRows() > leftRows) &&
    sheet.deleteRows(leftRows + 1, sheet.getMaxRows() - leftRows);
  (topCols && sheet.getMaxColumns() > topCols) &&
    sheet.deleteColumns(topCols + 1, sheet.getMaxColumns() - topCols);
  return sheet;
}
```
#### Example
```javascript
  cropSheetBySize(sheet, 2, 3);
```
### cropSheetByData(sheet)
Prettify the given sheet cropping it
```javascript
function cropSheetByData(sheet){
  return cropSheetBySize(sheet, sheet.getLastRow(), sheet.getLastColumn());
}
```
#### Example
```javascript
 sheet.clearContents().getRange(2, 1, values.length, values[0].length).setValues(values);
 cropSheetByData(sheet);
```
### flushAndSleep(milliseconds)
Give your tables to cool down. Don't use with loops
```javascript
function flushAndSleep(milliseconds){
  SpreadsheetApp.flush();
  Utilities.sleep(milliseconds || 100);
}
```<commit_msg>"Move to last cell in specific worksheet" added<commit_after># Google Apps Script Snippets
## Gists
- [The Modal Dialog][1] works longer than the Prompt.
- [Visibility Browser API][2] for sidebars & add-ons
- [Set content to file, get content from file][3]
- [Sort Active Range][4] of Spreadsheet

[1]: https://gist.github.com/oshliaer/9d9dd61ccebf4ea0070c
[2]: https://gist.github.com/oshliaer/4c3c926ab0a5bc9630fb
[3]: https://gist.github.com/oshliaer/25d4fca21e8745df4de9
[4]: https://gist.github.com/oshliaer/0801f151fb57a4bee7f2

## Spreadsheet
### getSheetByName(spreadsheet, sheetName)
Always returns a sheet
```javascript
function getSheetByName(spreadsheet, sheetName){
  var sheet = spreadsheet.getSheetByName(sheetName);
  return sheet || spreadsheet.insertSheet(sheetName);
}
```
#### Example
```javascript
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var settingsSheet = getSheetByName(spreadsheet, 'Settings');
```
### cropSheetBySize(sheet, leftRows, topCols)
Prettify the given sheet cropping it
```javascript
function cropSheetBySize(sheet, leftRows, topCols){
  (leftRows && sheet.getMaxRows() > leftRows) &&
    sheet.deleteRows(leftRows + 1, sheet.getMaxRows() - leftRows);
  (topCols && sheet.getMaxColumns() > topCols) &&
    sheet.deleteColumns(topCols + 1, sheet.getMaxColumns() - topCols);
  return sheet;
}
```
#### Example
```javascript
  cropSheetBySize(sheet, 2, 3);
```
### cropSheetByData(sheet)
Prettify the given sheet cropping it
```javascript
function cropSheetByData(sheet){
  return cropSheetBySize(sheet, sheet.getLastRow(), sheet.getLastColumn());
}
```
#### Example
```javascript
 sheet.clearContents().getRange(2, 1, values.length, values[0].length).setValues(values);
 cropSheetByData(sheet);
```
### flushAndSleep(milliseconds)
Give your tables to cool down. Don't use with loops
```javascript
function flushAndSleep(milliseconds){
  SpreadsheetApp.flush();
  Utilities.sleep(milliseconds || 100);
}
```
### onOpen() Move to last cell in specific worksheet [Simple]
```javascript
  var name = 'Sheet1';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  sheet.getRange(sheet.getLastRow(), 1).activate();
```
### onOpen() Move to last cell in specific worksheet [Advanced]
```javascript
function onOpen(){
  var uid = '792071603';
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var uidsMap = sheets.reduce(function(p,c,i){p[c.getSheetId()]=i;return p;},{});
  var sheet = sheets[uidsMap[uid]];
  sheet.getRange(sheet.getLastRow(), 1).activate();
}
```
{'fraction_non_alphanumeric': 0.1488268156424581, 'fraction_numerical': 0.02994413407821229, 'mean_word_length': 6.313725490196078, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2688752', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1451, 'n_words': 332}