-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathparseA1Notation.ts
More file actions
311 lines (264 loc) · 8.31 KB
/
parseA1Notation.ts
File metadata and controls
311 lines (264 loc) · 8.31 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { IllegalArgumentException } from "../../exception";
import { isNull, nonNull, requireNonEmptyString, toInteger } from "../../lang";
import { getColumnIndexByLetter } from "./getColumnIndexByLetter";
import { getColumnLetterByIndex } from "./getColumnLetterByIndex";
import type { GridRange } from "./types";
const SHEET_AND_RANGE_REGEX =
/^(?:'(?<sQuoteSheet>[^']*(?:''[^']*)*)'|"(?<dQuoteSheet>[^"]*(?:""[^"]*)*)"|(?<simpleSheet>[A-Z][A-Z0-9_]*))!(?<a1Range>.*)$/i;
const A1_RANGE_REGEX =
/^(?<startCol>[A-Z]*)(?<startRow>\d*)(?::(?<endCol>[A-Z]*)(?<endRow>\d*))?$/i;
/**
* Parses an A1 notation string into a <a href="./types/GridRange.ts"><code>GridRange</code></a> object.
*
* @example 1
* ```javascript
* const range = SpreadsheetApp.getActiveRange();
* const a1Notation = range.getA1Notation();
* const result = parseA1Notation(a1Notation);
*
* console.log(result);
* ```
*
* @example 2
* ```javascript
* parseA1Notation("A1:AZ10");
* parseA1Notation("B5");
* parseA1Notation("5:15");
* parseA1Notation("M:X");
* parseA1Notation("B2:B2");
* parseA1Notation("B");
* parseA1Notation("5");
* parseA1Notation("15:5");
* parseA1Notation("15:M5");
* parseA1Notation("Sheet1!A1:B2");
* parseA1Notation("'Sheet name'!A1:B2");
* ```
*
* @param {string} a1Notation - The A1 notation string to parse.
* @returns {GridRange} An object representing the parsed grid range.
* @throws <a href="../../exception/IllegalArgumentException.ts"><code>IllegalArgumentException</code></a>
* @see {@link parseA1Notations}
* @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.0.0
* @version 1.1.0
* @environment `Google Apps Script`, `Browser`
* @author Maksym Stoianov <stoianov.maksym@gmail.com>
* @license Apache-2.0
*/
export function parseA1Notation(a1Notation: string): GridRange {
if (arguments.length === 0) {
throw new IllegalArgumentException();
}
const trimmedInput = requireNonEmptyString(a1Notation).trim();
const { sheetName, rangePart } = splitSheetAndRange(trimmedInput);
if (rangePart === "") {
throw new SyntaxError(`"${a1Notation}" is not a valid A1 notation.`);
}
const match = rangePart.match(A1_RANGE_REGEX);
if (!match || !match.groups) {
throw new SyntaxError(`"${a1Notation}" is not a valid A1 notation.`);
}
const { startCol, startRow, endCol, endRow } = match.groups;
const hasColon = rangePart.includes(":");
let sRow = startRow ? toInteger(startRow)! - 1 : null;
let sCol = startCol ? getColumnIndexByLetter(startCol) : null;
let eRow = endRow ? toInteger(endRow)! - 1 : null;
let eCol = endCol ? getColumnIndexByLetter(endCol) : null;
if (hasColon) {
if (isNull(sRow) && isNull(eRow)) {
sRow = 0;
} else if (isNull(sCol) && isNull(eCol)) {
sCol = 0;
} else if (nonNull(sRow) && nonNull(sCol) && nonNull(eCol) && isNull(eRow)) {
// Keep eRow as null (unbounded)
} else if (isNull(sRow) && nonNull(sCol) && nonNull(eCol) && nonNull(eRow)) {
sRow = 0;
}
} else {
if (nonNull(sCol) && nonNull(sRow)) {
eRow = sRow;
eCol = sCol;
} else if (nonNull(sCol)) {
sRow = 0;
eCol = sCol;
} else if (nonNull(sRow)) {
sCol = 0;
eRow = sRow;
} else {
throw new SyntaxError(`"${a1Notation}" is not a valid A1 notation.`);
}
}
// Normalize: ensure start <= end if both are present
if (nonNull(sRow) && nonNull(eRow) && sRow > eRow) {
[sRow, eRow] = [eRow, sRow];
}
if (nonNull(sCol) && nonNull(eCol) && sCol > eCol) {
[sCol, eCol] = [eCol, sCol];
}
const startRowIndex = sRow;
const endRowIndex = nonNull(eRow) ? eRow + 1 : null;
const startColumnIndex = sCol;
const endColumnIndex = nonNull(eCol) ? eCol + 1 : null;
const canonicalA1Notation = generateCanonicalA1({
startCol: startCol || null,
startRow: startRow || null,
endCol: endCol || null,
endRow: endRow || null,
hasColon,
startRowIndex,
endRowIndex,
startColumnIndex,
endColumnIndex
});
return {
sheetName,
a1Notation: canonicalA1Notation,
startRowIndex,
endRowIndex,
startColumnIndex,
endColumnIndex
};
}
/**
* Splits input into sheet name and range part.
*/
function splitSheetAndRange(input: string): {
sheetName: string | null;
rangePart: string;
} {
const match = input.match(SHEET_AND_RANGE_REGEX);
if (match && match.groups) {
const { sQuoteSheet, dQuoteSheet, simpleSheet, a1Range } = match.groups;
let sheetName: string | null = null;
if (sQuoteSheet !== undefined) {
sheetName = sQuoteSheet.replace(/''/g, "'");
} else if (dQuoteSheet !== undefined) {
sheetName = dQuoteSheet.replace(/""/g, '"');
} else if (simpleSheet !== undefined) {
sheetName = simpleSheet;
}
return { sheetName, rangePart: a1Range };
}
return { sheetName: null, rangePart: input };
}
/**
* Generates canonical A1 notation.
*/
function generateCanonicalA1(params: {
startCol: string | null;
startRow: string | null;
endCol: string | null;
endRow: string | null;
hasColon: boolean;
startRowIndex: number | null;
endRowIndex: number | null;
startColumnIndex: number | null;
endColumnIndex: number | null;
}): string {
const {
startCol,
startRow,
endCol,
endRow,
hasColon,
startRowIndex,
endRowIndex,
startColumnIndex,
endColumnIndex
} = params;
const sColLetter = nonNull(startColumnIndex) ? getColumnLetterByIndex(startColumnIndex) : "";
const sRowNum = nonNull(startRowIndex) ? startRowIndex + 1 : "";
// Single cell case
if (
!hasColon &&
nonNull(startColumnIndex) &&
nonNull(startRowIndex) &&
nonNull(endColumnIndex) &&
nonNull(endRowIndex) &&
startColumnIndex === endColumnIndex - 1 &&
startRowIndex === endRowIndex - 1
) {
return `${sColLetter}${sRowNum}`;
}
// Single column case (B -> B:B)
if (
!hasColon &&
nonNull(startColumnIndex) &&
nonNull(endColumnIndex) &&
startRowIndex === 0 &&
isNull(endRowIndex)
) {
return `${sColLetter}:${sColLetter}`;
}
// Single row case (5 -> 5:5)
if (
!hasColon &&
nonNull(startRowIndex) &&
nonNull(endRowIndex) &&
startColumnIndex === 0 &&
isNull(endColumnIndex)
) {
return `${sRowNum}:${sRowNum}`;
}
// B2:B2 -> B2
if (
hasColon &&
nonNull(startColumnIndex) &&
nonNull(startRowIndex) &&
nonNull(endColumnIndex) &&
nonNull(endRowIndex) &&
startColumnIndex === endColumnIndex - 1 &&
startRowIndex === endRowIndex - 1
) {
return `${sColLetter}${sRowNum}`;
}
// Column range (A:B)
if (
hasColon &&
nonNull(startColumnIndex) &&
nonNull(endColumnIndex) &&
startRowIndex === 0 &&
isNull(endRowIndex) &&
startCol &&
endCol &&
!startRow &&
!endRow
) {
const eColLetter = getColumnLetterByIndex(endColumnIndex - 1);
return `${sColLetter}:${eColLetter}`;
}
// Special GAS Cases for B:B if it was parsed from B
if (
!hasColon &&
nonNull(startColumnIndex) &&
nonNull(endColumnIndex) &&
startRowIndex === 0 &&
isNull(endRowIndex)
) {
return `${sColLetter}:${sColLetter}`;
}
if (hasColon) {
const eColLetter = nonNull(endColumnIndex) ? getColumnLetterByIndex(endColumnIndex - 1) : "";
const eRowNum = nonNull(endRowIndex) ? endRowIndex : "";
if (startCol && startRow && endCol && endRow) {
return `${sColLetter}${sRowNum}:${eColLetter}${eRowNum}`;
}
if (startCol && endCol && !startRow && !endRow) {
return `${sColLetter}:${eColLetter}`;
}
if (startRow && endRow && !startCol && !endCol) {
return `${sRowNum}:${eRowNum}`;
}
if (startCol && startRow && endCol && !endRow) {
return `${sColLetter}${sRowNum}:${eColLetter}`;
}
if (startCol && endCol && endRow && !startRow) {
return `${sColLetter}:${eColLetter}${eRowNum}`;
}
}
// Fallback for some GAS weirdness if we can't match logic above, but it should be valid A1
// But original code throws SyntaxError here if it doesn't match specific combinations.
throw new SyntaxError(`Invalid A1 notation.`);
}