-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate links test helper.js
112 lines (100 loc) · 3.3 KB
/
Validate links test helper.js
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
104
105
106
107
108
109
110
111
112
function validateLinksTestHelper() {
const ui = getUi();
const linksArray = [];
if (HOST_APP == 'docs') {
// Doc part
const doc = DocumentApp.getActiveDocument();
const element = doc.getBody();
testingFindAllLinks(element, 'body', linksArray);
const footnotes = doc.getFootnotes();
let footnote, numChildren;
for (let i in footnotes) {
footnote = footnotes[i].getFootnoteContents();
if (footnote == null) {
alertSuggestedFootnoteBug(i);
continue;
}
numChildren = footnote.getNumChildren();
for (let j = 0; j < numChildren; j++) {
testingFindAllLinks(footnote.getChild(j), 'footnotes', linksArray);
}
}
// End. Doc part
} else {
// Slides part
const slides = SlidesApp.getActivePresentation().getSlides();
let url, linkText, runs;
for (let i in slides) {
slides[i].getPageElements().forEach(function (pageElement) {
if (pageElement.getPageElementType() == SlidesApp.PageElementType.SHAPE) {
runs = pageElement.asShape().getText().getRuns();
for (let j in runs) {
// Logger.log(runs[j].getStartIndex());
// Logger.log(runs[j].asString());
// Logger.log(runs[j].getLinks());
links = runs[j].getLinks();
for (let m in links) {
//linksArray.push(links[j].getTextStyle().getLink().getUrl());
url = links[m].getTextStyle().getLink().getUrl();
// linkText = links[m].getTextStyle().getLink();
linkText = runs[j].asString();
linksArray.push({ link: url, linkText: linkText });
}
}
}
});
}
//End. Slides part
}
//Logger.log(linksArray);
let allLinks = '';
for (let i in linksArray) {
allLinks += `
<br>
${linksArray[i].linkText.replace('<', '<').replace('>', '>')}
<br>
<a target="_blank" href="${linksArray[i].link}">${linksArray[i].link}</a>
<br>
`;
}
let html = `<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
${allLinks}
</body>
</html>`;
html = HtmlService.createHtmlOutput(html).setWidth(800).setHeight(800);
ui.showModalDialog(html, 'Links');
}
function testingFindAllLinks(element, source, linksArray) {
let text, end, indices, partAttributes, numChildren;
const elementType = String(element.getType());
if (elementType == 'TEXT') {
indices = element.getTextAttributeIndices();
//Logger.log(indices);
for (let i = 0; i < indices.length; i++) {
partAttributes = element.getAttributes(indices[i]);
//Logger.log(partAttributes);
if (partAttributes.LINK_URL) {
text = element.getText();
if (i == indices.length - 1) {
end = text.length - 1;
} else {
end = indices[i + 1] - 1;
}
linksArray.push({ link: partAttributes.LINK_URL, linkText: text.substr(indices[i], end - indices[i] + 1), source: source });
}
}
} else {
const arrayTypes = ['BODY_SECTION', 'PARAGRAPH', 'LIST_ITEM', 'TABLE', 'TABLE_ROW', 'TABLE_CELL'];
if (arrayTypes.includes(elementType)) {
numChildren = element.getNumChildren();
for (let i = 0; i < numChildren; i++) {
testingFindAllLinks(element.getChild(i), source, linksArray);
}
}
}
}