/** * @OnlyCurrentDoc Limits the script to only accessing the current presentation. *//** * Create a open translate menu item. * @param {Event} event The open event. */functiononOpen(event){SlidesApp.getUi().createAddonMenu().addItem('OpenTranslate','showSidebar').addToUi();}/** * Open the Add-on upon install. * @param {Event} event The install event. */functiononInstall(event){onOpen(event);}/** * Opens a sidebar in the document containing the add-on's user interface. */functionshowSidebar(){constui=HtmlService.createHtmlOutputFromFile('sidebar').setTitle('Translate');SlidesApp.getUi().showSidebar(ui);}/** * Recursively gets child text elements a list of elements. * @param {PageElement[]} elements The elements to get text from. * @return {Text[]} An array of text elements. */functiongetElementTexts(elements){lettexts=[];elements.forEach((element)=>{switch(element.getPageElementType()){caseSlidesApp.PageElementType.GROUP:element.asGroup().getChildren().forEach((child)=>{texts=texts.concat(getElementTexts(child));});break;caseSlidesApp.PageElementType.TABLE:consttable=element.asTable();for(lety=0;y < table.getNumColumns();++y){for(letx=0;x < table.getNumRows();++x){texts.push(table.getCell(x,y).getText());}}break;caseSlidesApp.PageElementType.SHAPE:texts.push(element.asShape().getText());break;}});returntexts;}/** * Translates selected slide elements to the target language using Apps Script's Language service. * * @param {string} targetLanguage The two-letter short form for the target language. (ISO 639-1) * @return {number} The number of elements translated. */functiontranslateSelectedElements(targetLanguage){// Get selected elements.constselection=SlidesApp.getActivePresentation().getSelection();constselectionType=selection.getSelectionType();lettexts=[];switch(selectionType){caseSlidesApp.SelectionType.PAGE:selection.getPageRange().getPages().forEach((page)=>{texts=texts.concat(getElementTexts(page.getPageElements()));});break;caseSlidesApp.SelectionType.PAGE_ELEMENT:constpageElements=selection.getPageElementRange().getPageElements();texts=texts.concat(getElementTexts(pageElements));break;caseSlidesApp.SelectionType.TABLE_CELL:selection.getTableCellRange().getTableCells().forEach((cell)=>{texts.push(cell.getText());});break;caseSlidesApp.SelectionType.TEXT:selection.getPageElementRange().getPageElements().forEach((element)=>{texts.push(element.asShape().getText());});break;}// Translate all elements in-place.texts.forEach((text)=>{text.setText(LanguageApp.translate(text.asRenderedString(),'',targetLanguage));});returntexts.length;}