summaryrefslogtreecommitdiff
path: root/google-docs-bibliografie-und-abbildungsverzeichnis.js
blob: 50709485b7f083807f4c2a0a2b2e8b451dc2c800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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];
}