如何在当前工作表上打印脚本的结果 [英] How to print the result of a script on the current sheet

查看:47
本文介绍了如何在当前工作表上打印脚本的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个脚本,该脚本通过Deepl API翻译工作表单元格中的文本.

I am working with a script that translates a text in a cell of a sheet through the Deepl API.

感谢同事的帮助,它可以正常工作(使用Deepl API和Google表格).

Thanks to a colleague help it worked correctly (Use Deepl API and google sheets).

在这种情况下,我需要知道如何将翻译结果包含在同一张纸的单元格中.

In this case, I need to know how I can make the result of the translation to be included in a cell of the same sheet.

我的数据对象如下.第一个脚本从第2列第3行中提取文本.如果将结果打印在第2列第4行中,将是完美的选择,但我不知道该怎么做.

My data object is the following. The first script picks up the text from column 2, row 3. It would be perfect if the result is printed in column 2, row 4, but I don't know exactly how to do it.

我提供了相应数据的图像

I include an image of the corresponding data

我正在使用此脚本来翻译工作表中单元格的文本:

I am using this script to translate the text of a cell in my sheet:

function deeplapi() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var text = sheet.getRange(3,2).getValue(); // define text before response
  var response = UrlFetchApp.fetch("https://api.deepl.com/v2/translate?auth_key=xxxx-xxxx-xxxx-xxxx-xxxx&text="+ text +"&target_lang=en&source_lang=es");
  var json = response.getContentText(); 
  var data = JSON.parse(json); 
  Logger.log(data);
}

通过这种方式,我可以在记录"部分获得结果,但格式如下:Información{translations = [{text =我是Carlos,detected_source_language = ES}]}

This way I get the result in the records section, but with this format: Información {translations=[{text=Hi, I'm Carlos, detected_source_language=ES}]}

首先,我尝试仅提取本节中的翻译,然后将其添加到同一张纸中.有了这个,我得到了确切的翻译,但是我不知道如何将其包括在我的工作表的特定范围内.

First, I have tried to extract only the translation in this section and then add it in the same sheet. With this I get the exact translation, but I don't know how to include it in a specific range of my sheet.

Logger.log(data.translations [0] .text);

除此之外,我认为要获得此结果并将其粘贴到工作表中,我将不得不使用:

In addition to this, I think that to get this result and paste it in the sheet I would have to use:

sheet.getRange().setValues(sortedOutput);

反正我不确定

推荐答案

在脚本底部尝试类似的操作,将翻译文本粘贴到单元格 B4 :

Try something like that at the bottom of your script to paste the translation text into cell B4:

sheet.getRange('B4').setValue(data.translations[0].text)

可重现的最小示例:

const data = {translations:[{text:"Hi, I'm Carlos", detected_source_language:"ES"}]};
console.log(data.translations[0].text)

这篇关于如何在当前工作表上打印脚本的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆