Skip to content

Commit 408d54d

Browse files
Merge pull request #83 from oliversmith1987/add
Add
2 parents a8f5282 + 3d057cb commit 408d54d

10 files changed

Lines changed: 102 additions & 39 deletions

File tree

.idea/prettier.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
## [1.6.1](https://github.com/MaksymStoianov/apps-script-utils/compare/v1.6.0...v1.6.1) (2025-11-01)
44

5-
65
### Bug Fixes
76

8-
* update package.json ([a180666](https://github.com/MaksymStoianov/apps-script-utils/commit/a180666a82a16b9c7ae5dae10dc38d802515010d))
9-
* update package.json ([87d27ac](https://github.com/MaksymStoianov/apps-script-utils/commit/87d27ac131bfdef5adb07dafc4d637bb105ff636))
7+
- update package.json ([a180666](https://github.com/MaksymStoianov/apps-script-utils/commit/a180666a82a16b9c7ae5dae10dc38d802515010d))
8+
- update package.json ([87d27ac](https://github.com/MaksymStoianov/apps-script-utils/commit/87d27ac131bfdef5adb07dafc4d637bb105ff636))
109

1110
## [1.6.0](https://github.com/MaksymStoianov/appsscript-utils/compare/v1.5.1...v1.6.0) (2025-10-11)
1211

README.md

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,28 @@
1919
[![GitHub Sponsors](https://img.shields.io/github/sponsors/MaksymStoianov?style=social&logo=github)](https://github.com/sponsors/MaksymStoianov)
2020

2121
<!-- TOC -->
22-
* [Utilities for Google Apps Script™ projects](#utilities-for-google-apps-script-projects)
23-
* [Introduction](#introduction)
24-
* [How to Install](#how-to-install)
25-
* [Functions by Category](#functions-by-category)
26-
* [1. Google Apps Script Module](#1-google-apps-script-module)
27-
* [1.1. Google Base Methods](#11-google-base-methods)
28-
* [1.2. Google Admin SDK Directory Methods](#12-google-admin-sdk-directory-methods)
29-
* [1.3. Google Drive Methods](#13-google-drive-methods)
30-
* [1.4. Google Docs Methods](#14-google-docs-methods)
31-
* [1.5. Google Forms Methods](#15-google-forms-methods)
32-
* [1.6. Google Sheets Methods](#16-google-sheets-methods)
33-
* [1.7. Google Slides Methods](#17-google-slides-methods)
34-
* [1.8. Google UI Methods](#18-google-ui-methods)
35-
* [2. `Base` Utilities](#2-base-utilities)
36-
* [3. Exceptions Module](#3-exceptions-module)
37-
* [4. `path` Module](#4-path-module)
38-
* [5. `abstracts` and `interfaces`](#5-abstracts-and-interfaces)
39-
* [Tasks](#tasks)
40-
* [Changelog](#changelog)
41-
* [License](#license)
42-
<!-- TOC -->
22+
23+
- [Utilities for Google Apps Script™ projects](#utilities-for-google-apps-script-projects)
24+
- [Introduction](#introduction)
25+
- [How to Install](#how-to-install)
26+
- [Functions by Category](#functions-by-category)
27+
- [1. Google Apps Script Module](#1-google-apps-script-module)
28+
- [1.1. Google Base Methods](#11-google-base-methods)
29+
- [1.2. Google Admin SDK Directory Methods](#12-google-admin-sdk-directory-methods)
30+
- [1.3. Google Drive Methods](#13-google-drive-methods)
31+
- [1.4. Google Docs Methods](#14-google-docs-methods)
32+
- [1.5. Google Forms Methods](#15-google-forms-methods)
33+
- [1.6. Google Sheets Methods](#16-google-sheets-methods)
34+
- [1.7. Google Slides Methods](#17-google-slides-methods)
35+
- [1.8. Google UI Methods](#18-google-ui-methods)
36+
- [2. `Base` Utilities](#2-base-utilities)
37+
- [3. Exceptions Module](#3-exceptions-module)
38+
- [4. `path` Module](#4-path-module)
39+
- [5. `abstracts` and `interfaces`](#5-abstracts-and-interfaces)
40+
- [Tasks](#tasks)
41+
- [Changelog](#changelog)
42+
- [License](#license)
43+
<!-- TOC -->
4344

4445
## Introduction
4546

@@ -257,6 +258,11 @@ Functions that enable various operations on Google Sheets.
257258
<td></td>
258259
<td>Gets a sheet by its ID.</td>
259260
</tr>
261+
<tr>
262+
<td><a href="src/appsscript/sheet/getSheetByIndex.ts"><code>getSheetByIndex</code></a></td>
263+
<td></td>
264+
<td>Gets a sheet by its index.</td>
265+
</tr>
260266
<tr>
261267
<td><a href="src/appsscript/sheet/highlightHtml.ts"><code>highlightHtml</code></a></td>
262268
<td></td>
@@ -451,7 +457,7 @@ This package contains core utility functions that are not tied to a specific App
451457
<details open><summary>Functions</summary>
452458

453459
| Function | Description |
454-
| :------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------- |
460+
|:--------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------|
455461
| [`chunk`](src/lang/array/chunk.ts) | Splits an array into chunks of a specified size. |
456462
| [`decodeHtml`](src/html/decodeHtml.ts) | Decodes HTML entities. |
457463
| [`encodeHtml`](src/html/encodeHtml.ts) | Encodes a string for safe use in HTML. |
@@ -471,7 +477,10 @@ This package contains core utility functions that are not tied to a specific App
471477
| [`isLength`](src/lang/base/isLength.ts) | Checks if a value is "length-like" (arrays, strings, etc.). |
472478
| [`isNil`](src/lang/base/isNil.ts) | Checks if a value is `null` or `undefined`. |
473479
| [`isNull`](src/lang/base/isNull.ts) | Checks if a value is `null`. |
474-
| [`isNumber`](src/lang/base/isNumber.ts) | Checks if a variable is a number. |
480+
| [`toInteger`](src/lang/number/toInteger.ts) | Converts a value to an integer. |
481+
| [`isInteger`](src/lang/number/isInteger.ts) | |
482+
| [`isCountable)`](src/lang/number/isCountable.ts) | |
483+
| [`nonNegative`](src/lang/number/nonNegative.ts) | |
475484
| [`isNumberLike`](src/lang/base/isNumberLike.ts) | Checks if a value can be converted to a number. |
476485
| [`isObject`](src/lang/base/isObject.ts) | Checks if a variable is an object (but not `null` or an array). |
477486
| [`isObjectLike`](src/lang/base/isObjectLike.ts) | Checks if a variable is object-like (objects, arrays, functions). |
@@ -522,14 +531,14 @@ This package is for all exception classes.
522531
<details open><summary>Functions</summary>
523532

524533
| Exception | Description |
525-
| :----------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------ |
534+
|:-------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------|
526535
| [`AdminDirectoryException`](src/exception/appsscript/admin/AdminDirectoryException.ts) | Represents an exception thrown when the Admin SDK Directory Service is not available or enabled. |
527-
| [`InvalidGridRangeException`](src/exception/appsscript/sheet/InvalidGridRangeException.ts) | Represents an exception thrown when an invalid <a href="src/appsscript/sheet/types/GridRange.ts"><code>GridRange</code></a> object is provided. |
536+
| [`InvalidGridRangeException`](src/exception/appsscript/sheet/InvalidGridRangeException.ts) | Represents an exception thrown when an invalid <a href="src/appsscript/sheet/types/GridRange.ts"><code>GridRange</code></a> object is provided. |
528537
| [`InvalidRangeException`](src/exception/appsscript/sheet/InvalidRangeException.ts) | Represents an exception thrown when an invalid [range](https://developers.google.com/apps-script/reference/spreadsheet/sheet) object is provided. |
529538
| [`InvalidSheetException`](src/exception/appsscript/sheet/InvalidSheetException.ts) | Represents an exception thrown when an invalid [sheet](https://developers.google.com/apps-script/reference/spreadsheet/sheet) object is provided. |
530539

531540
| Exception | Description |
532-
| :---------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------- |
541+
|:------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------|
533542
| [`Exception`](src/exception/Exception.ts) | Base exception class. |
534543
| [`RuntimeException`](src/exception/RuntimeException.ts) | Exception for runtime errors. |
535544
| [`EmptyStringException`](src/exception/EmptyStringException.ts) | Exception for empty strings. |

package-lock.json

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/appsscript/sheet/getSheetById.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IllegalArgumentException } from "../../exception";
2-
import { isEmpty } from "../../lang";
2+
import { isCountable, isEmpty } from "../../lang";
33

44
/**
55
* ## getSheetById
@@ -10,6 +10,7 @@ import { isEmpty } from "../../lang";
1010
* @param [ss] - The Spreadsheet object to search within. Defaults to the active Spreadsheet if not provided.
1111
* @returns The {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet} object if found, otherwise `null`.
1212
* @throws {@link IllegalArgumentException}
13+
* @see {@link getSheetByIndex}
1314
* @see {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet}
1415
* @see [Class Sheet](https://developers.google.com/apps-script/reference/spreadsheet/sheet)
1516
* @since 1.0.0
@@ -24,6 +25,10 @@ export function getSheetById(
2425
throw new IllegalArgumentException();
2526
}
2627

28+
if (!(sheetId === 0 || isCountable(sheetId))) {
29+
throw new IllegalArgumentException();
30+
}
31+
2732
const sheets = (ss || SpreadsheetApp.getActiveSpreadsheet()).getSheets();
2833

2934
if (isEmpty(sheets)) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { IllegalArgumentException } from "../../exception";
2+
import { isCountable, isEmpty } from "../../lang";
3+
4+
/**
5+
* ## getSheetById
6+
*
7+
* Retrieves a Google Sheet by its position (index) in the spreadsheet
8+
*
9+
* @param sheetIndex - The zero-based index of the sheet (0 is the first sheet).
10+
* @param [spreadsheet] - The Spreadsheet object to search within. Defaults to the active Spreadsheet if not provided.
11+
* @returns The {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet} object if found, otherwise `null`.
12+
* @throws {@link IllegalArgumentException}
13+
* @see {@link getSheetById}
14+
* @see {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet}
15+
* @see [Class Sheet](https://developers.google.com/apps-script/reference/spreadsheet/sheet)
16+
* @version 1.0.0
17+
* @environment `Google Apps Script`
18+
*/
19+
export function getSheetByIndex(
20+
sheetIndex: number,
21+
spreadsheet?: GoogleAppsScript.Spreadsheet.Spreadsheet | undefined | null
22+
): GoogleAppsScript.Spreadsheet.Sheet | null {
23+
if (arguments.length === 0) {
24+
throw new IllegalArgumentException();
25+
}
26+
27+
if (!(sheetIndex === 0 || isCountable(sheetIndex))) {
28+
throw new IllegalArgumentException();
29+
}
30+
31+
const ss = spreadsheet || SpreadsheetApp.getActiveSpreadsheet();
32+
33+
if (!ss) {
34+
return null;
35+
}
36+
37+
const sheets = ss.getSheets();
38+
39+
if (isEmpty(sheets)) {
40+
return null;
41+
}
42+
43+
return sheets[sheetIndex] ?? null;
44+
}

src/appsscript/sheet/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export { extractRangeFromA1Notation } from "./extractRangeFromA1Notation";
5151
export { extractSheetNameFromA1Notation } from "./extractSheetNameFromA1Notation";
5252
export { getColumnIndexByLetter } from "./getColumnIndexByLetter";
5353
export { getColumnLetterByIndex } from "./getColumnLetterByIndex";
54-
export { getSheetById } from "./getSheetById";
54+
export * from "./getSheetById";
55+
export * from "./getSheetByIndex";
5556
// TODO: getValues(sheet: Sheet, config: Object)
5657
// TODO: clearColumnsByConditional(sheet: Sheet, callback: Function)
5758
// TODO: clearRowsByConditional(sheet: Sheet, callback: Function)

src/lang/number/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// TODO: isDouble
22
// TODO: isFloat
3+
export * from "./isCountable";
34
export { isInteger } from "./isInteger";
45
// TODO: isSafeInteger
56
// TODO: isNaN
@@ -9,6 +10,7 @@ export { isInteger } from "./isInteger";
910
// TODO: nonInteger
1011
// TODO: nonSafeInteger
1112
// TODO: nonNaN
13+
export * from "./nonNegative";
1214

1315
// TODO: requireDouble
1416
// TODO: requireFloat

src/lang/number/isCountable.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { nonNegative } from "./nonNegative";
2+
3+
export function isCountable(value: unknown): value is number {
4+
return nonNegative(value) && Number.isSafeInteger(value);
5+
}

src/lang/number/nonNegative.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isNumber } from "../base";
2+
3+
export function nonNegative(value: unknown): value is number {
4+
return isNumber(value) && !isNaN(value) && value >= 0;
5+
}

0 commit comments

Comments
 (0)