Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions snippets/docs/uncheck-checklist/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"timeZone": "Europe/Moscow",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Docs",
"serviceId": "docs",
"version": "v1"
}]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
4 changes: 4 additions & 0 deletions snippets/docs/uncheck-checklist/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "container-bound-doc",
"src": []
}
17 changes: 17 additions & 0 deletions snippets/docs/uncheck-checklist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Check/uncheck the entire checklist in the Document
*
* @param {GoogleAppsScript.Document.Document} doc A Document
* @param {string} index The checklist id
*/
function uncheckListById_(doc, id) {
const body = doc.getBody();
const lists = body.getListItems().filter((l) => l.getListId() === id);
const last = lists.splice(-1)[0];
lists.reverse().forEach((item) => {
const copy = last.copy();
copy.asListItem().setText(item.asListItem().getText());
body.insertListItem(body.getChildIndex(item), copy);
item.removeFromParent();
});
}
39 changes: 39 additions & 0 deletions snippets/docs/uncheck-checklist/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: 'Uncheck a checklist'
date: '2021-07-09'
description: 'Check/uncheck the entire checklist in Google Documents.'
tags: ['docs']
categories: ['snippets']
images: ['./snippets/sheets/uncheck-checklist/screenshot.png']
---

## Check/uncheck the entire checklist in Google Documents

{{< toc >}}

<video controls width="100%" height="350px" autoplay="true" loop="true">
<source src="./screenrecord.mp4" type="video/mp4">
Sorry, your browser doesn't support embedded videos.
</video>

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.

> **Important**. The code does not support paragraph deep copying.

### Snippet

- {{< externalLink >}}
- {{< commentLink >}}
- {{< scrvizLink >}}

{{< codeFromFile "index.js" >}}

### Run it

{{< codeFromFile "run.js" >}}

### Ui triggers

{{< codeFromFile "triggerActions.js" >}}

{{< clipboard >}}
31 changes: 31 additions & 0 deletions snippets/docs/uncheck-checklist/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* global uncheckListById_ */

/**
* Run the snippet
*/
function run() {
uncheckListById_(DocumentApp.getActiveDocument(), 'kix.yy1s6bgaip7h');
}

/**
* Prints ids of lists in the current Document
*/
function printListIds() {
console.log(getListIds_(DocumentApp.getActiveDocument()));
}

/**
* Gets ids of lists in the current Document
*
* @param {globalThis.DocumentApp.Document} doc
*/
function getListIds_(doc) {
return doc
.getBody()
.getListItems()
.reduce((a, c) => {
const id = c.getListId();
if (a.indexOf(id) === -1) a.push(id);
return a;
}, []);
}
Binary file added snippets/docs/uncheck-checklist/screenrecord.mp4
Binary file not shown.
Binary file added snippets/docs/uncheck-checklist/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions snippets/docs/uncheck-checklist/triggerActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Creates the user menu for handy use.
*/
function onOpen() {
DocumentApp.getUi()
.createMenu('Checklists')
.addItem('Mark the list as its last item', 'run')
.addToUi();
}