diff --git a/snippets/docs/uncheck-checklist/appsscript.json b/snippets/docs/uncheck-checklist/appsscript.json new file mode 100644 index 0000000..6e03377 --- /dev/null +++ b/snippets/docs/uncheck-checklist/appsscript.json @@ -0,0 +1,12 @@ +{ + "timeZone": "Europe/Moscow", + "dependencies": { + "enabledAdvancedServices": [{ + "userSymbol": "Docs", + "serviceId": "docs", + "version": "v1" + }] + }, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8" +} \ No newline at end of file diff --git a/snippets/docs/uncheck-checklist/config.json b/snippets/docs/uncheck-checklist/config.json new file mode 100644 index 0000000..8899331 --- /dev/null +++ b/snippets/docs/uncheck-checklist/config.json @@ -0,0 +1,4 @@ +{ + "type": "container-bound-doc", + "src": [] +} diff --git a/snippets/docs/uncheck-checklist/index.js b/snippets/docs/uncheck-checklist/index.js new file mode 100644 index 0000000..fa8569a --- /dev/null +++ b/snippets/docs/uncheck-checklist/index.js @@ -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(); + }); +} diff --git a/snippets/docs/uncheck-checklist/readme.md b/snippets/docs/uncheck-checklist/readme.md new file mode 100644 index 0000000..0957e7d --- /dev/null +++ b/snippets/docs/uncheck-checklist/readme.md @@ -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 >}} + + + +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 >}} diff --git a/snippets/docs/uncheck-checklist/run.js b/snippets/docs/uncheck-checklist/run.js new file mode 100644 index 0000000..4a7cba0 --- /dev/null +++ b/snippets/docs/uncheck-checklist/run.js @@ -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; + }, []); +} diff --git a/snippets/docs/uncheck-checklist/screenrecord.mp4 b/snippets/docs/uncheck-checklist/screenrecord.mp4 new file mode 100644 index 0000000..d2bd4e7 Binary files /dev/null and b/snippets/docs/uncheck-checklist/screenrecord.mp4 differ diff --git a/snippets/docs/uncheck-checklist/screenshot.png b/snippets/docs/uncheck-checklist/screenshot.png new file mode 100644 index 0000000..5ad461e Binary files /dev/null and b/snippets/docs/uncheck-checklist/screenshot.png differ diff --git a/snippets/docs/uncheck-checklist/triggerActions.js b/snippets/docs/uncheck-checklist/triggerActions.js new file mode 100644 index 0000000..4b0b718 --- /dev/null +++ b/snippets/docs/uncheck-checklist/triggerActions.js @@ -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(); +}