반응형
💡 Apps Script에서 LanguageApp 의 translate를 이용해 텍스트를 번역해본다.
Apps Script에서 LanguageApp 의 translate 함수를 이용해 텍스트를 번역 할 수 있다.
사용 방법
// The code below will write "Esta es una prueba" to the log.
var spanish = LanguageApp.translate("Hi! I am hero", "en", "es");
Logger.log(spanish);
이름 | 유형 | 설명 |
text | String | 번역할 텍스트 |
sourceLanguage | String | 텍스트가 작성된 언어 코드입니다. 빈 문자열로 설정하면 출발어가 자동으로 감지됩니다. |
targetLanguage | String | 텍스트를 번역해야 하는 언어 코드입니다. |
사용되는 언어 코드
언어 지원 | Cloud Translation | Google Cloud
언어 지원 | Cloud Translation | Google Cloud
의견 보내기 언어 지원 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Translation API의 인식 엔진은 인공신경망 기계 번역(NMT) 모델에 다양한 언어를 지원합니
cloud.google.com
응용하기
아래와 같이 Google Sheets에서 입력한 텍스트를,
다음 셀에 자동으로 변환해서 보여주는 기능을 구현해본다.
/*
* 텍스트 번역하기
* @param {string} fromLan = 출발어
* @param {string} toLan = 번역어
* @param {string} originText = 텍스트
*/
function translater(fromLan, toLan, originText) {
const translateResult = LanguageApp.translate(originText, fromLan, toLan);
return translateResult;
}
function onEdit(e) {
const getValue = e.value;
const spreadSheet = e.source;
const sheetName = spreadSheet.getActiveSheet().getName();
const column = e.range.getColumn();
const row = e.range.getRow();
const activeCell = spreadSheet.getActiveCell();
const sheet = e.source.getActiveSheet();
console.log("입력 값 : " + e.value);
if ( sheetName == "translate" ) {
if ( column == 3 && row > 1 ) {
const fromCellVal = activeCell.offset(0, -2).getValue();
const toCellVal = activeCell.offset(0, -1).getValue();
const resultCell = activeCell.offset(0, 1);
console.log("실행 파라미터 : " + fromCellVal + " / " + toCellVal + " / " + getValue);
const setVal = translater(fromCellVal, toCellVal, getValue);
console.log("번역 값 : " + setVal);
resultCell.setValue(setVal);
}
}
}
반응형
'GCP > Apps Script' 카테고리의 다른 글
Apps Script로 URL호출을 통한 이메일 보내기 (API처럼 사용하기) (0) | 2023.03.17 |
---|---|
Apps Script로 웹페이지를 생성해, PDF, Image 등의 URL을 입력 받아 OCR한 결과 출 (0) | 2023.03.16 |
Apps Script로 Google Sheets의 Toast 메시지 띄우기 (0) | 2023.03.10 |
Apps Script로 Google Sheets의 활성(선택)영역 지정 (1) | 2023.03.08 |
Apps Script로 Google Sheets의 정렬(sort) 적용하기 (1) | 2023.03.07 |