Skip to content

Commit 99dc027

Browse files
Merge pull request #64 from contributorpw/edits
2 parents e47ceec + 814e9c0 commit 99dc027

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"timeZone": "Europe/Moscow",
3+
"exceptionLogging": "STACKDRIVER",
4+
"runtimeVersion": "V8"
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "container-bound-sheet",
3+
"src": []
4+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* TOC object
3+
* @typedef {Array.<{
4+
* name: string,
5+
* gid: number,
6+
* richTextValueBuilder: GoogleAppsScript.Spreadsheet.RichTextValueBuilder
7+
* }>} TOCBuild
8+
*/
9+
10+
/**
11+
* Build a TOC object from the passed book
12+
* @param {GoogleAppsScript.Spreadsheet.Spreadsheet} book
13+
* @returns {TOCBuild}
14+
*/
15+
function tocBuilder_(book = SpreadsheetApp.getActive()) {
16+
return book
17+
.getSheets()
18+
.map((sheet) => ({
19+
name: sheet.getName(),
20+
gid: sheet.getSheetId(),
21+
}))
22+
.map(
23+
(item) => (
24+
(item.richTextValueBuilder = createRichTextValueLink_(
25+
item.name,
26+
`#gid=${item.gid}`
27+
)),
28+
item
29+
)
30+
);
31+
}
32+
33+
/**
34+
* Update the specific Range with the TOC object
35+
* @param {TOCBuild} tocBuild
36+
* @param {GoogleAppsScript.Spreadsheet.Range} range
37+
* @returns {GoogleAppsScript.Spreadsheet.Range} The updated range
38+
*/
39+
function tocUpdater_(tocBuild, range) {
40+
range
41+
.getSheet()
42+
.getRange(range.getRow(), range.getColumn(), tocBuild.length, 1)
43+
.setRichTextValues(
44+
tocBuild.map((item) => [item.richTextValueBuilder.build()])
45+
);
46+
}
47+
48+
/**
49+
* Create a RichTextValueBuilder with the text and the linkUrl
50+
* @param {string} text
51+
* @param {string} linkUrl
52+
* @returns {GoogleAppsScript.Spreadsheet.RichTextValueBuilder}
53+
*/
54+
function createRichTextValueLink_(text, linkUrl) {
55+
return SpreadsheetApp.newRichTextValue().setText(text).setLinkUrl(linkUrl);
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: 'TOC generator for Google Sheets'
3+
date: '2021-03-18'
4+
description: 'Generates a table of contents for a specific Spreadsheet'
5+
tags: ['sheets']
6+
categories: ['snippets']
7+
images: ['./snippets/sheets/toc_generator/screenrecord.gif']
8+
---
9+
10+
{{< toc >}}
11+
12+
## TOC generator for Google Sheets
13+
14+
![Snippet of TOC generator for Google Sheets](./screenrecord.gif)
15+
16+
### Snippet
17+
18+
- {{< externalLink >}}
19+
- {{< commentLink >}}
20+
- {{< scrvizLink >}}
21+
22+
{{< codeFromFile "index.js" >}}
23+
24+
### Run it
25+
26+
{{< codeFromFile "run.js" >}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* global tocBuilder_ tocUpdater_*/
2+
3+
/**
4+
* Runs the snippet.
5+
* Generates a TOC to 'Sheet27!B2' without 'Sheet27' item.
6+
*/
7+
function run() {
8+
const excludeSheetNames = ['Sheet27'];
9+
const book = SpreadsheetApp.getActive();
10+
const tocBuild = tocBuilder_(book).filter(
11+
(item) => excludeSheetNames.indexOf(item.name) === -1
12+
);
13+
const range = book.getRange('Sheet27!B2:B').clearContent();
14+
tocUpdater_(tocBuild, range);
15+
range.activate();
16+
}
17+
18+
/**
19+
* Create menu for handy use
20+
*/
21+
function onOpen() {
22+
SpreadsheetApp.getUi()
23+
.createMenu('Apps Script Snippets')
24+
.addItem('Generate TOC', 'run')
25+
.addToUi();
26+
}

0 commit comments

Comments
 (0)