-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bookmarklet to export Confluence table to CSV.html
184 lines (171 loc) · 6.11 KB
/
Bookmarklet to export Confluence table to CSV.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!doctype HTML public "-//W3O//DTD W3 HTML 3.2//EN">
<html><head>
<title>Export Confluence table to CSV</title>
<style type="text/css">
body { font-family: Arial }
a.drag:hover {
border: 1px solid #44b;
color: #00f;
border-radius: 5px;
box-shadow: 4px 4px 6px #77a;
background-color: #fff;
text-shadow: none;
}
a.drag {
text-decoration: none;
box-shadow: 1px 1px 1px #aaa;
background-color: #f8f8f8;
color: #ddf;
text-shadow: -1px -1px 0px #00a;
}
.button-like {
border: 1px solid #aaa;
padding: 2pt;
}
.button {
font-size: 9pt;
background-color: #dad7d0;
border-radius: 5pt;
}
img.inline {
position: relative;
top: 3px;
}
</style>
</head>
<body>
<p>Usage:</p>
<ol>
<li>Just <b>drag the following link</b> to the bookmark area or menu of your browser:
<a class="button-like drag" href="javascript:(function(){
function provideJqueryAndCall(success) {
if(typeof jQuery != 'undefined') {
success();
return;
}
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-latest.min.js';
var head = document.getElementsByTagName('head')[0];
var done = false;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
function getParaText(selector) {
var result = jQuery(selector).html();
if(result.indexOf('<br>') < 0) {
result = jQuery(selector).text();
} else {
result = result.
replace(/<br *\/?>/g, '\r\n'). // replace <br>
replace(/[\x20\x09]*(\r\n|\n|\r)+[\x20\x09]*/g,
'\r\n'); // normalise line breaks and surrounding space
}
return '\r\n' + result.trim();
}
function convertTableCell(cell) {
var result = '';
if(jQuery('div', cell).length) {
jQuery('div', cell).each(function(){
result += getParaText(this);
});
} else if(jQuery('ul', cell).length) {
jQuery('ul > li', cell).each(function(){
result += getParaText(this);
});
} else if(jQuery('p', cell).length) {
jQuery('p', cell).each(function(){
result += getParaText(this);
});
} else {
result += getParaText(cell);
}
return result.
replace(/\xa0/g, '\x20'). // non-breaking spaces
replace(/[\x20\x09]+/g, '\x20'). // normalise spaces and tabs
replace(/\x22/g, '\x22\x22'). // double quotes
trim();
}
function downloadCSVFile(filename, mime, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:' + mime +
';charset=utf-8,%EF%BB%BF' + encodeURIComponent(text));
pom.setAttribute('download', filename);
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
function addCSVLinks() {
jQuery('.csvLink').remove();
jQuery('table').each(function(index){
jQuery(this).attr('data-csvtable', index)
.before('<a href=\22#\x22 class=\x22csvLink\x22 ' +
'data-forcsvtable=\x22' + index + '\x22>Export table to CSV file</a>');
});
jQuery('.csvLink').click(function(){
var text = '';
var csvTableIndex = jQuery(this).attr('data-forcsvtable');
jQuery('table[data-csvtable=\x22' + csvTableIndex + '\x22] tr').each(function(){
if(jQuery(this).parent(':hidden').length) {
return;
}
jQuery('td, th', this).each(function(index){
if(index != 0) { text += ';'; }
text += '\x22';
text += convertTableCell(this);
text += '\x22';
});
text += '\r\n';
});
jQuery('.csvLink').remove();
downloadCSVFile('Confluence-Table.csv', 'text/csv', text);
});
}
provideJqueryAndCall(function(){ addCSVLinks(); });
})();">Export Confluence table to CSV</a>
<div style="margin: 5px 0px 5px 0px;"> or use right-click and select
<span class="button button-like">Bookmark This Link</span>.</div></li>
<li>Visit a Confluence page that contains a table.</li>
<li>In the bookmark area or menu of your browser: press the button <span class="button button-like"
><img class="inline" src="globe.png"/> Export Confluence table to CSV</span>.</li>
</ol>
<div style="padding-top: 70px;" />
<div style="padding: 1px 10px 1px 10px;
border: 1px solid #ccc;
border-radius: 10px;
width: 800px;
background-color: #f4f4f4;
color: #666;
font-size: 11pt;
">
<p>The original code was copied from <a href="http://mydailyissues.blogspot.com/2016/08/javascript-export-altassian-confluence.html"
>mydailyissues.blogspot.com/2016/08</a>,
another source is at <a href="https://gist.github.com/ddikman/c915abaa1ea63606ebe33fa93a379d7a"
>gist.github.com/ddikman</a>.</p>
<p>Additionally I wanted to </p>
<ul>
<li>add line breaks between specific elements
like <tt><div></tt>, <tt><ul></tt>/<tt><li></tt>
and <tt><p></tt></li>
<li>normalize whitespace, i.e. leave only a single whitespace in all places
where there could have been multiple whitespaces in the input</li>
<li>remove the repetition of the header row.</li>
</ul>
<p>To enhance the script I first was looking for a way to change the script
and test it as fast as possible,<br /> i.e. I was looking to minimise the turnaround
times between edit, execute, evaluate results, repeat.</p>
<p>Therefore I </p>
<ol>
<li>created a local test file <tt>synthetic.html</tt>
<li>adapted the script to execute automatically on reload</li>
<li>inserted all special test cases from the confluence table that I wanted to convert</li>
<li>added some functions to replace white space characters in the result by visible unicode characters</li>
</ol>
</div>
</body></html>