GCP/Apps Script

Apps Script로 LanguageApp의Translate API를 사용하여 텍스트 번역하기

whistory 2023. 3. 14. 14:21
반응형

 

 

 

💡 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);
        }
    }
}

 

 

 

 

 

 

반응형