-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathisGridRangeContainedIn.ts
More file actions
70 lines (57 loc) · 2.79 KB
/
isGridRangeContainedIn.ts
File metadata and controls
70 lines (57 loc) · 2.79 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
import { InvalidGridRangeException } from "../../exception";
import { isObject } from "../../lang";
import type { GridRange } from "./types";
/**
* Checks if a <a href="./types/GridRange.ts"><code>GridRange</code></a> is entirely contained within another <a href="./types/GridRange.ts"><code>GridRange</code></a>.
* Both ranges must be located on the same sheet (either by `sheetId` or by `sheetName`).
*
* @param {GridRange} gridRange - The <a href="./types/GridRange.ts"><code>GridRange</code></a> object that is potentially a subset (child range).
* @param {GridRange} containerGridRange - The <a href="./types/GridRange.ts"><code>GridRange</code></a> object that is potentially a superset (parent range).
* @returns {boolean} `true` if `gridRange` is fully contained within `containerGridRange` and they are on the same sheet;
* `false` otherwise.
* @throws <a href="../../exception/IllegalArgumentException.ts"><code>IllegalArgumentException</code></a>
* @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`
*/
export function isGridRangeContainedIn(
gridRange: GridRange,
containerGridRange: GridRange
): boolean {
if (!isObject(gridRange)) {
throw new InvalidGridRangeException();
}
const sheetIdsMatch =
containerGridRange.sheetId != null && gridRange.sheetId != null
? containerGridRange.sheetId === gridRange.sheetId
: true;
const sheetNamesMatch =
containerGridRange.sheetName != null && gridRange.sheetName != null
? containerGridRange.sheetName === gridRange.sheetName
: true;
if (!sheetIdsMatch || !sheetNamesMatch) {
if (
(containerGridRange.sheetId != null && gridRange.sheetId != null && !sheetIdsMatch) ||
(containerGridRange.sheetName != null && gridRange.sheetName != null && !sheetNamesMatch)
) {
return false;
}
}
const outerStartRow = containerGridRange.startRowIndex ?? 0;
const outerEndRow = containerGridRange.endRowIndex ?? Infinity;
const outerStartCol = containerGridRange.startColumnIndex ?? 0;
const outerEndCol = containerGridRange.endColumnIndex ?? Infinity;
const innerStartRow = gridRange.startRowIndex ?? 0;
const innerEndRow = gridRange.endRowIndex ?? Infinity;
const innerStartCol = gridRange.startColumnIndex ?? 0;
const innerEndCol = gridRange.endColumnIndex ?? Infinity;
return (
innerStartRow >= outerStartRow &&
innerEndRow <= outerEndRow &&
innerStartCol >= outerStartCol &&
innerEndCol <= outerEndCol
);
}