-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs-edit.js
More file actions
165 lines (145 loc) · 4.7 KB
/
docs-edit.js
File metadata and controls
165 lines (145 loc) · 4.7 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
import { getDocsClient } from './google-clients.js';
import { readDocument, extractText, countOccurrences, getAllIndices, parseColor } from './docs-core.js';
export async function editDocument(auth, docId, oldText, newText, replaceAll = false) {
const { text } = await readDocument(auth, docId);
const count = countOccurrences(text, oldText);
if (count === 0) {
throw new Error(
`old_text not found in document. ` +
`Make sure the text exists exactly as specified, including whitespace and punctuation.`
);
}
if (count > 1 && !replaceAll) {
throw new Error(
`old_text appears ${count} times in document. ` +
`The edit will fail because old_text must be unique. ` +
`Either include more surrounding context to make it unique, ` +
`or set replace_all to true to replace all ${count} occurrences.`
);
}
const docs = getDocsClient(auth);
const indices = getAllIndices(text, oldText);
const requests = [];
for (let i = indices.length - 1; i >= 0; i--) {
const idx = indices[i];
requests.push({
deleteContentRange: { range: { startIndex: idx + 1, endIndex: idx + oldText.length + 1 } }
});
if (newText) {
requests.push({
insertText: { location: { index: idx + 1 }, text: newText }
});
}
}
await docs.documents.batchUpdate({
documentId: docId,
requestBody: { requests }
});
return { replacements: indices.length };
}
export async function insertDocument(auth, docId, text, position = 'end') {
const docs = getDocsClient(auth);
const doc = await docs.documents.get({ documentId: docId });
let index;
if (position === 'end') {
const lastElem = doc.data.body.content[doc.data.body.content.length - 1];
index = (lastElem.endIndex || doc.data.body.content.length) - 1;
} else if (typeof position === 'number') {
index = position + 1;
} else {
const content = extractText(doc.data.body.content);
const pos = content.indexOf(position);
if (pos === -1) {
throw new Error(
`Position text not found in document. ` +
`Make sure the text exists exactly as specified.`
);
}
index = pos + position.length + 1;
}
await docs.documents.batchUpdate({
documentId: docId,
requestBody: { requests: [{ insertText: { location: { index }, text } }] }
});
}
export async function deleteText(auth, docId, searchText, replaceAll = false) {
return editDocument(auth, docId, searchText, '', replaceAll);
}
export async function insertTable(auth, docId, rows, cols, position = 'end') {
const docs = getDocsClient(auth);
const doc = await docs.documents.get({ documentId: docId });
let index;
if (position === 'end') {
const lastElem = doc.data.body.content[doc.data.body.content.length - 1];
index = (lastElem.endIndex || doc.data.body.content.length) - 1;
} else if (typeof position === 'number') {
index = position + 1;
} else {
const content = extractText(doc.data.body.content);
const pos = content.indexOf(position);
if (pos === -1) {
throw new Error(`Position text not found in document.`);
}
index = pos + position.length + 1;
}
await docs.documents.batchUpdate({
documentId: docId,
requestBody: {
requests: [{
insertTable: {
rows,
columns: cols,
location: { index }
}
}]
}
});
return { rows, cols };
}
export async function batchUpdate(auth, docId, operations) {
const docs = getDocsClient(auth);
const requests = [];
for (const op of operations) {
switch (op.type) {
case 'insert':
requests.push({
insertText: {
location: { index: op.index + 1 },
text: op.text
}
});
break;
case 'delete':
requests.push({
deleteContentRange: {
range: { startIndex: op.startIndex + 1, endIndex: op.endIndex + 1 }
}
});
break;
case 'format':
const textStyle = {};
if (op.bold !== undefined) textStyle.bold = op.bold;
if (op.italic !== undefined) textStyle.italic = op.italic;
if (op.underline !== undefined) textStyle.underline = op.underline;
const fields = Object.keys(textStyle).join(',');
if (fields) {
requests.push({
updateTextStyle: {
range: { startIndex: op.startIndex + 1, endIndex: op.endIndex + 1 },
textStyle,
fields
}
});
}
break;
}
}
if (requests.length === 0) {
throw new Error('No valid operations provided.');
}
await docs.documents.batchUpdate({
documentId: docId,
requestBody: { requests }
});
return { operationsApplied: requests.length };
}