From af9c095d97f5d3ece5db06687d7d677e98ca69dd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 21 Nov 2025 22:24:34 +0000 Subject: [PATCH 1/2] 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. --- docs/cursorInspector/cursorInspector.gs | 2 +- docs/dialog2sidebar/Code.gs | 2 +- docs/quickstart/quickstart.gs | 3 ++- docs/translate/translate.gs | 36 +++++++++++++------------ 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/docs/cursorInspector/cursorInspector.gs b/docs/cursorInspector/cursorInspector.gs index a4fa4c810..b73ff44bd 100644 --- a/docs/cursorInspector/cursorInspector.gs +++ b/docs/cursorInspector/cursorInspector.gs @@ -74,7 +74,7 @@ function getDocumentInfo() { /** * Gets information about a given element. - * @param {Element} element The element. + * @param {GoogleAppsScript.Document.Element} element The element. * @return {Object} The information. */ function getElementInfo(element) { diff --git a/docs/dialog2sidebar/Code.gs b/docs/dialog2sidebar/Code.gs index 0d91872ed..08667697f 100644 --- a/docs/dialog2sidebar/Code.gs +++ b/docs/dialog2sidebar/Code.gs @@ -38,7 +38,7 @@ function showSidebar() { * @return {string} The dialog ID. */ function openDialog() { - var dialogId = Utilities.base64Encode(Math.random()); + var dialogId = Utilities.base64Encode(String(Math.random())); var template = HtmlService.createTemplateFromFile('Dialog'); template.dialogId = dialogId; var page = template.evaluate() diff --git a/docs/quickstart/quickstart.gs b/docs/quickstart/quickstart.gs index 5ef105882..73d8b7e70 100644 --- a/docs/quickstart/quickstart.gs +++ b/docs/quickstart/quickstart.gs @@ -21,7 +21,8 @@ */ function printDocTitle() { const documentId = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'; - const doc = Docs.Documents.get(documentId, {'includeTabsContent': true}); + const doc = /** @type {any} */ (Docs).Documents.get(documentId, + {'includeTabsContent': true}); console.log(`The title of the doc is: ${doc.title}`); } // [END docs_quickstart] diff --git a/docs/translate/translate.gs b/docs/translate/translate.gs index 38f7fc5b9..5a959389e 100644 --- a/docs/translate/translate.gs +++ b/docs/translate/translate.gs @@ -87,7 +87,7 @@ function getSelectedText() { const element = elements[i].getElement(); // Only translate elements that can be edited as text; skip images and // other non-text elements. - if (element.editAsText) { + if ((/** @type {any} */ (element)).editAsText) { const elementText = element.asText().getText(); // This check is necessary to exclude images, which return a blank // text element. @@ -190,10 +190,10 @@ function insertText(newText) { } } else { const element = elements[i].getElement(); - if (!replaced && element.editAsText) { + if (!replaced && (/** @type {any} */ (element)).editAsText) { // Only translate elements that can be edited as text, removing other // elements. - element.clear(); + (/** @type {any} */ (element)).clear(); element.asText().setText(newText); replaced = true; } else { @@ -202,30 +202,32 @@ function insertText(newText) { if (element.getNextSibling()) { element.removeFromParent(); } else { - element.clear(); + (/** @type {any} */ (element)).clear(); } } } } } else { const cursor = DocumentApp.getActiveDocument().getCursor(); - const surroundingText = cursor.getSurroundingText().getText(); - const surroundingTextOffset = cursor.getSurroundingTextOffset(); + if (cursor) { + const surroundingText = cursor.getSurroundingText().getText(); + const surroundingTextOffset = cursor.getSurroundingTextOffset(); - // If the cursor follows or preceds a non-space character, insert a space - // between the character and the translation. Otherwise, just insert the - // translation. - if (surroundingTextOffset > 0) { - if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') { - newText = ' ' + newText; + // If the cursor follows or preceds a non-space character, insert a space + // between the character and the translation. Otherwise, just insert the + // translation. + if (surroundingTextOffset > 0) { + if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') { + newText = ' ' + newText; + } } - } - if (surroundingTextOffset < surroundingText.length) { - if (surroundingText.charAt(surroundingTextOffset) !== ' ') { - newText += ' '; + if (surroundingTextOffset < surroundingText.length) { + if (surroundingText.charAt(surroundingTextOffset) !== ' ') { + newText += ' '; + } } + cursor.insertText(newText); } - cursor.insertText(newText); } } From eb41e26dd0d52eefc2ca22dd98982d241ba4880e Mon Sep 17 00:00:00 2001 From: Justin Poehnelt Date: Fri, 21 Nov 2025 16:12:23 -0700 Subject: [PATCH 2/2] fix(docs): address review comments --- docs/cursorInspector/cursorInspector.gs | 2 +- docs/translate/translate.gs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/cursorInspector/cursorInspector.gs b/docs/cursorInspector/cursorInspector.gs index b73ff44bd..31b7a5727 100644 --- a/docs/cursorInspector/cursorInspector.gs +++ b/docs/cursorInspector/cursorInspector.gs @@ -74,7 +74,7 @@ function getDocumentInfo() { /** * Gets information about a given element. - * @param {GoogleAppsScript.Document.Element} element The element. + * @param {Object} element The element. * @return {Object} The information. */ function getElementInfo(element) { diff --git a/docs/translate/translate.gs b/docs/translate/translate.gs index 5a959389e..a405ab718 100644 --- a/docs/translate/translate.gs +++ b/docs/translate/translate.gs @@ -87,7 +87,7 @@ function getSelectedText() { const element = elements[i].getElement(); // Only translate elements that can be edited as text; skip images and // other non-text elements. - if ((/** @type {any} */ (element)).editAsText) { + if (element.getType() === DocumentApp.ElementType.TEXT) { const elementText = element.asText().getText(); // This check is necessary to exclude images, which return a blank // text element. @@ -190,19 +190,21 @@ function insertText(newText) { } } else { const element = elements[i].getElement(); - if (!replaced && (/** @type {any} */ (element)).editAsText) { - // Only translate elements that can be edited as text, removing other - // elements. - (/** @type {any} */ (element)).clear(); - element.asText().setText(newText); + // Only translate elements that can be edited as text; skip images and + // other non-text elements. + const type = element.getType(); + if (!replaced && type === DocumentApp.ElementType.TEXT) { + const textElement = element.asText(); + textElement.clear(); + textElement.setText(newText); replaced = true; } else { // We cannot remove the last paragraph of a doc. If this is the case, // just clear the element. if (element.getNextSibling()) { element.removeFromParent(); - } else { - (/** @type {any} */ (element)).clear(); + } else if (type === DocumentApp.ElementType.TEXT) { + element.asText().clear(); } } }