/**
* @OnlyCurrentDoc Limits the script to only accessing the current presentation.
*/
/**
* Create a open translate menu item.
* @param {Event} event The open event.
*/
function onOpen(event) {
SlidesApp.getUi().createAddonMenu()
.addItem('Open Translate', 'showSidebar')
.addToUi();
}
/**
* Open the Add-on upon install.
* @param {Event} event The install event.
*/
function onInstall(event) {
onOpen(event);
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
*/
function showSidebar() {
const ui = 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.
*/
function getElementTexts(elements) {
let texts = [];
elements.forEach((element)=> {
switch (element.getPageElementType()) {
case SlidesApp.PageElementType.GROUP:
element.asGroup().getChildren().forEach((child)=> {
texts = texts.concat(getElementTexts(child));
});
break;
case SlidesApp.PageElementType.TABLE:
const table = element.asTable();
for (let y = 0; y < table.getNumColumns(); ++y) {
for (let x = 0; x < table.getNumRows(); ++x) {
texts.push(table.getCell(x, y).getText());
}
}
break;
case SlidesApp.PageElementType.SHAPE:
texts.push(element.asShape().getText());
break;
}
});
return texts;
}
/**
* 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.
*/
function translateSelectedElements(targetLanguage) {
// Get selected elements.
const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
let texts = [];
switch (selectionType) {
case SlidesApp.SelectionType.PAGE:
selection.getPageRange().getPages().forEach((page)=> {
texts = texts.concat(getElementTexts(page.getPageElements()));
});
break;
case SlidesApp.SelectionType.PAGE_ELEMENT:
const pageElements = selection.getPageElementRange().getPageElements();
texts = texts.concat(getElementTexts(pageElements));
break;
case SlidesApp.SelectionType.TABLE_CELL:
selection.getTableCellRange().getTableCells().forEach((cell)=> {
texts.push(cell.getText());
});
break;
case SlidesApp.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));
});
return texts.length;
}
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2024-09-02 (שעון UTC)."],[[["This quickstart guide details how to build a Google Slides add-on using Apps Script to translate selected presentation text."],["The add-on allows users to select text within their Google Slides presentation and translate it into various languages such as Arabic, Chinese, English, French, German, Hindi, Japanese, Portuguese, and Spanish."],["To utilize this add-on, users need a Google Account, a web browser, and must follow setup instructions which include creating a Slides presentation, enabling Apps Script, and pasting provided code into designated script files."],["Users can run the add-on by reloading their Slides presentation, authorizing the add-on, and selecting the text they wish to translate before clicking the \"Translate\" button in the add-on sidebar."]]],[]]