Skip to content

Instantly share code, notes, and snippets.

@tohagan
Created August 4, 2025 07:18
Show Gist options
  • Save tohagan/3f96ac134b03816d3c9c929f566c598c to your computer and use it in GitHub Desktop.
Save tohagan/3f96ac134b03816d3c9c929f566c598c to your computer and use it in GitHub Desktop.
AppScript functions for automated translation in Google Sheets
// I found the GOOGLETRANSLATE() function in GSheets was returning poor translations results in comparison other APIs including Google own offical API.
// So I created this AppScript code to replace GOOGLETRANSLATE() in Google sheets.
// Just call TRANSLATE(text, from_language, to_language) in your sheet to perform translations.
// To use Google Translate
// 1. Create your Google Translate API key as shown in this video
// - https://www.youtube.com/watch?v=5hTlSGD4_zk
//
// 2. Uncomment the next line and replace XXXX with your API key and Run the script.
// PropertiesService.getUserProperties().setProperty('GT_API_KEY', 'XXXX');
//
// 3. Then undo the changes from Step 2 so that the code no longer has your API key and it's back as comment.
// This keeps the key secret!
// Optional ...
// To use Azure Translate service, create an API key watch the following video
// - https://www.youtube.com/watch?v=l6Rl4HSZVvg
// Just note that the code below is setup to use an API key for the US West region.
// Apply the same Steps 2 & 3 above for this key:
// PropertiesService.getUserProperties().setProperty('AZ_API_KEY', 'YYYY');
var AZ_REGION = 'westus';
function test_gt() {
var hi = GT_TRANSLATE("The quick brown fox jumps over the lazy dog", "en", "hi");
console.log(hi)
var en = GT_TRANSLATE(hi, "hi", "en");
console.log(en)
}
function test_az() {
var hi = AZ_TRANSLATE("The quick brown fox jumps over the lazy dog", "en", "hi");
console.log(hi)
var en = AZ_TRANSLATE(hi, "hi", "en");
console.log(en)
}
// Use the TRANSLATE() function in your sheet
function TRANSLATE(text, sourceLanguage, targetLanguage) {
return GG_TRANSLATE(text, sourceLanguage, targetLanguage) - Google Translate
// return AZ_TRANSLATE(text, sourceLanguage, targetLanguage) - Azure Translate
}
/**
* Translates text using Google Cloud Translation API.
*
* @param {string} text - The text to be translated.
* @param {string} targetLanguage - The language to translate the text into.
* @param {string} [sourceLanguage='auto'] - The source language of the text.
* @return {string} The translated text.
* @customfunction
*/
function GT_TRANSLATE(text, sourceLanguage, targetLanguage) {
// sourceLanguage = sourceLanguage || 'auto';
var apiKey = PropertiesService.getUserProperties().getProperty('GT_API_KEY');
var url = 'https://translation.googleapis.com/language/translate/v2?key=' + apiKey;
var data = {
'q': text,
'source': sourceLanguage,
'target': targetLanguage
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
var jsonResponse = JSON.parse(response);
if (jsonResponse.error) {
throw jsonResponse.error.message;
}
return jsonResponse.data.translations[0].translatedText;
}
function AZ_TRANSLATE(text, sourceLanguage, targetLanguage) {
const endpoint = 'https://api.cognitive.microsofttranslator.com';
const path = '/translate?api-version=3.0';
const url = endpoint + path + '&from=' + sourceLanguage + '&to=' + targetLanguage;
const body = JSON.stringify([{ 'Text': text }]);
var apiKey = PropertiesService.getUserProperties().getProperty('AZ_API_KEY');
const headers = {
'Ocp-Apim-Subscription-Key': apiKey,
'Ocp-Apim-Subscription-Region': AZ_REGION,
'Content-type': 'application/json',
'X-ClientTraceId': Utilities.getUuid()
};
const options = {
'method' : 'post',
'headers': headers,
'payload' : body
};
try {
const response = UrlFetchApp.fetch(url, options);
const json = JSON.parse(response.getContentText());
return json[0].translations[0].text;
} catch (error) {
Logger.log(error);
return 'Error: Unable to translate';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment