Apps Scriptリファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2023年9月22日金曜日

Trying Google Translate with Google Apps Script


Introduction

We will create a simple English to Japanese translator using Google Apps Script. 
The application will have a front-end input form and a back-end server that will process the translation results.



Application

We need to create a front-end form that will allow users to enter the English text they want to translate. 




When you enter English that you want to translate to Japanese and click the button, the English is passed to the server side, and the translated Japanese is returned to the front end using LanguageApp.translate.


Source Code

Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index').setTitle("Title");
}

function returnJapanese(value) {
return LanguageApp.translate(value, "en", "ja");
}


index.html
<!DOCTYPE html>
<html>
<body>
<textarea id="text"></textarea>
<button id="submit">Translate to Japanese</button>
<script>
document.getElementById("submit").onclick = runTranslate;

function runTranslate() {
var value = document.getElementById("text").value;
google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
.returnJapanese(value);
}

function onSuccess(result) {
alert(result);
}

function onFailure(e) {
alert([e.message, e.stack]);
}
</script>
</body>
</html>


Tips

LanguageApp.translate(text, sourceLanguage, targetLanguage)

If source language is empty, it will be auto-detected.


Reference

Class LanguageApp
https://developers.google.com/apps-script/reference/language/language-app

Language support
https://cloud.google.com/translate/docs/languages



English
Take your time.


Japanese
ゆっくりしてください。






Latest post

Google Classroom API でクラスの一覧を取得したい

自分が指導・参加しているクラスの名称とIDを取得するコードを試しました 下記 Code.gs では pageSize で 100 を設定していますが 必ず 100件 返ってくるとは限らないらしいです https://developers.google.com/workspace/...