Skip to content

Commit 669987f

Browse files
feat(appsscript/sheets/sortSheets): Add sortSheets utility function
1 parent a4dec07 commit 669987f

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Functions specifically designed for Google Apps Script environments, including u
4646
<details open><summary>Functions</summary>
4747

4848
| Function | Description |
49-
| :--------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- |
49+
|:-----------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------|
5050
| [`checkMultipleAccount`](src/appsscript/checkMultipleAccount.ts) | Checks if multiple Google accounts are in use. |
5151
| [`getByteSize`](src/appsscript/getByteSize.ts) | Returns the size of a string in bytes. |
5252
| [`isHtmlOutput`](src/appsscript/isHtmlOutput.ts) | Checks if an object is an [`HtmlOutput`](https://developers.google.com/apps-script/reference/html/html-output). |
@@ -55,14 +55,24 @@ Functions specifically designed for Google Apps Script environments, including u
5555

5656
</details>
5757

58-
#### 1.2. `appsscript/sheets` (Google Sheets Utilities)
58+
#### 1.2. `appsscript/admin` (Admin SDK Directory Service)
59+
60+
<details open><summary>Functions</summary>
61+
62+
| Function | Description |
63+
|:------------------------------------------------|:-------------------------------------------------------------------------------|
64+
| [`isAdmin`](src/appsscript/admin/isAdmin.ts) 🆕 | Checks if the current user is an administrator of the Google Workspace domain. |
65+
66+
</details>
67+
68+
#### 1.3. `appsscript/sheets` (Google Sheets Utilities)
5969

6070
A collection of functions to simplify working with Google Sheets.
6171

6272
<details open><summary>Functions</summary>
6373

6474
| Function | Description |
65-
| :-------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------- |
75+
|:----------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------|
6676
| [`appendColumn`](src/appsscript/sheets/appendColumn.ts) | Appends a single column of data to the sheet. |
6777
| [`appendColumns`](src/appsscript/sheets/appendColumns.ts) | Appends multiple columns of data to the sheet. |
6878
| [`appendRow`](src/appsscript/sheets/appendRow.ts) | Appends a single row of data to the sheet. |
@@ -89,6 +99,7 @@ A collection of functions to simplify working with Google Sheets.
8999
| [`parseA1Notation`](src/appsscript/sheets/parseA1Notation.ts) | Parses an A1 notation (e.g., 'A1:B2') into [`GridRange`](src/appsscript/sheets/types/GridRange.ts) components. |
90100
| [`prependRow`](src/appsscript/sheets/prependRow.ts) | Prepends a single row of data to the sheet. |
91101
| [`prependRows`](src/appsscript/sheets/prependRows.ts) | Prepends multiple rows of data to the sheet. |
102+
| [`sortSheets`](src/appsscript/sheets/sortSheets.ts) 🆕 | Sorts all sheets in a spreadsheet alphabetically by name. |
92103
| [`toA1Notation`](src/appsscript/sheets/toA1Notation.ts) | Converts a [`GridRange`](src/appsscript/sheets/types/GridRange.ts) to A1 notation. |
93104

94105
</details>
@@ -102,7 +113,7 @@ General utility functions that can be useful in any JavaScript/TypeScript projec
102113
<details open><summary>Functions</summary>
103114

104115
| Function | Description |
105-
| :----------------------------------------------------------- | :--------------------------------------------------------------------------------------------- |
116+
|:-------------------------------------------------------------|:-----------------------------------------------------------------------------------------------|
106117
| [`chunk`](src/base/chunk.ts) | Splits an array into chunks of a specified size. |
107118
| [`decodeHtml`](src/base/decodeHtml.ts) | Decodes HTML entities. |
108119
| [`encodeHtml`](src/base/encodeHtml.ts) | Encodes a string for safe use in HTML. |
@@ -173,7 +184,7 @@ A set of custom exception classes for more specific error handling.
173184
<details open><summary>Functions</summary>
174185

175186
| Exception | Description |
176-
| :----------------------------------------------------------------------------- | :---------------------------------- |
187+
|:-------------------------------------------------------------------------------|:------------------------------------|
177188
| [`Exception`](src/exceptions/Exception.ts) | Base exception class. |
178189
| [`RuntimeException`](src/exceptions/RuntimeException.ts) | Exception for runtime errors. |
179190
| [`EmptyStringException`](src/exceptions/EmptyStringException.ts) | Exception for empty strings. |
@@ -190,7 +201,7 @@ Functions for working with file paths and URLs.
190201
<details open><summary>Functions</summary>
191202

192203
| Function | Description |
193-
| :------------------------------------------- | :-------------------------------------------------------------- |
204+
|:---------------------------------------------|:----------------------------------------------------------------|
194205
| [`isAbsolute`](src/path/isAbsolute.ts) | Checks if a path is absolute. |
195206
| [`isRelative`](src/path/isRelative.ts) | Checks if a path is relative. |
196207
| [`isValidDomain`](src/path/isValidDomain.ts) | Checks if a string is a valid domain name. |
@@ -205,15 +216,15 @@ Functions for working with file paths and URLs.
205216
<details open><summary>Functions</summary>
206217

207218
| Abstract | Description |
208-
| :------------------------------- | :---------- |
219+
|:---------------------------------|:------------|
209220
| [`Class`](src/abstract/Class.ts) | |
210221

211222
</details>
212223

213224
<details open><summary>Functions</summary>
214225

215226
| Interface | Description |
216-
| :--------------------------------------- | :----------------------- |
227+
|:-----------------------------------------|:-------------------------|
217228
| [`Iterator`](src/interfaces/Iterator.ts) | Interface for iterators. |
218229

219230
</details>

src/appsscript/admin/isAdmin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { nonFunction } from "../../base";
44
* ## isAdmin
55
*
66
* Checks if the current user is an administrator of the Google Workspace domain.
7-
* Requires the `Admin SDK Directory Service` to be enabled.
7+
*
8+
* **Note:** Requires the `Admin SDK Directory Service` to be enabled.
89
*
910
* @returns `true` if the user is an administrator; otherwise, `false`.
1011
*/

src/appsscript/sheets/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export { getColumnIndexByLetter } from "./getColumnIndexByLetter";
1212
export { getColumnLetterByIndex } from "./getColumnLetterByIndex";
1313
export { getSheetById } from "./getSheetById";
1414
export { highlightHtml } from "./highlightHtml";
15+
export { sortSheets } from "./sortSheets";
1516

1617
export { isCellGridRange } from "./isCellGridRange";
1718
export { isGridRangeContainedIn } from "./isGridRangeContainedIn";
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { isSpreadsheet } from "./isSpreadsheet";
2+
3+
/**
4+
* Sorts all sheets in a spreadsheet alphabetically by name.
5+
*
6+
* @param spreadsheet - The spreadsheet object.
7+
* @param [callback] - An optional callback function for custom sorting.
8+
*/
9+
export function sortSheets(
10+
spreadsheet: GoogleAppsScript.Spreadsheet.Spreadsheet,
11+
callback?: (a: string, b: string) => number
12+
): void {
13+
if (!isSpreadsheet(spreadsheet)) {
14+
throw new Error("Spreadsheet object is not valid.");
15+
}
16+
17+
try {
18+
const sheets = spreadsheet.getSheets();
19+
20+
if (sheets.length < 2) {
21+
return;
22+
}
23+
24+
const sheetNames = sheets.map(sheet => sheet.getName());
25+
sheetNames.sort(callback);
26+
27+
sheetNames.forEach(function (name, i) {
28+
const sheet = spreadsheet.getSheetByName(name);
29+
30+
if (sheet && sheet.getIndex() !== i + 1) {
31+
spreadsheet.setActiveSheet(sheet);
32+
spreadsheet.moveActiveSheet(i + 1);
33+
}
34+
});
35+
} catch (err: unknown) {
36+
console.error(`Failed to sort sheets: ${err}`);
37+
}
38+
}

0 commit comments

Comments
 (0)