-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgetSheetByIndex.ts
More file actions
42 lines (35 loc) · 1.47 KB
/
getSheetByIndex.ts
File metadata and controls
42 lines (35 loc) · 1.47 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
import { IllegalArgumentException } from "../../exception";
import { isCountable, isEmpty } from "../../lang";
/**
* Retrieves a Google Sheet by its position (index) in the spreadsheet
*
* @param {number} sheetIndex - The zero-based index of the sheet (0 is the first sheet).
* @param {GoogleAppsScript.Spreadsheet.Spreadsheet | null} [spreadsheet] - The Spreadsheet object to search within. Defaults to the active Spreadsheet if not provided.
* @returns {GoogleAppsScript.Spreadsheet.Sheet | null} The {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet} object if found, otherwise `null`.
* @throws {@link IllegalArgumentException}
* @see {@link getSheetById}
* @see {@link GoogleAppsScript.Spreadsheet.Sheet|Sheet}
* @see [Class Sheet](https://developers.google.com/apps-script/reference/spreadsheet/sheet)
* @version 1.0.0
* @environment `Google Apps Script`
*/
export function getSheetByIndex(
sheetIndex: number,
spreadsheet?: GoogleAppsScript.Spreadsheet.Spreadsheet | undefined | null
): GoogleAppsScript.Spreadsheet.Sheet | null {
if (arguments.length === 0) {
throw new IllegalArgumentException();
}
if (!(sheetIndex === 0 || isCountable(sheetIndex))) {
throw new IllegalArgumentException();
}
const ss = spreadsheet || SpreadsheetApp.getActiveSpreadsheet();
if (!ss) {
return null;
}
const sheets = ss.getSheets();
if (isEmpty(sheets)) {
return null;
}
return sheets[sheetIndex] ?? null;
}