This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskpane.html
More file actions
102 lines (85 loc) · 3.2 KB
/
Copy pathtaskpane.html
File metadata and controls
102 lines (85 loc) · 3.2 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
<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. -->
<!DOCTYPE html>
<html>
<head>
<!-- Office JavaScript API -->
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
</head>
<body>
<p>This add-in will insert comments on where questions might be helpful for writers to reflect on.</p>
<button id="insertButton">Start Inserting!</button>
</body>
<script>
Office.onReady((info) => {
// Check that we loaded into Word
if (info.host === Office.HostType.Word) {
document.getElementById("insertButton").onclick = main;
}
});
// Send the document to the web server.
async function sendDocument(data) {
const response = await fetch("https://dev6.kenarnold.org/data", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});
const result = await response.json();
return result;
}
// Fetch the response from the web server.
async function getComments() {
let response = await fetch("https://dev6.kenarnold.org/response");
let data = await response.json();
return data;
}
// Split the paragraph into a Word.RangeCollection object
async function paragraphSplitter(paragraph) {
// Split the paragraph by whitespace and return the result
const result = paragraph.getTextRanges([" "])
result.load()
await result.context.sync()
return result
}
// Insert the anchored comment into the target word
async function addComment(paragraph, targetword) {
const result = await paragraphSplitter(paragraph)
// targetword.index - 1 as index starts at 1 on server
result.items[targetword.index - 1].getRange().insertComment(targetword.comment)
}
// Main program.
function main() {
return Word.run(async function (context) {
try {
// Get the document as a Word.ParagraphCollection class
const documentParagraphs = context.document.body.paragraphs;
documentParagraphs.load("items");
await context.sync();
// Send the document to the server as an array of sentences, and get the result
let data = [];
for (let i = 0; i < documentParagraphs.items.length; i++) {
data.push(documentParagraphs.items[i].text);
}
const result = await sendDocument(data);
console.log(result) // this line can be removed when send and get are combined
const questionsForTheWholeDoc = await getComments();
for (let i = 0; i < documentParagraphs.items.length; i++) {
// Filter the questions by the paragraph index
const questionsForThisParagraph = questionsForTheWholeDoc.filter(
// Paragraph index starts at 1 on server
(q) => q.paragraph === i + 1
);
// Insert comment into target words
for (let j = 0; j < questionsForThisParagraph.length; j++) {
// Pass in the current paragraph and all the target words in the paragraph
await addComment(documentParagraphs.items[i], questionsForThisParagraph[j]);
}
}
} catch (error) {
console.log(error)
}
});
}
</script>
</html>