1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DeepL × Google Apps Script で翻訳ツールを作る方法

Posted at

YouTubeで解説しています → https://youtu.be/2lFDi_86Omo

プログラム

コード.gs
const deepl_url = "https://api-free.deepl.com/v2/translate";
const deepl_key = "DeepL-Auth-Key *************************************"

/**
 curl -X POST 'https://api-free.deepl.com/v2/translate' \
	-H 'Authorization: DeepL-Auth-Key **********************************:fx' \
	-d 'text=Hello%2C%20world!' \
	-d 'target_lang=DE'
 */ 

// 翻訳
function deepL(msg) {
  // msg = "今日の天気は?"
  msg = "What is the weather today"
  let lang = 'EN'
  if (isEnglish(msg))
    lang = 'JA'
  let headers = {
    'Authorization' : deepl_key
  }
  let payload = {
    'text' : msg,
    'target_lang' : lang
  }
  let options = {
    'method' : 'post',
    'headers' : headers,
    'payload' : payload
  }
  const response = UrlFetchApp.fetch(deepl_url, options).getContentText()
  const json = JSON.parse(response)
  const text = json.translations[0].text
  return text
}

// 英語かどうか判定
function isEnglish(text) {
  return !text.split('').some(char => char.charCodeAt() > 255);
}

参考リンク

◆ DeepL 公式ページ
https://www.deepl.com/

◆ DeepL API 料金表
https://www.deepl.com/pro?cta=header-prices

◆ DeepL API ドキュメント
https://www.deepl.com/docs-api/translate-text/

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?