|
1 | 1 | import { IllegalArgumentException } from "../../exception"; |
2 | | -import { requireNonEmptyString } from "../../lang"; |
| 2 | +import { isString } from "../../lang"; |
3 | 3 | import { isValidSheetName } from "./isValidSheetName"; |
4 | 4 | import { parseA1Notation } from "./parseA1Notation"; |
5 | 5 |
|
6 | | -export function extractSheetNameFromA1Notation(a1Notation: string): string { |
| 6 | +/** |
| 7 | + * ## extractSheetNameFromA1Notation |
| 8 | + * |
| 9 | + * Extracts the sheet name from an A1 notation string (e.g., `Sheet1!A1:B2`). |
| 10 | + * |
| 11 | + * This function returns the sheet name as a clean string, or `null` if the |
| 12 | + * notation contains only the range part (e.g., `A1:B2`). |
| 13 | + * |
| 14 | + * @param {string} a1Notation - The A1 notation string (e.g., `SheetName!A1`, `Sheet Name'!A:A`). |
| 15 | + * @returns {string|null} The extracted sheet name, or `null` if no sheet name is present in the notation. |
| 16 | + * @throws {Error} |
| 17 | + * @throws {IllegalArgumentException} |
| 18 | + * @see {@link parseA1Notation} |
| 19 | + * @see {@link GridRange} |
| 20 | + * @see {@link GoogleAppsScript.Spreadsheet.Range|Range} |
| 21 | + * @see {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet} |
| 22 | + * @see [Class Range](https://developers.google.com/apps-script/reference/spreadsheet/range) |
| 23 | + * @see [Class Sheet](https://developers.google.com/apps-script/reference/spreadsheet/sheet) |
| 24 | + * @since 1.6.0 |
| 25 | + * @version 1.0.0 |
| 26 | + * @environment `Google Apps Script`, `Browser` |
| 27 | + * @author Maksym Stoianov <stoianov.maksym@gmail.com> |
| 28 | + * @license Apache-2.0 |
| 29 | + */ |
| 30 | +export function extractSheetNameFromA1Notation( |
| 31 | + a1Notation: string |
| 32 | +): string | null { |
7 | 33 | if (arguments.length === 0) { |
8 | 34 | throw new IllegalArgumentException(); |
9 | 35 | } |
10 | 36 |
|
11 | | - const gridRange = parseA1Notation(a1Notation); |
| 37 | + const { sheetName } = parseA1Notation(a1Notation); |
12 | 38 |
|
13 | | - const sheetName = requireNonEmptyString(gridRange.sheetName); |
| 39 | + if (!isString(sheetName)) { |
| 40 | + return null; |
| 41 | + } |
14 | 42 |
|
15 | 43 | if (!isValidSheetName(sheetName)) { |
16 | | - throw new Error(); |
| 44 | + throw new Error("Expected a valid sheet name"); |
17 | 45 | } |
18 | 46 |
|
19 | 47 | return sheetName; |
|
0 commit comments