-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs-format.js
More file actions
105 lines (92 loc) · 3.12 KB
/
docs-format.js
File metadata and controls
105 lines (92 loc) · 3.12 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
import { getDocsClient } from './google-clients.js';
import { readDocument, getAllIndices, parseColor } from './docs-core.js';
export async function formatDocument(auth, docId, searchText, formatting) {
const { text } = await readDocument(auth, docId);
const indices = getAllIndices(text, searchText);
if (indices.length === 0) {
throw new Error(
`Text not found in document. ` +
`Make sure the text exists exactly as specified.`
);
}
const docs = getDocsClient(auth);
const requests = [];
for (const idx of indices) {
const startIndex = idx + 1;
const endIndex = idx + searchText.length + 1;
const textStyle = {};
if (formatting.bold !== undefined) textStyle.bold = formatting.bold;
if (formatting.italic !== undefined) textStyle.italic = formatting.italic;
if (formatting.underline !== undefined) textStyle.underline = formatting.underline;
if (formatting.strikethrough !== undefined) textStyle.strikethrough = formatting.strikethrough;
if (formatting.fontSize) {
textStyle.fontSize = { magnitude: formatting.fontSize, unit: 'PT' };
}
if (formatting.fontFamily) {
textStyle.weightedFontFamily = { fontFamily: formatting.fontFamily };
}
if (formatting.foregroundColor) {
const color = parseColor(formatting.foregroundColor);
if (color) textStyle.foregroundColor = { color: { rgbColor: color } };
}
if (formatting.backgroundColor) {
const color = parseColor(formatting.backgroundColor);
if (color) textStyle.backgroundColor = { color: { rgbColor: color } };
}
const fields = Object.keys(textStyle).join(',');
if (fields) {
requests.push({
updateTextStyle: {
range: { startIndex, endIndex },
textStyle,
fields
}
});
}
if (formatting.heading) {
const headingMap = {
'TITLE': 'TITLE',
'SUBTITLE': 'SUBTITLE',
'HEADING_1': 'HEADING_1',
'HEADING_2': 'HEADING_2',
'HEADING_3': 'HEADING_3',
'HEADING_4': 'HEADING_4',
'HEADING_5': 'HEADING_5',
'HEADING_6': 'HEADING_6',
'NORMAL_TEXT': 'NORMAL_TEXT'
};
const namedStyle = headingMap[formatting.heading.toUpperCase()] || 'NORMAL_TEXT';
requests.push({
updateParagraphStyle: {
range: { startIndex, endIndex },
paragraphStyle: { namedStyleType: namedStyle },
fields: 'namedStyleType'
}
});
}
if (formatting.alignment) {
const alignMap = {
'LEFT': 'START',
'CENTER': 'CENTER',
'RIGHT': 'END',
'JUSTIFY': 'JUSTIFIED'
};
const alignment = alignMap[formatting.alignment.toUpperCase()] || 'START';
requests.push({
updateParagraphStyle: {
range: { startIndex, endIndex },
paragraphStyle: { alignment },
fields: 'alignment'
}
});
}
}
if (requests.length === 0) {
throw new Error('No formatting options specified.');
}
await docs.documents.batchUpdate({
documentId: docId,
requestBody: { requests }
});
return { formattedOccurrences: indices.length };
}