-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconvertRichTextToHtml.ts
More file actions
144 lines (112 loc) · 3.77 KB
/
convertRichTextToHtml.ts
File metadata and controls
144 lines (112 loc) · 3.77 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
import { IllegalArgumentException } from "../../exception";
import { isRichTextValue } from "./isRichTextValue";
/**
* Converts a [`RichTextValue`](https://developers.google.com/apps-script/reference/spreadsheet/rich-text-value) object into HTML, preserving text formatting.
*
* @example
* ```javascript
* const ss = SpreadsheetApp.getActiveSpreadsheet();
* const sheet = ss.getSheetByName('Sheet1');
* const range = sheet.getRange('A1');
* const richTextValue = range.getRichTextValue();
* const html = convertRichTextToHtml(richTextValue);
*
* console.log(html);
* ```
*
* @param {GoogleAppsScript.Spreadsheet.RichTextValue} richText - The {@link GoogleAppsScript.Spreadsheet.RichTextValue|RichTextValue} object containing formatted text.
* @returns {string} The HTML string representing the formatted text.
* @throws {@link IllegalArgumentException}
* @see {@link GoogleAppsScript.Spreadsheet.RichTextValue|RichTextValue}
* @see [Class RichTextValue](https://developers.google.com/apps-script/reference/spreadsheet/rich-text-value)
* @since 1.0.0
* @version 1.1.0
* @environment `Google Apps Script`
* @author Maksym Stoianov <stoianov.maksym@gmail.com>
* @license Apache-2.0
*/
export function convertRichTextToHtml(
richText: GoogleAppsScript.Spreadsheet.RichTextValue
): string {
if (arguments.length === 0) {
throw new IllegalArgumentException();
}
if (!isRichTextValue(richText)) {
throw new TypeError('Expected an object of type "RichTextValue".');
}
const runs = richText.getRuns();
let html = "";
/**
* @param {object} input
*/
const _toStringStyles = (input: object): string =>
Object.entries(input)
.map(([key, value]) => `${key}: ${value}`)
.join("; ");
/**
* @param {object} input
*/
const _toStringAttrs = (input: object): string =>
Object.entries(input)
.map(([key, value]) => `${key}="${value}"`)
.join(" ");
for (const run of runs) {
const textStyle = run.getTextStyle();
const attributes: Record<string, string> = {};
const styles: Record<string, string> = {};
const tags: string[] = [];
if (textStyle.isStrikethrough()) {
tags.push("s");
}
if (textStyle.isUnderline()) {
tags.push("u");
}
if (textStyle.isBold()) {
tags.push("b");
}
if (textStyle.isItalic()) {
tags.push("i");
}
const fontFamily = textStyle.getFontFamily();
if (fontFamily && fontFamily !== "Arial") {
styles["font-family"] = fontFamily;
}
const fontSize = textStyle.getFontSize();
if (fontSize !== 10) {
styles["font-size"] = `${fontSize}px`;
}
const color = textStyle.getForegroundColor();
if (color && !["#000000", "#000", "black"].includes(color)) {
styles["color"] = color;
}
const href = run.getLinkUrl();
let tag: string = "span";
if (href) {
tag = "a";
attributes["href"] = href;
// htmlAttributes["target"] = "_blank";
// htmlAttributes["rel"] = "noopener noreferrer";
} else if (tags.length > 0) {
tag = tags.pop() ?? "span";
}
if (Object.keys(styles).length > 0) {
attributes["style"] = _toStringStyles(styles);
}
const attrString = Object.keys(attributes).length > 0 ? " " + _toStringAttrs(attributes) : "";
const joinedOpenTags = tags.join("><");
const openTags = tags.length > 0 ? `<${joinedOpenTags}>` : "";
const text = run.getText().replace(/\r?\n|\r/g, "<br>");
const joinedCloseTags = tags.reverse().join("></");
const closeTags = tags.length > 0 ? `</${joinedCloseTags}>` : "";
html += Utilities.formatString(
"<%s%s>%s%s%s</%s>",
tag,
attrString,
openTags,
text,
closeTags,
tag
);
}
return html;
}