forked from thejimbirch/GoogleDoc2Html
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.js
544 lines (475 loc) · 16.4 KB
/
code.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
var docUrl;
var properties = PropertiesService.getScriptProperties();
function doGet(e) {
const req = e.parameter;
docUrl = req.url;
var html;
if(req.type === "time"){
const lastUpdated = getLastUpdated().toJSON();
return ContentService.createTextOutput(lastUpdated);
}else if(req.type === "html"){
try{
html = ConvertGoogleDocToCleanHtml();
}catch{
html = ConvertGoogleSheetToCleanHtml();
}
return HtmlService.createHtmlOutput(html);
}else{
try{
html = ConvertGoogleDocToCleanHtml();
}catch{
html = ConvertGoogleSheetToCleanHtml();
}
return ContentService.createTextOutput(html);
}
}
function test(){
docUrl = "https://docs.google.com/document/d/1B7ZEh4xrX3dsFSq3LF9yhrTdoQ9tsqexn1IfCWtd0Rc/edit?usp=sharing"; //replace this with your own document
Logger.log(getLastUpdated().toJSON());
var html;
try{
html = ConvertGoogleDocToCleanHtml();
}catch{
html = ConvertGoogleSheetToCleanHtml();
}
Logger.log(html);
}
function getLastUpdated(){
try{
const docId = DocumentApp.openByUrl(docUrl).getId();
return DriveApp.getFileById(docId).getLastUpdated();
}catch{
const docId = SpreadsheetApp.openByUrl(docUrl).getId();
return DriveApp.getFileById(docId).getLastUpdated();
}
}
function ConvertGoogleDocToCleanHtml() {
//var body = DocumentApp.getActiveDocument().getBody();
trashSavedImages();
var doc = DocumentApp.openByUrl(docUrl);
var body = doc.getBody();
var numChildren = body.getNumChildren();
var output = [];
var listCounters = {};
// Walk through all the child elements of the body.
for (var i = 0; i < numChildren; i++) {
var child = body.getChild(i);
output.push(processItem(child, listCounters));
}
var html = output.join('\r');
//emailHtml(html, images);
//createDocumentForHtml(html, images);
return html;
}
function dumpAttributes(atts) {
// Log the paragraph attributes.
for (var att in atts) {
Logger.log(att + ":" + atts[att]);
}
}
function processItem(item, listCounters) {
var output = [];
var prefix = "", suffix = "";
var style = "";
var hasPositionedImages = false;
if (item.getPositionedImages) {
positionedImages = item.getPositionedImages();
hasPositionedImages = true;
}
var itemType = item.getType();
if (itemType == DocumentApp.ElementType.PARAGRAPH) {
//https://developers.google.com/apps-script/reference/document/paragraph
if (item.getNumChildren() == 0) {
return "<br />";
}
var p = "";
if (item.getIndentStart() != null) {
p += "margin-left:" + item.getIndentStart() + "; ";
}
if (item.getIndentFirstLine() != null) {
p += "text-indent:" + (item.getIndentFirstLine() - item.getIndentStart()) + "; ";
}
if (item.getIndentEnd() != null) {
p += "margin-right:" + item.getIndentEnd() + "; ";
}
if(item.getLineSpacing() != null){
p += "line-height: " + item.getLineSpacing() + "; ";
}
if(item.getSpacingBefore() != null){
p += "margin-top: " + item.getSpacingBefore() + "; ";
}else{
p += "margin-top: 0; ";
}
if(item.getSpacingAfter() != null){
p += "margin-bottom: " + item.getSpacingAfter() + "; ";
}else{
p += "margin-bottom: 0; ";
}
//Text Alignment
switch (item.getAlignment()) {
// Add a # for each heading level. No break, so we accumulate the right number.
//case DocumentApp.HorizontalAlignment.LEFT:
// p += "text-align: left;"; break;
case DocumentApp.HorizontalAlignment.CENTER:
p += "text-align: center;";
break;
case DocumentApp.HorizontalAlignment.RIGHT:
p += "text-align: right;";
break;
case DocumentApp.HorizontalAlignment.JUSTIFY:
p += "text-align: justify;";
break;
default:
p += "";
}
//TODO: getLineSpacing(line-height), getSpacingBefore(margin-top), getSpacingAfter(margin-bottom),
//TODO:
//INDENT_END Enum The end indentation setting in points, for paragraph elements.
//INDENT_FIRST_LINE Enum The first line indentation setting in points, for paragraph elements.
//INDENT_START Enum The start indentation setting in points, for paragraph elements.
if (p !== "") {
style = ' style="' + p + '"';
}
//TODO: add DocumentApp.ParagraphHeading.TITLE, DocumentApp.ParagraphHeading.SUBTITLE
//Heading or only paragraph
switch (item.getHeading()) {
// Add a # for each heading level. No break, so we accumulate the right number.
case DocumentApp.ParagraphHeading.HEADING6:
prefix = "<h6" + style + ">", suffix = "</h6>"; break;
case DocumentApp.ParagraphHeading.HEADING5:
prefix = "<h5" + style + ">", suffix = "</h5>"; break;
case DocumentApp.ParagraphHeading.HEADING4:
prefix = "<h4" + style + ">", suffix = "</h4>"; break;
case DocumentApp.ParagraphHeading.HEADING3:
prefix = "<h3" + style + ">", suffix = "</h3>"; break;
case DocumentApp.ParagraphHeading.HEADING2:
prefix = "<h2" + style + ">", suffix = "</h2>"; break;
case DocumentApp.ParagraphHeading.HEADING1:
prefix = "<h1" + style + ">", suffix = "</h1>"; break;
default:
prefix = "<p" + style + ">", suffix = "</p>";
}
var attr = item.getAttributes();
} else if (itemType === DocumentApp.ElementType.INLINE_IMAGE) {
processImage(item, output);
} else if (itemType === DocumentApp.ElementType.INLINE_DRAWING) {
//TODO
Logger.log("INLINE_DRAWING: " + JSON.stringify(item));
} else if (itemType === DocumentApp.ElementType.LIST_ITEM) {
var listItem = item;
var gt = listItem.getGlyphType();
var key = listItem.getListId() + '.' + listItem.getNestingLevel();
var counter = listCounters[key] || 0;
var p = "";
if(item.getLineSpacing() != null){
p += "line-height: " + item.getLineSpacing() + "; ";
}
if(item.getSpacingBefore() != null){
p += "margin-top: " + item.getSpacingBefore() + "; ";
}else{
p += "margin-top: 0; ";
}
if(item.getSpacingAfter() != null){
p += "margin-bottom: " + item.getSpacingAfter() + "; ";
}else{
p += "margin-bottom: 0; ";
}
if (p !== "") {
style = ' style="' + p + '"';
}
// First list item
if ( counter == 0 ) {
// Bullet list (<ul>):
if (gt === DocumentApp.GlyphType.BULLET
|| gt === DocumentApp.GlyphType.HOLLOW_BULLET
|| gt === DocumentApp.GlyphType.SQUARE_BULLET) {
prefix = "<ul><li" + style + ">", suffix = "</li>";
} else {
// Ordered list (<ol>):
prefix = "<ol><li" + style + ">", suffix = "</li>";
}
}
else {
prefix = "<li" + style + ">";
suffix = "</li>";
}
var nextSibling = listItem.getNextSibling();
var nestingLevel = listItem.getNestingLevel();
while(nestingLevel >= 0 &&
(item.isAtDocumentEnd() || (
nextSibling && (
nextSibling.getType() != DocumentApp.ElementType.LIST_ITEM ||
nextSibling.getNestingLevel() < nestingLevel
)))) {
if (gt === DocumentApp.GlyphType.BULLET
|| gt === DocumentApp.GlyphType.HOLLOW_BULLET
|| gt === DocumentApp.GlyphType.SQUARE_BULLET) {
suffix += "</ul>";
}
else {
// Ordered list (<ol>):
suffix += "</ol>";
}
listCounters[listItem.getListId() + '.' + nestingLevel] = 0;
nestingLevel--;
}
if(nestingLevel == listItem.getNestingLevel()){
counter++;
listCounters[key] = counter;
}
} else if (itemType === DocumentApp.ElementType.TABLE) {
var row = item.getRow(0)
var numCells = row.getNumCells();
var tableWidth = 0;
for (var i = 0; i < numCells; i++) {
if(item.getColumnWidth(i) != null){
tableWidth += item.getColumnWidth(i);
}else{
tableWidth = 0; //use default width
break;
}
//Logger.log(item.getColumnWidth(i))
}
//Logger.log("TABLE tableWidth: " + tableWidth);
if(tableWidth == 0){
//todo: tableWidth = width of the document;
}
//https://stackoverflow.com/questions/339923/set-cellpadding-and-cellspacing-in-css
var p = "border-collapse: collapse; ";
if(tableWidth != 0){
p += "width: " + tableWidth + "; ";
}
if (p !== "") {
style = ' style="' + p + '"';
}
prefix = '<table' + style + '>\r', suffix = "</table>";
//Logger.log("TABLE: " + JSON.stringify(item));
} else if (itemType === DocumentApp.ElementType.TABLE_ROW) {
var minimumHeight = item.getMinimumHeight();
//Logger.log("TABLE_ROW getMinimumHeight: " + minimumHeight);
var p = "";
if(minimumHeight != null){
p += "height: " + minimumHeight + "; ";
}
if (p !== "") {
style = ' style="' + p + '"';
}
prefix = "<tr" + style + ">\r", suffix = "</tr>";
//Logger.log("TABLE_ROW: " + JSON.stringify(item));
} else if (itemType === DocumentApp.ElementType.TABLE_CELL) {
/*
BACKGROUND_COLOR Enum The background color of an element (Paragraph, Table, etc) or document.
BORDER_COLOR Enum The border color, for table elements.
BORDER_WIDTH Enum The border width in points, for table elements.
PADDING_BOTTOM Enum The bottom padding setting in points, for table cell elements.
PADDING_LEFT Enum The left padding setting in points, for table cell elements.
PADDING_RIGHT Enum The right padding setting in points, for table cell elements.
PADDING_TOP Enum The top padding setting in points, for table cell elements.
VERTICAL_ALIGNMENT Enum The vertical alignment setting, for table cell elements.
WIDTH Enum The width setting, for table cell and image elements.
*/
//https://wiki.selfhtml.org/wiki/HTML/Tabellen/Zellen_verbinden
var colSpan = item.getColSpan();
//Logger.log("TABLE_CELL getColSpan: " + colSpan);
// colspan="3"
var rowSpan = item.getRowSpan();
//Logger.log("TABLE_CELL getRowSpan: " + rowSpan);
// rowspan ="3"
var span = "";
if(colSpan != 1){
span += " colspan=" + colSpan;
}
if(rowSpan != 1){
span += " rowspan=" + rowSpan;
}
if(colSpan == 0 || rowSpan == 0){
return "";
// this cell is disabled
}
//TODO: WIDTH must be recalculated in percent
var atts = item.getAttributes();
var p = "border: 1px solid black; padding: 5px; ";
if (atts.WIDTH != null){
p += "width: " + atts.WIDTH + "; ";
}
if (p !== "") {
style = ' style="' + p + '"';
}
prefix = '<td' + style + span + '>', suffix = "</td>\r";
//Logger.log("TABLE_CELL: " + JSON.stringify(item));
} else if (itemType === DocumentApp.ElementType.FOOTNOTE) {
//TODO
var note = item.getFootnoteContents();
var counter = footnotes.length + 1;
output.push("<sup><a name='link" + counter + "' href='#footnote" + counter + "'>[" + counter + "]</a></sup>");
var newFootnote = "<aside class='footnote' epub:type='footnote' id='footnote" + counter + "'><a name='footnote" + counter + "' epub:type='noteref'>[" + counter + "]</a>";
var numChildren = note.getNumChildren();
for (var i = 0; i < numChildren; i++) {
var child = note.getChild(i);
newFootnote += processItem(child, listCounters, images, imagesOptions, footnotes);
}
newFootnote += "<a href='#link" + counter + "' id='#link" + counter + "'>↩</a></aside>"
footnotes.push(newFootnote);
Logger.log("FOOTNOTE: " + JSON.stringify(item));
} else if (itemType === DocumentApp.ElementType.HORIZONTAL_RULE) {
output.push("<hr />");
//Logger.log("HORIZONTAL_RULE: " + JSON.stringify(item));
} else if (itemType === DocumentApp.ElementType.UNSUPPORTED) {
Logger.log("UNSUPPORTED: " + JSON.stringify(item));
}
output.push(prefix);
if (hasPositionedImages === true) {
//todo
//processPositionedImages(positionedImages, images, output, imagesOptions);
Logger.log("hasPositionedImages");
}
if (item.getType() == DocumentApp.ElementType.TEXT) {
processText(item, output);
}
else {
if (item.getNumChildren) {
var numChildren = item.getNumChildren();
// Walk through all the child elements of the doc.
for (var i = 0; i < numChildren; i++) {
var child = item.getChild(i);
output.push(processItem(child, listCounters));
}
}
}
output.push(suffix);
return output.join('');
}
function processText(item, output) {
var text = item.getText();
var indices = item.getTextAttributeIndices();
for (var i=0; i < indices.length; i ++) {
var partAtts = item.getAttributes(indices[i]);
var startPos = indices[i];
var endPos = i+1 < indices.length ? indices[i+1]: text.length;
var partText = text.substring(startPos, endPos);
var mylink = item.getLinkUrl(startPos);
partText = partText.replace(new RegExp("(\r)", 'g'), "<br />\r");
//Logger.log(partText);
var style = "";
//TODO if only ITALIC use: <blockquote></blockquote>
//TODO: change html tags to css (i, strong, u)
if (partAtts.LINK_URL) {
output.push('<a href="' + mylink + '">');
}
//css font-style:italic;
if (partAtts.ITALIC) {
output.push('<i>');
}
//css font-weight: bold;
if (partAtts.BOLD) {
output.push('<strong>');
}
//css text-decoration: underline
if (partAtts.UNDERLINE) {
output.push('<u>');
}
// font family, color and size changes disabled
/*if (partAtts.FONT_FAMILY) {
style = style + 'font-family: ' + partAtts.FONT_FAMILY + '; ';
}
if (partAtts.FONT_SIZE) {
var pt = partAtts.FONT_SIZE;
var em = pixelToEm(pointsToPixel(pt));
style = style + 'font-size: ' + pt + 'pt; font-size: ' + em + 'em; ';
}
if (partAtts.FOREGROUND_COLOR) {
style = style + 'color: ' + partAtts.FOREGROUND_COLOR + '; '; //partAtts.FOREGROUND_COLOR
}
if (partAtts.BACKGROUND_COLOR) {
style = style + 'background-color: ' + partAtts.BACKGROUND_COLOR + '; ';
}*/
if (partAtts.STRIKETHROUGH) {
style = style + 'text-decoration: line-through; ';
}
var a = item.getTextAlignment(startPos);
if (a !== DocumentApp.TextAlignment.NORMAL && a !== null) {
if (a === DocumentApp.TextAlignment.SUBSCRIPT) {
style = style + 'vertical-align : sub; font-size : 60%; ';
} else if (a === DocumentApp.TextAlignment.SUPERSCRIPT) {
style = style + 'vertical-align : super; font-size : 60%; ';
}
}
tabs = 0;
while (partText !== "" && partText[0] === "\t"){
partText = partText.slice(1);
tabs++;
}
if (tabs !== 0){
style += 'margin-left: ' + (tabs * 36) + "; ";
}
if (style !== "") {
style = ' style="' + style + '"';
}
// If someone has written [xxx] and made this whole text some special font, like superscript
// then treat it as a reference and make it superscript.
// Unfortunately in Google Docs, there's no way to detect superscript
if (partText.indexOf('[')==0 && partText[partText.length-1] == ']') {
output.push('<sup' + style + '>' + partText + '</sup>');
}
else if (partText.trim().indexOf('http://') == 0) {
output.push('<a' + style + ' href="' + partText + '" rel="nofollow">' + partText + '</a>');
}
else if (partText.trim().indexOf('https://') == 0) {
output.push('<a' + style + ' href="' + partText + '" rel="nofollow">' + partText + '</a>');
}
else {
output.push('<span' + style + '>' + partText + '</span>');
}
if (partAtts.ITALIC) {
output.push('</i>');
}
if (partAtts.BOLD) {
output.push('</strong>');
}
if (partAtts.UNDERLINE) {
output.push('</u>');
}
if (partAtts.LINK_URL) {
output.push('</a>');
}
}
}
function processImage(item, output)
{
var blob = item.getBlob().copyBlob();
var contentType = blob.getContentType();
var extension = "";
if (/\/png$/.test(contentType)) {
extension = ".png";
} else if (/\/gif$/.test(contentType)) {
extension = ".gif";
} else if (/\/jpe?g$/.test(contentType)) {
extension = ".jpg";
} else {
throw "Unsupported image type: "+contentType;
}
blob.setName("Doc2HTML_Image" + extension);
var file = DriveApp.createFile(blob);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
const imageUrl = file.getDownloadUrl();
const width = item.getWidth();
const height = item.getHeight();
output.push(`<img src="${imageUrl}" width=${width} height=${height}/>`);
var ids = properties.getProperty(docUrl);
if(ids == null){
ids = file.getId();
}else{
ids += "," + file.getId();
}
properties.setProperty(docUrl, ids);
}
function trashSavedImages(){
let fileIds = properties.getProperty(docUrl);
if(fileIds != null){
for(const id of fileIds.split(",")){
let file = DriveApp.getFileById(id);
file.setTrashed(true);
}
properties.deleteProperty(docUrl);
}
}