-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdoGridRangesIntersect.ts
More file actions
79 lines (64 loc) · 2.86 KB
/
doGridRangesIntersect.ts
File metadata and controls
79 lines (64 loc) · 2.86 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
import { IllegalArgumentException, InvalidGridRangeException } from "../../exception";
import { isObject } from "../../lang";
import { isValidSheetId } from "./isValidSheetId";
import { isValidSheetName } from "./isValidSheetName";
import type { GridRange } from "./types";
/**
* Checks if two <a href="./types/GridRange.ts"><code>GridRange</code></a> objects overlap and are on the same sheet.
*
* @param {GridRange} gridRange1 - The first <a href="./types/GridRange.ts"><code>GridRange</code></a> object to check.
* @param {GridRange} gridRange2 - The second <a href="./types/GridRange.ts"><code>GridRange</code></a> object to check.
* @returns {boolean} `true` if the ranges share at least one common cell and are located on the same sheet; `false` otherwise.
* @throws <a href="../../exception/IllegalArgumentException.ts"><code>IllegalArgumentException</code></a>
* @throws <a href="../../exception/appsscript/sheet/InvalidGridRangeException.ts"><code>InvalidGridRangeException</code></a>
* @see <a href="./types/GridRange.ts"><code>GridRange</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 doGridRangesIntersect(gridRange1: GridRange, gridRange2: GridRange): boolean {
if (arguments.length === 0) {
throw new IllegalArgumentException();
}
if (!isObject(gridRange1)) {
throw new InvalidGridRangeException();
}
if (!isObject(gridRange2)) {
throw new InvalidGridRangeException();
}
const sheetIdsMatch =
isValidSheetId(gridRange1.sheetId) && isValidSheetId(gridRange2.sheetId)
? gridRange1.sheetId === gridRange2.sheetId
: true;
const sheetNamesMatch =
isValidSheetName(gridRange1.sheetName) && isValidSheetName(gridRange2.sheetName)
? gridRange1.sheetName === gridRange2.sheetName
: true;
if (!sheetIdsMatch || !sheetNamesMatch) {
if (
(isValidSheetId(gridRange1.sheetId) &&
isValidSheetId(gridRange2.sheetId) &&
!sheetIdsMatch) ||
(isValidSheetName(gridRange1.sheetName) &&
isValidSheetName(gridRange2.sheetName) &&
!sheetNamesMatch)
) {
return false;
}
}
const r1_startRow = gridRange1.startRowIndex ?? 0;
const r1_endRow = gridRange1.endRowIndex ?? Infinity;
const r1_startCol = gridRange1.startColumnIndex ?? 0;
const r1_endCol = gridRange1.endColumnIndex ?? Infinity;
const r2_startRow = gridRange2.startRowIndex ?? 0;
const r2_endRow = gridRange2.endRowIndex ?? Infinity;
const r2_startCol = gridRange2.startColumnIndex ?? 0;
const r2_endCol = gridRange2.endColumnIndex ?? Infinity;
return !(
r1_endCol <= r2_startCol ||
r2_endCol <= r1_startCol ||
r1_endRow <= r2_startRow ||
r2_endRow <= r1_startRow
);
}