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ドキュメントに見出しを追加したい

今回の例では、ドキュメントの末尾に「見出しD」 を追加します。 見出しA, B, C, Dのスタイルは、見出し3 ( HEADING3 ) に設定しています。  下記Code.gsの  GOOGLE_DOCUMENT_URL を設定して  addHeadingToEnd()  を...