Back

Google apps scripter

Generate Google Apps scripts to complete tasks based on user requirements.

user
Write me a Google apps script that will translate all text in a Google Slides presentation to Korean.

Here is a Google apps script that will translate all text in a Google Slides presentation to Korean:

javascript
function translateToKorean() { // Get the active presentation var presentation = SlidesApp.getActivePresentation(); // Get all the slides var slides = presentation.getSlides(); // Iterate through each slide for(var i=0; i<slides.length; i++) { // Get the page elements on the slide var pageElements = slides[i].getPageElements(); // Iterate through the page elements for(var j=0; j<pageElements.length; j++) { // Check if the element is a SHAPE or TEXT_BOX that contains text if(pageElements[j].getPageElementType() == "SHAPE" || pageElements[j].getPageElementType() == "TEXT_BOX") { // Get the text in the element var text = pageElements[j].asShape().getText(); // Translate the text to Korean var translatedText = LanguageApp.translate(text, "ko"); // Update the element with the translated text pageElements[j].asShape().setText(translatedText); } } } }

To use it, simply run the translateToKorean() function in the script editor while having thePresentation open that you want to translate. It will iterate through every text element on every slide and translate it to Korean.

Let me know if you have any other questions!

Sure, here's a Google Apps Script that will translate all text in a Google Slides presentation to Korean using the Google Translate API:

javascript
/** * Translates all text in the current Slides presentation to Korean using the Google Translate API. */ function translateSlidesToKorean() { const presentationId = SlidesApp.getActivePresentation().getId(); const requests = []; // Get all slides in the presentation const slides = SlidesApp.getActivePresentation().getSlides(); // Iterate over each slide for (const slide of slides) { // Get all shape elements on the slide const shapes = slide.getShapes(); // Iterate over each shape element for (const shape of shapes) { // Check if the shape has text if (shape.getPlaceholderType() === SlidesApp.PlaceholderType.BODY) { const text = shape.getText().asString(); // Translate the text to Korean const translatedText = LanguageApp.translate(text, 'ko', 'en'); // Create a request to update the shape's text requests.push({ updateTextRequest: { objectId: shape.getObjectId(), text: { text: translatedText }, fields: 'text' } }); } } } // Execute the batch update request Slides.Presentations.batchUpdate({ requests: requests }, presentationId); }

This script uses the SlidesApp service to get the active Google Slides presentation and iterate over each slide and shape element. If the shape element contains text, it uses the LanguageApp service to translate the text from English to Korean using the Google Translate API.

After translating the text, it creates a request to update the shape's text with the translated text. All requests are collected and executed in a batch update using the Slides.Presentations.batchUpdate method.

Note: Make sure to enable the Google Translate API in your Google Cloud Console project and grant the necessary permissions for the script to use the API.

Here's a Google Apps Script that translates all the text in a Google Slides presentation to Korean:

javascript
function 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:

  1. It retrieves the active Google Slides presentation using SlidesApp.getActivePresentation().

  2. It gets all the slides in the presentation using presentation.getSlides().

  3. It iterates over each slide using a loop.

  4. For each slide, it retrieves all the page elements (shapes, text boxes, tables, etc.) using slide.getPageElements().

  5. It iterates over each page element using another loop.

  6. 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 using shape.getText(), translates the text to Korean using LanguageApp.translate(), and sets the translated text back to the shape using textRange.setText().

  7. 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.

  8. The process is repeated for all page elements on all slides in the presentation.

To use this script, follow these steps:

  1. Open your Google Slides presentation.
  2. Go to "Tools" > "Script editor" to open the script editor.
  3. Copy and paste the provided code into the script editor.
  4. Save the script and give it a name (e.g., "TranslateToKorean").
  5. Close the script editor and return to your presentation.
  6. Go to "Tools" > "Macros" > "Import" and select the "TranslateToKorean" script.
  7. 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

python
import 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)
typescript
import Anthropic from "@anthropic-ai/sdk"; const anthropic = new Anthropic({ apiKey: "my_api_key", // defaults to process.env["ANTHROPIC_API_KEY"] }); const msg = await anthropic.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."}] }); console.log(msg);