Skip to content

Commit af9c095

Browse files
fix(docs): resolve type errors
Resolves TypeScript errors in the `docs` directory by adding JSDoc annotations and making minor code adjustments. - Casts `Element` to `any` to allow access to untyped properties. - Adds a null check for the `cursor` object. - Casts the `Docs` object to `any` to bypass the type checker. - Converts a `number` to a `string` to satisfy a function signature. - Replaces `Element` with the fully qualified `GoogleAppsScript.Document.Element` type.
1 parent 275a497 commit af9c095

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

docs/cursorInspector/cursorInspector.gs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function getDocumentInfo() {
7474

7575
/**
7676
* Gets information about a given element.
77-
* @param {Element} element The element.
77+
* @param {GoogleAppsScript.Document.Element} element The element.
7878
* @return {Object} The information.
7979
*/
8080
function getElementInfo(element) {

docs/dialog2sidebar/Code.gs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function showSidebar() {
3838
* @return {string} The dialog ID.
3939
*/
4040
function openDialog() {
41-
var dialogId = Utilities.base64Encode(Math.random());
41+
var dialogId = Utilities.base64Encode(String(Math.random()));
4242
var template = HtmlService.createTemplateFromFile('Dialog');
4343
template.dialogId = dialogId;
4444
var page = template.evaluate()

docs/quickstart/quickstart.gs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
*/
2222
function printDocTitle() {
2323
const documentId = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE';
24-
const doc = Docs.Documents.get(documentId, {'includeTabsContent': true});
24+
const doc = /** @type {any} */ (Docs).Documents.get(documentId,
25+
{'includeTabsContent': true});
2526
console.log(`The title of the doc is: ${doc.title}`);
2627
}
2728
// [END docs_quickstart]

docs/translate/translate.gs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function getSelectedText() {
8787
const element = elements[i].getElement();
8888
// Only translate elements that can be edited as text; skip images and
8989
// other non-text elements.
90-
if (element.editAsText) {
90+
if ((/** @type {any} */ (element)).editAsText) {
9191
const elementText = element.asText().getText();
9292
// This check is necessary to exclude images, which return a blank
9393
// text element.
@@ -190,10 +190,10 @@ function insertText(newText) {
190190
}
191191
} else {
192192
const element = elements[i].getElement();
193-
if (!replaced && element.editAsText) {
193+
if (!replaced && (/** @type {any} */ (element)).editAsText) {
194194
// Only translate elements that can be edited as text, removing other
195195
// elements.
196-
element.clear();
196+
(/** @type {any} */ (element)).clear();
197197
element.asText().setText(newText);
198198
replaced = true;
199199
} else {
@@ -202,30 +202,32 @@ function insertText(newText) {
202202
if (element.getNextSibling()) {
203203
element.removeFromParent();
204204
} else {
205-
element.clear();
205+
(/** @type {any} */ (element)).clear();
206206
}
207207
}
208208
}
209209
}
210210
} else {
211211
const cursor = DocumentApp.getActiveDocument().getCursor();
212-
const surroundingText = cursor.getSurroundingText().getText();
213-
const surroundingTextOffset = cursor.getSurroundingTextOffset();
212+
if (cursor) {
213+
const surroundingText = cursor.getSurroundingText().getText();
214+
const surroundingTextOffset = cursor.getSurroundingTextOffset();
214215

215-
// If the cursor follows or preceds a non-space character, insert a space
216-
// between the character and the translation. Otherwise, just insert the
217-
// translation.
218-
if (surroundingTextOffset > 0) {
219-
if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') {
220-
newText = ' ' + newText;
216+
// If the cursor follows or preceds a non-space character, insert a space
217+
// between the character and the translation. Otherwise, just insert the
218+
// translation.
219+
if (surroundingTextOffset > 0) {
220+
if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') {
221+
newText = ' ' + newText;
222+
}
221223
}
222-
}
223-
if (surroundingTextOffset < surroundingText.length) {
224-
if (surroundingText.charAt(surroundingTextOffset) !== ' ') {
225-
newText += ' ';
224+
if (surroundingTextOffset < surroundingText.length) {
225+
if (surroundingText.charAt(surroundingTextOffset) !== ' ') {
226+
newText += ' ';
227+
}
226228
}
229+
cursor.insertText(newText);
227230
}
228-
cursor.insertText(newText);
229231
}
230232
}
231233

0 commit comments

Comments
 (0)