5 | ChatGPT directly integrated into Google Docs 📑
6 |
7 |
8 |
9 | ## Features
10 | - Faster than [chat.openai.com](https://chat.openai.com)
11 | - Free
12 | - Live Chat Within Google Docs
13 | - Text Completion
14 |
15 |
16 | ## Usage
17 |
18 | - **Google Docs**
19 | - **MS Word**
20 |
21 | ### Google Docs
22 |
23 | 1. Get the template: https://docs.google.com/document/d/1N7qvw5mZdVe2u2IQ5pnVDmUjHsLEfq9_Z0Tf8PHloZA/edit?usp=sharing
24 |
25 | 2. Make a copy of the document
26 |
27 | 
28 |
29 | 3. Type something in your Google Doc
30 |
31 | 
32 |
33 | 4. Select your question, or whatever text you want to send to ChatGPT
34 |
35 | 
36 |
37 | 5. Use the extension! (click Start instead of Ask to access the chat pop-up)
38 |
39 | 
40 |
41 | 6. Accept the Authorization request & sign into google
42 |
43 | 
44 |
45 | 7. Click Advanced, go to ChatGPT & allow the scopes required
46 |
47 | 
48 |
49 |
50 | 8. Get your result!
51 |
52 | 
53 |
54 | ### MS Word
55 |
56 | 1. Open a new word document
57 |
58 | 2. Enable the Developer Tab on Word
59 |
60 | 3. Click Macros
61 | 
62 |
63 | 4. Create a new macro with the name AddToShortcut
64 | 
65 |
66 | 5. Copy the code in `wordGPT/ask.bas` of this repo, and paste it into the Word VBA Editor
67 |
68 | 6. Click `Tools > References` in the navbar
69 | 
70 |
71 | 7. Search for Microsoft Scripting Runtime and enable it
72 | 
73 |
74 | 8. Click OK and Save the file containing the code you pasted.
75 |
76 | 9. Right click selected text in Word and click `Ask ChatGPT`
77 |
78 | 
79 |
80 | 1. Wait for your result! (Currently it is not recommended to ask ChatGPT again before current question is answered, otherwise all the answers can be mixed up. Keyboard input is disabled while waiting for the result, see issue #9.)
81 |
82 |
--------------------------------------------------------------------------------
/add-on/ask.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | // [START apps_script_docs_translate_quickstart]
17 | /**
18 | * @OnlyCurrentDoc
19 | *
20 | * The above comment directs Apps Script to limit the scope of file
21 | * access for this add-on. It specifies that this add-on will only
22 | * attempt to read or modify the files in which the add-on is used,
23 | * and not all of the user's files. The authorization request message
24 | * presented to users will reflect this limited scope.
25 | */
26 |
27 | /**
28 | * Creates a menu entry in the Google Docs UI when the document is opened.
29 | * This method is only used by the regular add-on, and is never called by
30 | * the mobile add-on version.
31 | *
32 | * @param {object} e The event parameter for a simple onOpen trigger. To
33 | * determine which authorization mode (ScriptApp.AuthMode) the trigger is
34 | * running in, inspect e.authMode.
35 | */
36 | function onOpen(e) {
37 | DocumentApp.getUi().createAddonMenu()
38 | .addItem('Start', 'showSidebar')
39 | .addItem('Ask', 'autoComplete')
40 | .addToUi();
41 | showSidebar();
42 | }
43 |
44 | /**
45 | * Runs when the add-on is installed.
46 | * This method is only used by the regular add-on, and is never called by
47 | * the mobile add-on version.
48 | *
49 | * @param {object} e The event parameter for a simple onInstall trigger. To
50 | * determine which authorization mode (ScriptApp.AuthMode) the trigger is
51 | * running in, inspect e.authMode. (In practice, onInstall triggers always
52 | * run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
53 | * AuthMode.NONE.)
54 | */
55 | function onInstall(e) {
56 | onOpen(e);
57 | }
58 |
59 | function autoComplete() {
60 |
61 | const selected = getSelectedText();
62 |
63 | const response = getGPTResponse(selected[0]);
64 |
65 | insertTextWithoutReplacement(response);
66 | }
67 |
68 | /**
69 | * Opens a sidebar in the document containing the add-on's user interface.
70 | * This method is only used by the regular add-on, and is never called by
71 | * the mobile add-on version.
72 | */
73 | function showSidebar() {
74 | const ui = HtmlService.createHtmlOutputFromFile('sidebar')
75 | .setTitle('docGPT');
76 | DocumentApp.getUi().showSidebar(ui);
77 | }
78 |
79 | /**
80 | * Gets the text the user has selected. If there is no selection,
81 | * this function displays an error message.
82 | *
83 | * @return {Array.} The selected text.
84 | */
85 | function getSelectedText() {
86 | const selection = DocumentApp.getActiveDocument().getSelection();
87 | const text = [];
88 | if (selection) {
89 | const elements = selection.getSelectedElements();
90 | for (let i = 0; i < elements.length; ++i) {
91 | if (elements[i].isPartial()) {
92 | const element = elements[i].getElement().asText();
93 | const startIndex = elements[i].getStartOffset();
94 | const endIndex = elements[i].getEndOffsetInclusive();
95 |
96 | text.push(element.getText().substring(startIndex, endIndex + 1));
97 | } else {
98 | const element = elements[i].getElement();
99 | // Only translate elements that can be edited as text; skip images and
100 | // other non-text elements.
101 | if (element.editAsText) {
102 | const elementText = element.asText().getText();
103 | // This check is necessary to exclude images, which return a blank
104 | // text element.
105 | if (elementText) {
106 | text.push(elementText);
107 | }
108 | }
109 | }
110 | }
111 | }
112 | if (!text.length) throw new Error('Please select some text.');
113 | return text;
114 | }
115 |
116 | /**
117 | * Gets the stored user preferences for the origin and destination languages,
118 | * if they exist.
119 | * This method is only used by the regular add-on, and is never called by
120 | * the mobile add-on version.
121 | *
122 | * @return {Object} The user's origin and destination language preferences, if
123 | * they exist.
124 | */
125 | function getPreferences() {
126 | const userProperties = PropertiesService.getUserProperties();
127 | return {
128 | originLang: userProperties.getProperty('originLang'),
129 | destLang: userProperties.getProperty('destLang')
130 | };
131 | }
132 |
133 | /**
134 | * Gets the user-selected text and translates it from the origin language to the
135 | * destination language. The languages are notated by their two-letter short
136 | * form. For example, English is 'en', and Spanish is 'es'. The origin language
137 | * may be specified as an empty string to indicate that Google Translate should
138 | * auto-detect the language.
139 | *
140 | * @param {string} origin The two-letter short form for the origin language.
141 | * @param {string} dest The two-letter short form for the destination language.
142 | * @param {boolean} savePrefs Whether to save the origin and destination
143 | * language preferences.
144 | * @return {Object} Object containing the original text and the result of the
145 | * translation.
146 | */
147 | function getGPTResponse(question) {
148 |
149 | var options = {
150 | 'method' : 'post',
151 | 'contentType': 'application/json',
152 | // Convert the JavaScript object to a JSON string.
153 | 'payload' : JSON.stringify({
154 | 'message': question
155 | })
156 | };
157 |
158 | const response = UrlFetchApp.fetch('https://chatgpt-api.kesarx.repl.co/chat', options);
159 | const json = JSON.parse(response.getContentText());
160 | return json.choices[0].message.content;
161 | }
162 |
163 | /**
164 | * Replaces the text of the current selection with the provided text, or
165 | * inserts text at the current cursor location. (There will always be either
166 | * a selection or a cursor.) If multiple elements are selected, only inserts the
167 | * translated text in the first element that can contain text and removes the
168 | * other elements.
169 | *
170 | * @param {string} newText The text with which to replace the current selection.
171 | */
172 | function insertText(newText) {
173 | const selection = DocumentApp.getActiveDocument().getSelection();
174 | if (selection) {
175 | let replaced = false;
176 | const elements = selection.getSelectedElements();
177 | if (elements.length === 1 && elements[0].getElement().getType() ===
178 | DocumentApp.ElementType.INLINE_IMAGE) {
179 | throw new Error('Can\'t insert text into an image.');
180 | }
181 | for (let i = 0; i < elements.length; ++i) {
182 | if (elements[i].isPartial()) {
183 | const element = elements[i].getElement().asText();
184 | const startIndex = elements[i].getStartOffset();
185 | const endIndex = elements[i].getEndOffsetInclusive();
186 | element.deleteText(startIndex, endIndex);
187 | if (!replaced) {
188 | element.insertText(startIndex, newText);
189 | replaced = true;
190 | } else {
191 | // This block handles a selection that ends with a partial element. We
192 | // want to copy this partial text to the previous element so we don't
193 | // have a line-break before the last partial.
194 | const parent = element.getParent();
195 | const remainingText = element.getText().substring(endIndex + 1);
196 | parent.getPreviousSibling().asText().appendText(remainingText);
197 | // We cannot remove the last paragraph of a doc. If this is the case,
198 | // just remove the text within the last paragraph instead.
199 | if (parent.getNextSibling()) {
200 | parent.removeFromParent();
201 | } else {
202 | element.removeFromParent();
203 | }
204 | }
205 | } else {
206 | const element = elements[i].getElement();
207 | if (!replaced && element.editAsText) {
208 | // Only translate elements that can be edited as text, removing other
209 | // elements.
210 | element.clear();
211 | element.asText().setText(newText);
212 | replaced = true;
213 | } else {
214 | // We cannot remove the last paragraph of a doc. If this is the case,
215 | // just clear the element.
216 | if (element.getNextSibling()) {
217 | element.removeFromParent();
218 | } else {
219 | element.clear();
220 | }
221 | }
222 | }
223 | }
224 | } else {
225 | const cursor = DocumentApp.getActiveDocument().getCursor();
226 | const surroundingText = cursor.getSurroundingText().getText();
227 | const surroundingTextOffset = cursor.getSurroundingTextOffset();
228 |
229 | // If the cursor follows or preceds a non-space character, insert a space
230 | // between the character and the translation. Otherwise, just insert the
231 | // translation.
232 | if (surroundingTextOffset > 0) {
233 | if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') {
234 | newText = ' ' + newText;
235 | }
236 | }
237 | if (surroundingTextOffset < surroundingText.length) {
238 | if (surroundingText.charAt(surroundingTextOffset) !== ' ') {
239 | newText += ' ';
240 | }
241 | }
242 | cursor.insertText(newText);
243 | }
244 | }
245 |
246 | /**
247 | * Inserts text after the current selected text.
248 | *
249 | * @param {string} newText The text with which to replace the current selection.
250 | */
251 | function insertTextWithoutReplacement(newText) {
252 | const selection = DocumentApp.getActiveDocument().getSelection();
253 | if (selection) {
254 | const elements = selection.getSelectedElements();
255 | if (elements.length === 1 && elements[0].getElement().getType() ===
256 | DocumentApp.ElementType.INLINE_IMAGE) {
257 | throw new Error('Can\'t insert text into an image.');
258 | }
259 | const element = elements[elements.length - 1].getElement().asText();
260 | if (elements[elements.length - 1].isPartial()) {
261 | const endIndex = elements[elements.length - 1].getEndOffsetInclusive();
262 | element.insertText(endIndex + 1, newText);
263 | } else {
264 | element.appendText(newText);
265 | }
266 | }
267 | }
268 |
--------------------------------------------------------------------------------
/add-on/sidebar.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
98 |
99 |
100 |
101 |