Skip to content

Commit 6f9dd6f

Browse files
feat(appsscript/sheet): Add extractSheetNameFromA1Notation utility function
1 parent 55f1c99 commit 6f9dd6f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/appsscript/sheet/extractSheetNameFromA1Notation.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
import { IllegalArgumentException } from "../../exception";
2-
import { requireNonEmptyString } from "../../lang";
2+
import { isString } from "../../lang";
33
import { isValidSheetName } from "./isValidSheetName";
44
import { parseA1Notation } from "./parseA1Notation";
55

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 {
733
if (arguments.length === 0) {
834
throw new IllegalArgumentException();
935
}
1036

11-
const gridRange = parseA1Notation(a1Notation);
37+
const { sheetName } = parseA1Notation(a1Notation);
1238

13-
const sheetName = requireNonEmptyString(gridRange.sheetName);
39+
if (!isString(sheetName)) {
40+
return null;
41+
}
1442

1543
if (!isValidSheetName(sheetName)) {
16-
throw new Error();
44+
throw new Error("Expected a valid sheet name");
1745
}
1846

1947
return sheetName;

0 commit comments

Comments
 (0)