Google apps scripter
Generate Google Apps scripts to complete tasks based on user requirements.
Here's a Google Apps Script that translates all the text in a Google Slides presentation to Korean:
javascriptfunction translateToKorean() { var presentation = SlidesApp.getActivePresentation(); var slides = presentation.getSlides(); for (var i = 0; i < slides.length; i++) { var slide = slides[i]; var pageElements = slide.getPageElements(); for (var j = 0; j < pageElements.length; j++) { var pageElement = pageElements[j]; if (pageElement.getPageElementType() === SlidesApp.PageElementType.SHAPE) { var shape = pageElement.asShape(); if (shape.getText() !== null) { var textRange = shape.getText(); var translatedText = LanguageApp.translate(textRange.asString(), '', 'ko'); textRange.setText(translatedText); } } if (pageElement.getPageElementType() === SlidesApp.PageElementType.TABLE) { var table = pageElement.asTable(); for (var row = 0; row < table.getNumRows(); row++) { for (var col = 0; col < table.getNumColumns(); col++) { var tableCell = table.getCell(row, col); var textRange = tableCell.getText(); var translatedText = LanguageApp.translate(textRange.asString(), '', 'ko'); textRange.setText(translatedText); } } } } } }
Here's how the script works:
-
It retrieves the active Google Slides presentation using
SlidesApp.getActivePresentation()
. -
It gets all the slides in the presentation using
presentation.getSlides()
. -
It iterates over each slide using a loop.
-
For each slide, it retrieves all the page elements (shapes, text boxes, tables, etc.) using
slide.getPageElements()
. -
It iterates over each page element using another loop.
-
For each page element, it checks if it is a shape (text box) using
pageElement.getPageElementType() === SlidesApp.PageElementType.SHAPE
. If it is a shape, it retrieves the text content usingshape.getText()
, translates the text to Korean usingLanguageApp.translate()
, and sets the translated text back to the shape usingtextRange.setText()
. -
Similarly, if the page element is a table, it iterates over each cell of the table, retrieves the text content, translates it to Korean, and sets the translated text back to the cell.
-
The process is repeated for all page elements on all slides in the presentation.
To use this script, follow these steps:
- Open your Google Slides presentation.
- Go to "Tools" > "Script editor" to open the script editor.
- Copy and paste the provided code into the script editor.
- Save the script and give it a name (e.g., "TranslateToKorean").
- Close the script editor and return to your presentation.
- Go to "Tools" > "Macros" > "Import" and select the "TranslateToKorean" script.
- Run the script by going to "Tools" > "Macros" > "TranslateToKorean".
The script will translate all the text in your Google Slides presentation to Korean. Note that you may need to grant necessary permissions for the script to access your presentation and use the translation service.
API request
pythonimport anthropic client = anthropic.Anthropic( # defaults to os.environ.get("ANTHROPIC_API_KEY") api_key="my_api_key", ) message = client.messages.create( model="claude-3-opus-20240229", max_tokens=2000, temperature=0, messages=[{"role":"user","content":"Write me a Google apps script that will translate all text in a Google Slides presentation to Korean."}] ) print(message.content)