-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
290 lines (227 loc) · 10.2 KB
/
Code.gs
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
// Firestore Credentials
const email = 'firebase-adminsdk-6tjgn@mla-formatter-851fa.iam.gserviceaccount.com';
const key = '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDNGHrnk9ZjIHS\n9tnloaH1waJAJJT4BgCVbIY/EXp8lGQYtVhMcadK5VYrqssfTaH7PmTnQgCZKiKo\nUesNybRe9Oe+MdfvunMSdTmm9FvlWGp2J0mLOCj34j21MnBcXMH3CewrYzXb87TB\n+04TnQbfpqxQBrAOEwoOmw3kCJc1xIQC2lGgqcJabG9vHrxpFZcn8gii5IUK9Avs\nnpyEhJtTWT514w/nmzQj+jkzgzqnM7h2aOA30xg5N0F0ep1Uu/aV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Nu8+AtuZZ9i+0g0L7YJdPA9fIpox3zij1bO2J50/tFBKE9\nU81x+lyQMr67H9DuGOkFGXF2myLj9Fd18JpJ3dpnfQKBgEz0sTXAeYvP4xpw1r/a\nK0TtsMMejeL0x+DHhUfAJN17ddQfbFCmYYEU+hXpyI6DY/uigiPAxQQ3E9gU0gc3\nIlJ+PhuP/kBXCrGyBisH9HtIxowugoZNrAhlTpJljR2Ukkb+ss49arh3UycrMbIb\nPpbV08huqBl1vPvD8m6k9enP\n-----END PRIVATE KEY-----\n';
const projectId = 'mla-formatter-851fa'
const firestore = FirestoreApp.getFirestore(email, key, projectId);
// Start Firebase Utils
const testFirebase = () => {
// createAccount('NEW_FIREBASE_UID_2.0', '[email protected]');
createNewTemplate('Writing 60', 'SAMPLE_FIREBASE_UID', 'Ostend', 'Suryajaya', 'Professor Liu', 'WR60');
};
const createAccount = (firebaseUID, email) => {
// Get the Firestore instance
// Create a new user document
var newUser = {
email: email,
};
// Add the new user document to the 'users' collection
firestore.createDocument(`users/${firebaseUID}`, newUser);
Logger.log('New account added successfully!');
};
const createNewTemplate = (className, firebaseUID, firstName, lastName, professor, templateName) => {
// Data for the new template document
var newTemplateData = {
className: className,
firebaseUID: firebaseUID,
firstName: firstName,
lastName: lastName,
professor: professor,
templateName: templateName
};
// Add the new template document to the 'templates' collection
firestore.createDocument('templates', newTemplateData);
Logger.log('New template added successfully!');
}
// End Firebase Utils
// Start Auth Utils
const testAuthUtils = () => {
Logger.log(`Here is the currently logged in uder: ${ScriptApp.getIdentityToken()}`)
};
// End Auth Utils
const onOpen = () => {
// Create an addon menu in Google Docs
DocumentApp.getUi()
.createAddonMenu()
.addItem("Open Formatter", "showSidebar")
.addToUi();
};
const showSidebar = () => {
// Create an HTML sidebar with specified title and width
const html = HtmlService.createHtmlOutputFromFile("index.html")
.setTitle("MLA/APA Formatter")
.setWidth(300);
DocumentApp.getUi().showSidebar(html);
};
const formatGoogleDoc = (firstName, lastName, prof, course) => {
// Get the active Google Doc and its body
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const header = doc.getHeader() || doc.addHeader();
header.clear();
const headerText = header.appendParagraph(lastName);
headerText.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
// Generate today's date in "MMMM dd, yyyy" format
const currentDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MMMM dd, yyyy");
// Check and replace or insert the first four paragraphs
body.insertParagraph(0, `${firstName} ${lastName}`);
body.insertParagraph(1, prof);
body.insertParagraph(2, course);
body.insertParagraph(3, currentDate);
formatTextInGoogleDoc();
// Save and close the document
doc.saveAndClose();
};
// Helper function to replace or insert a paragraph
function replaceOrInsertParagraph(body, index, text) {
const paragraphs = body.getParagraphs();
if (index < paragraphs.length) {
// Paragraph exists, so replace its text
paragraphs[index].setText(text);
} else {
// Paragraph doesn't exist, so insert it and apply formatting
const newParagraph = body.insertParagraph(index, text);
newParagraph.setLineSpacing(2.0)
.setFontFamily("Times New Roman")
.setFontSize(12);
}
}
const saveTemplate = (templateData, index) => {
const scriptProperties = PropertiesService.getScriptProperties();
let templates = JSON.parse(scriptProperties.getProperty("TEMPLATES") || "[]");
Logger.log(templates);
if (index !== undefined && index >= 0 && index < templates.length) {
// Update existing template
templates[index] = templateData;
} else {
// Add new template
templates.push(templateData);
}
scriptProperties.setProperty("TEMPLATES", JSON.stringify(templates));
};
const getSavedTemplates = () => {
// Retrieve saved templates from script properties
const scriptProperties = PropertiesService.getScriptProperties();
const templates = scriptProperties.getProperty("TEMPLATES");
Logger.log('Retrieved tempaltes:', templates )
if (templates) {
return JSON.parse(templates);
} else {
return [];
}
};
const formatTextInGoogleDoc = () => {
// Format the first four paragraphs with Times New Roman and font size 12
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
for (let i = 0; i < Math.min(paragraphs.length); i++) {
const line = paragraphs[i].editAsText();
line.setFontFamily("Times New Roman");
line.setFontSize(12);
paragraphs[i].setLineSpacing(2.0);
}
// Save and close the document
doc.saveAndClose();
};
const isFormatted = (firstName, lastName, prof, course, date) => {
// Check if the document is formatted correctly
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const documentText = body.getText();
const lines = documentText.split("\n");
if (lines.length >= 4) {
const line1 = lines[0];
const line2 = lines[1];
const line3 = lines[2];
const line4 = lines[3];
const name = `${firstName} ${lastName}`;
return line1 === name && line2 === prof && line3 === course && line4 === date;
} else {
return false;
}
};
const loadTemplateByIndex = (index) => {
Logger.log('Received index (before parsing):', index);
// Parse the index to an integer
index = parseInt(index, 10);
Logger.log('Parsed index:', index);
const scriptProperties = PropertiesService.getScriptProperties();
const templates = JSON.parse(scriptProperties.getProperty("TEMPLATES") || "[]");
Logger.log('Templates array:', templates);
// Check if index is valid
if (!Number.isInteger(index) || index < 0 || index >= templates.length) {
Logger.log('Invalid index or template not found for index:', index);
return;
}
const selectedTemplate = templates[index];
Logger.log('Selected template:', selectedTemplate);
if (selectedTemplate) {
formatGoogleDoc(
selectedTemplate.firstName,
selectedTemplate.lastName,
selectedTemplate.teacherName,
selectedTemplate.className,
selectedTemplate.currentDate
);
} else {
Logger.log('Template not found at index:', index);
}
};
function openEditDialog(index) {
var htmlContent ="<p>Test dialog</p>";
var html = HtmlService.createHtmlOutputFromFile(htmlContent)
.setWidth(400)
.setHeight(300);
DocumentApp.getUi().showModalDialog(html, 'Edit Template');
}
function getTemplateData() {
var scriptProperties = PropertiesService.getScriptProperties();
const index = scriptProperties.getProperty("currentEditingIndex");
const templates = JSON.parse(scriptProperties.getProperty("TEMPLATES") || "[]");
return templates[parseInt(index, 10)] || {};
}
const startEditingTemplate = (index) => {
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty("currentEditingIndex", index.toString());
};
// Function to get data for the template being edited
const getEditingTemplateData = () => {
const scriptProperties = PropertiesService.getScriptProperties();
const index = scriptProperties.getProperty("currentEditingIndex");
const templates = JSON.parse(scriptProperties.getProperty("TEMPLATES") || "[]");
return templates[parseInt(index, 10)] || {};
};
function saveEditedTemplate(index, editedTemplateData) {
const scriptProperties = PropertiesService.getScriptProperties();
let templates = JSON.parse(scriptProperties.getProperty("TEMPLATES") || "[]");
if (index >= 0 && index < templates.length) {
// Update the template at the specified index
templates[index] = editedTemplateData;
scriptProperties.setProperty("TEMPLATES", JSON.stringify(templates));
return true; // Indicate successful update
} else {
return false; // Indicate failure
}
}
const deleteTemplate = (index) => {
const scriptProperties = PropertiesService.getScriptProperties();
let templates = JSON.parse(scriptProperties.getProperty("TEMPLATES") || "[]");
if (index >= 0 && index < templates.length) {
templates.splice(index, 1); // Remove the template at the specified index
scriptProperties.setProperty("TEMPLATES", JSON.stringify(templates));
return true; // Indicate successful deletion
} else {
return false; // Indicate failure or invalid index
}
};
function openSimplePopup() {
var html = HtmlService.createHtmlOutputFromFile('SimplePopup')
.setWidth(300)
.setHeight(150);
DocumentApp.getUi().showModalDialog(html, 'Simple Popup');
}
const getCurrUser = () => {
// Get the current user's email address
const currUser = Session.getActiveUser().getEmail();
Logger.log(currUser);
};