[英]Calling an API in Google Sheets only when changed have been applied to a cell
I'm Thomas and pretty new to appscript.我是 Thomas,对 appscript 很陌生。 I managed to use it in order to translate cells in a google sheet using DeepL API.我设法使用它来使用 DeepL API 翻译谷歌表格中的单元格。 I did it thanks to this post : https://webapps.stackexchange.com/questions/149026/integrate-deepl-with-google-sheets But I have now an other problem.感谢这篇文章,我做到了: https ://webapps.stackexchange.com/questions/149026/integrate-deepl-with-google-sheets 但我现在还有另一个问题。 The data is refreshed each time the sheet is opened hence consuming a lot of translation characters.每次打开工作表时都会刷新数据,因此会消耗大量翻译字符。 The 500000 characters/month limit has been reached in only 2 days of using this sheet... And we were only 2 using it.使用此表仅 2 天就达到了每月 500000 个字符的限制……而我们只有 2 人使用它。 Is there any way of refreshing a cell only when the input content has been changed ?只有当输入内容发生变化时,有没有办法刷新单元格? Let's say content input is in cell A1 and translated content - output- is in cell A2.假设内容输入在单元格 A1 中,翻译的内容 - 输出 - 在单元格 A2 中。 I want A2 to be refreshed only when A1 has changed.我希望仅在 A1 更改时刷新 A2。 Thanks a lot to all for your help !非常感谢大家的帮助!
I am not sure how to disable the refresh on opening the sheet, but there may be another way to achieve what you want:我不确定如何在打开工作表时禁用刷新,但可能有另一种方法可以实现您想要的:
After the DeepL translation runs and the cells are filled with the translated text, you could save the results to those cells by copying and pasting the values over the top.在 DeepL 翻译运行并且单元格被翻译后的文本填充后,您可以通过将值复制并粘贴到顶部来将结果保存到这些单元格中。 That is:那是:
We finaly found a solution, thanks to my IT guy at work.感谢我的 IT 人员,我们终于找到了解决方案。 Here it is :这里是 :
function onEdit2(e) {
// Vérifie que l'onglet est à traiter
if (e.source.getActiveSheet().getName() !== 'Spare Parts Translation') return;
// Si valeur colonne 3 modifiée, on modifie les traductions
if (e.range.columnStart == 3) {
// Texte à traduire
var texte_fr = e.source.getActiveSheet().getRange(e.range.rowStart, e.range.columnStart).getValue();
// Ecrit texte traduit
e.source.getActiveSheet().getRange(e.range.rowStart, e.range.columnStart + 1).setValue(traduit(texte_fr,"en"));
e.source.getActiveSheet().getRange(e.range.rowStart, e.range.columnStart + 2).setValue(traduit(texte_fr,"de"));
e.source.getActiveSheet().getRange(e.range.rowStart, e.range.columnStart + 3).setValue(traduit(texte_fr,"es"));
e.source.getActiveSheet().getRange(e.range.rowStart, e.range.columnStart + 4).setValue(traduit(texte_fr,"it"));
}
}
function traduit(texte,lang) {
var response = UrlFetchApp.fetch("https://api-free.deepl.com/v2/translate?auth_key=*ENTER YOUR API KEY HERE*&text=" + texte + "&target_lang=" + lang + "&source_lang=fr", {muteHttpExceptions: true});
var json = response.getContentText();
var data = JSON.parse(json);
if (data["message"] == "Quota Exceeded") {
var texte = data;
}else{
var texte = data["translations"][0]["text"];
}
return texte;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.