Skip to content

Commit 38b8519

Browse files
Merge pull request #78 from contributorpw/edits
docs/uncheck-checklist
2 parents a4dddbd + 95766bc commit 38b8519

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"timeZone": "Europe/Moscow",
3+
"dependencies": {
4+
"enabledAdvancedServices": [{
5+
"userSymbol": "Docs",
6+
"serviceId": "docs",
7+
"version": "v1"
8+
}]
9+
},
10+
"exceptionLogging": "STACKDRIVER",
11+
"runtimeVersion": "V8"
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "container-bound-doc",
3+
"src": []
4+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Check/uncheck the entire checklist in the Document
3+
*
4+
* @param {GoogleAppsScript.Document.Document} doc A Document
5+
* @param {string} index The checklist id
6+
*/
7+
function uncheckListById_(doc, id) {
8+
const body = doc.getBody();
9+
const lists = body.getListItems().filter((l) => l.getListId() === id);
10+
const last = lists.splice(-1)[0];
11+
lists.reverse().forEach((item) => {
12+
const copy = last.copy();
13+
copy.asListItem().setText(item.asListItem().getText());
14+
body.insertListItem(body.getChildIndex(item), copy);
15+
item.removeFromParent();
16+
});
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: 'Uncheck a checklist'
3+
date: '2021-07-09'
4+
description: 'Check/uncheck the entire checklist in Google Documents.'
5+
tags: ['docs']
6+
categories: ['snippets']
7+
images: ['./snippets/sheets/uncheck-checklist/screenshot.png']
8+
---
9+
10+
## Check/uncheck the entire checklist in Google Documents
11+
12+
{{< toc >}}
13+
14+
<video controls width="100%" height="350px" autoplay="true" loop="true">
15+
<source src="./screenrecord.mp4" type="video/mp4">
16+
Sorry, your browser doesn't support embedded videos.
17+
</video>
18+
19+
Currently it is unknown if the checklist can be manipulated in Docs. [Clark Lind](https://groups.google.com/g/google-apps-script-community/c/Gl0hcbM0xM8/m/cqsL1krtAAAJ) suggests to replace a list item with a copy of one of them (which is in expected state -- check/uncheck). We need to know the `id` -- see `printListIds()` -- of the list in order to be able to change it. And also we need to know which element is in the right state. The code used the last one.
20+
21+
> **Important**. The code does not support paragraph deep copying.
22+
23+
### Snippet
24+
25+
- {{< externalLink >}}
26+
- {{< commentLink >}}
27+
- {{< scrvizLink >}}
28+
29+
{{< codeFromFile "index.js" >}}
30+
31+
### Run it
32+
33+
{{< codeFromFile "run.js" >}}
34+
35+
### Ui triggers
36+
37+
{{< codeFromFile "triggerActions.js" >}}
38+
39+
{{< clipboard >}}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* global uncheckListById_ */
2+
3+
/**
4+
* Run the snippet
5+
*/
6+
function run() {
7+
uncheckListById_(DocumentApp.getActiveDocument(), 'kix.yy1s6bgaip7h');
8+
}
9+
10+
/**
11+
* Prints ids of lists in the current Document
12+
*/
13+
function printListIds() {
14+
console.log(getListIds_(DocumentApp.getActiveDocument()));
15+
}
16+
17+
/**
18+
* Gets ids of lists in the current Document
19+
*
20+
* @param {globalThis.DocumentApp.Document} doc
21+
*/
22+
function getListIds_(doc) {
23+
return doc
24+
.getBody()
25+
.getListItems()
26+
.reduce((a, c) => {
27+
const id = c.getListId();
28+
if (a.indexOf(id) === -1) a.push(id);
29+
return a;
30+
}, []);
31+
}
220 KB
Binary file not shown.
33.8 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Creates the user menu for handy use.
3+
*/
4+
function onOpen() {
5+
DocumentApp.getUi()
6+
.createMenu('Checklists')
7+
.addItem('Mark the list as its last item', 'run')
8+
.addToUi();
9+
}

0 commit comments

Comments
 (0)