forked from chku2469/Bookmarklet-to-export-HTML-table-to-CSV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookmarklet to export Confluence table to CSV.html
More file actions
197 lines (183 loc) · 6.45 KB
/
Copy pathBookmarklet to export Confluence table to CSV.html
File metadata and controls
197 lines (183 loc) · 6.45 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
<!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;
position: relative;
top: -2px;
}
img.inline {
position: relative;
top: 3px;
}
ol.wide li, div.wide {
margin: 5px 0px 5px 0px;
}
span.link {
color: blue;
text-decoration: underline;
}
</style>
</head>
<body>
<p>Usage:</p>
<ol class="wide">
<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, prefix = '') {
var result = jQuery(selector).html();
if(result.indexOf('<br>') < 0) {
result = prefix + jQuery(selector).text();
} else {
result = result.
replace(/<br *\/?>/g, '\r\n'). /* replace <br> */
replace(/[\x20\x09]*[\r\n]+[\x20\x09]*/g,
'\r\n'); /* normalize line breaks and surrounding space */
}
return '\r\n' + result.trim();
}
function convertTableCell(cell) {
var result = '';
var blocks = jQuery('div, ul, p', cell);
if(blocks.length) {
blocks.each(function() {
if(this.nodeName == 'UL') {
jQuery('li', this).each(function() {
if(result == '') { result += '\x27'; }
result += getParaText(this, '\u2022 ');
});
} else {
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=iso-8859-1,%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=\x22#\x22 class=\x22csvLink\x22 ' +
'data-forcsvtable=\x22' + index + '\x22>Export table to CSV</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 class="wide"> 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>
<li>The script then will display a link <span class="link">Export table to CSV</span> above
each table on the current page.</li>
<li>In order to export the table below the link simply click on the link.</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>,
other sources are <a href="https://gist.github.com/ddikman/c915abaa1ea63606ebe33fa93a379d7a"
>gist.github.com/ddikman</a> and <a href="https://iamdav.in/2014/10/24/bookmarklet-creating-csv-html-table/">iamdav.in/2014/10/24</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>