我想将要翻译的文本放在 Google 文档中,这样每个句子都位于表格的一行中。下一列的每一行都是相应的机器翻译的句子。最后,还有第三个空白列,我将在其中为每个句子写下自己的翻译。
就像这样
用于访问特定的 Google Doc、插入表格、拆分每个句子的文本,然后将每个句子写入表格的一行的命令行代码是什么样的?
我知道 Google API,但到目前为止,我一直在努力解决身份验证问题。如果有人能勾勒出我的脚本应该是什么样子的大致轮廓,希望我可以补充细节。
我尝试过的这个代码适用于 Google Apps Scipt:
function translate() {
var doc = DocumentApp.openById('documentID');
var body = doc.getBody();
var str = "An elephant is the biggest living animal on land. It is quite huge in size. It is usually black or grey in colour. Elephants have four legs, a long trunk and two white tusks near their trunk. Apart from this, they have two big ears and a short tail. Elephants are vegetarian. They eat all kinds of plants especially bananas. They are quite social, intelligent and useful animals. They are used to carry logs of wood from one place to another. They are good swimmers.";
//Split paragraph into sentences.
var result = str.match( /[^\.!\?]+[\.!\?]+/g ).map(str => str.trim());;
var translated = [];
result.forEach(res => {
translated.push(LanguageApp.translate(res, 'en', 'es'));
})
var table = body.appendTable();
for(var i = 0; i <= result.length; i++){
var tr = table.appendTableRow();
if(i == 0){
tr.appendTableCell("Origin");
tr.appendTableCell("Translated");
tr.appendTableCell("");
}else{
tr.appendTableCell(result[i-1]);
tr.appendTableCell(translated[i-1]);
tr.appendTableCell("");
}
}
}
问题是我必须在命令行上使用 Google API 客户端库执行此操作,我还可以使用相同的代码吗?