-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdateSheetNameInA1Notation.ts
More file actions
53 lines (44 loc) · 2.18 KB
/
updateSheetNameInA1Notation.ts
File metadata and controls
53 lines (44 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { IllegalArgumentException } from "../../exception";
import { requireNonEmptyString } from "../../lang";
import { isValidSheetName } from "./isValidSheetName";
import { parseA1Notation } from "./parseA1Notation";
/**
* Updates or sets the sheet name within an A1 notation string, while preserving the range information (e.g., `A1:B2`).
*
* @param {string} a1Notation - The source A1 notation, which may or may not include a sheet name (e.g., `'Sheet Name'!A1:B2` or `A1:B2`).
* @param {string|null} [sheetName] - The new sheet name to set.
* Pass `null` or `undefined` to remove any existing sheet name and return only the range.
* @returns {string} The new A1 notation string with the updated sheet name (e.g., `'New Sheet'!A1:B2` or just `A1:B2`).
* @throws <a href="../../exception/IllegalArgumentException.ts"><code>IllegalArgumentException</code></a>
* @throws <a href="../../exception/EmptyStringException.ts"><code>EmptyStringException</code></a>
* @see {@link parseA1Notation}
* @see <a href="./types/GridRange.ts"><code>GridRange</code></a>
* @see <a href="https://developers.google.com/apps-script/reference/spreadsheet/range"><code>Range</code></a>
* @see <a href="https://developers.google.com/apps-script/reference/spreadsheet/sheet"><code>Sheet</code></a>
* @since 1.6.0
* @version 1.0.0
* @environment `Google Apps Script`, `Browser`
* @author Maksym Stoianov <stoianov.maksym@gmail.com>
* @license Apache-2.0
*/
export function updateSheetNameInA1Notation(a1Notation: string, sheetName?: string | null): string {
if (arguments.length === 0) {
throw new IllegalArgumentException();
}
const gridRange = parseA1Notation(a1Notation);
const range = requireNonEmptyString(gridRange.a1Notation);
if (!isValidSheetName(sheetName)) {
return range;
}
const isSimpleSheetName = /^[A-Z_][A-Z0-9_]*$/i.test(sheetName);
if (isSimpleSheetName) {
return `${sheetName}!${range}`;
}
if (!sheetName.includes("'")) {
return `'${sheetName}'!${range}`;
}
if (!sheetName.includes('"')) {
return `"${sheetName}"!${range}`;
}
return `'${sheetName.replace(/'/g, "''")}'!${range}`;
}