简体   繁体   English

仅当更改已应用于单元格时才在 Google 表格中调用 API

[英]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:那是:

  • select the cells you want to save选择要保存的单元格
  • Copy (Ctrl-C)复制 (Ctrl-C)
  • Paste special -> Values only (Ctrl-Shift-V)选择性粘贴 -> 仅值 (Ctrl-Shift-V)

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.

相关问题 每当 Google 表格中的单元格值发生更改时,都会运行 onEdit() function - Have an onEdit() function run whenever a cell value is changed in Google Sheets 当外部源(firebase)更改单元格时,如何在谷歌表格中触发谷歌应用程序脚本? - How to trigger a google apps script in google sheets when a cell is changed by an external source (firebase)? 如果另一个单元格的颜色发生更改,则在 Google 表格中为一个单元格添加时间戳 - Timestamp a cell in Google Sheets if the color of another cell is changed 在特定的 Google 表格选项卡上编辑单元格时发送 email 通知 - Send email notification when cell has been edited on a specific google sheets tab Google 表格 API 和 php - 查找单元格值 - Google Sheets API with php - Find a cell value 谷歌表格 API:读取单元格公式 - Google sheets API: Read cell formula Google表格在不需要时重新计算单元格 - Google sheets recalculating cell when not needed "在 Google 表格中选中单元格时锁定一行" - Lock a row when cell is checked in Google Sheets 选中复选框时调用 function - Google 表格 - Calling function when checking checkbox - Google Sheets 在 Google 表格中,有没有办法查看何时填充了单元格? - In Google Sheets, is there a way to see when a cell was populated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM