diff --git a/docs/cursorInspector/cursorInspector.gs b/docs/cursorInspector/cursorInspector.gs index a4fa4c810..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 {Element} element The element. + * @param {Object} 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..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 (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,42 +190,46 @@ function insertText(newText) { } } else { const element = elements[i].getElement(); - if (!replaced && element.editAsText) { - // Only translate elements that can be edited as text, removing other - // elements. - 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 { - element.clear(); + } else if (type === DocumentApp.ElementType.TEXT) { + element.asText().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); } }