function onOpen() { DocumentApp.getUi().createMenu('Verzeichnis-Tools') .addItem('Bibliografie bauen', 'buildBibliografie') .addItem('Abbildungsverzeichnis bauen', 'buildAbbildungsverzeichnis') .addToUi(); } function buildBibliografie() { var document = DocumentApp.getActiveDocument(); var body = document.getBody(); var footnotes = document.getFootnotes(); var footnotesArray = []; for (var idx in footnotes) { var footnote = footnotes[idx].getFootnoteContents(); footnotesArray.push(footnote.getText().replace(/^\s?(Vgl\. )?/, '')); } footnotesArray.sort(); var resultText = ''; for (var idx in footnotesArray) { resultText += (footnotesArray[idx] + '\n\n'); } getLastParagraph().appendText('\n\n---Bibliografie-beep-boop---\n\n' + resultText + '---Bibliografie-beep-boop---\n\n'); } function buildAbbildungsverzeichnis() { var document = DocumentApp.getActiveDocument(); var body = document.getBody(); var images = getImages(); var resultText = ''; for (var idx in images) { var image = images[idx]; resultText += (image.footNote.getText() + '\n\n'); } getLastParagraph().appendText('\n\n---Abbildungsverzeichnis-beep-boop---\n\n' + resultText + '---Abbildungsverzeichnis-beep-boop---\n\n'); } function getImages() { var document = DocumentApp.getActiveDocument(); var body = document.getBody(); var images = body.getImages(); var result = []; for (var idx in images) { var image = images[idx]; var footNote = getFootNote(image); var pageNumber = getPageNumber(image); result.push({ image, footNote, pageNumber, }); } return result; } function getFootNote(image) { var paragraph = getImmediateElement(image, DocumentApp.ElementType.PARAGRAPH); return paragraph.editAsText(); } function getPageNumber(element) { return '00'; } function getRangeElement(element) { var document = DocumentApp.getActiveDocument(); var range = document.newRange(); range.addElement(element); range.build(); var rangeElements = range.getRangeElements(); return rangeElements[0]; } function getImmediateElement(element, elementType) { var document = DocumentApp.getActiveDocument(); var body = document.getBody(); var foundElement = body.findElement(elementType, getRangeElement(element)); return foundElement.getElement(); } function getLastParagraph() { var document = DocumentApp.getActiveDocument(); var body = document.getBody(); var paragraphs = body.getParagraphs(); return paragraphs[paragraphs.length - 1]; }