0

The following code based on the one found here is working. Unfortunately in 3 days the 500.000/month characters were gone... But it doesn't have much sense because there are not so much text to finish the 500.000 characters in 3 days (actually I added a limit just in case of 350 characters per call)!

/**
 * Usage: =DEEPL($A1; "en"; "de")
 * API registered with xxxxx
 */
function DEEPL(text, sourceLang, targetLang) {

  if (text.length < 2){
    Logger.log(text.length);
    return "----"
  }
  else if(text.length >= 350){
    return "-beyond 350 characters maximum-"
  }
  else{
    var response = UrlFetchApp.fetch(
      "https://api-free.deepl.com/v2/translate?auth_key=ADD_YOUR_API_KEY_HERE" +
      "&text=" + encodeURIComponent(text) +
      "&target_lang=" + targetLang +
      "&source_lang=" + sourceLang
    );
    var json = response.getContentText();
    var data = JSON.parse(json);
    return data["translations"][0]["text"];
  }
}

As all translations have been "removed" by the text #ERROR!, it suggests to me that the problem is that the function is not only called when there is a modification of the cell, but also each time the sheet is opened, all functions are called again.

quote exceded

And also when we we sort it, again all functions are called.

sort google sheet screenshot

So the same translation API is called over and over.

How could I translate a text when there is an actual change only?

Rubén
  • 34,714
  • 9
  • 70
  • 166
chelder
  • 3,819
  • 6
  • 56
  • 90

0 Answers0