有没有什么方法可以让我们使用谷歌(应用程序)脚本翻译整个谷歌文档文件,但仍然保持所有格式、表格和图像的完整性



我正试图模仿;翻译文档";通过编写可以自动翻译一系列文档的谷歌(应用程序(脚本,在谷歌文档中使用该工具。

我试过LanguageApp。Translate((,但此语法只返回一个未格式化的字符串并删除所有表边界(纯字符串(。

这是我的代码:

function TranslateFunction() {
//Get the files in your indicated Folder
var TargetFolderID = '1VUNGtqiNbnHhIFCXmbdSwNZ-vZ5NWVTE'; //Paste the folder ID here to start
var folder = DriveApp.getFolderById(TargetFolderID);
var files = folder.getFiles();
//Get all the files' ID in the folder above
while (files.hasNext()){
var file = files.next();
var fileID = file.getId();
//Convert each file in the folder from Docx (Word) to Docs (Google)
var docx = DriveApp.getFileById(fileID);
var newDoc = Drive.newFile();
var blob = docx.getBlob();
var file=Drive.Files.insert(newDoc,blob,{convert:true});
DocumentApp.openById(file.id).setName(docx.getName().slice(0,-5));  
//Activate the file
var doc = DocumentApp.openById(file.id);
//Create a new Docs file to put the translation in + Name
var newDoc = DocumentApp.create("|EN| " + docx.getName().slice(0,-5)); 

//Get the text in the file
var bodyText = doc.getBody().getText();
//Translate the text and append it into the new Docs
Translatedtext = LanguageApp.translate(bodyText,'vi','en');
newDoc.getBody().appendParagraph(Translatedtext);
}
}

遇到了同样的挑战,对我来说似乎有效的方法是首先创建我试图翻译的文档的副本,然后循环浏览所有段落并逐一翻译。

在我的用例中,我想从谷歌工作表中协调这一点,因为我主要需要同一文档的多个不同翻译,然后需要同事审查,因此我通常会将源文档URL放入工作表中,运行一个将其翻译为所有所需语言的脚本,然后将URL反馈到工作表中。

对于源语言和目标语言,请使用ISO-639代码。

function translateDoc (sourceURL, sourceLang, targetLang) {
// get ID from URL, open file and get ID from the folder it's in
var sourceDocId = sourceURL.match(/[-w]{25,}/);
var sourceDoc = DriveApp.getFileById(sourceDocId);
var sourceTitle = sourceDoc.getName();
var sourceFolderId = sourceDoc.getParents().next().getId();
// create a copy in the same folder and indicate the language in the title
var targetTitle = "[" + String(targetLang).toUpperCase() + " translation] " + sourceTitle;
var targetDoc = sourceDoc.makeCopy(targetTitle, DriveApp.getFolderById(sourceFolderId));
// open copied Doc and start looping through the content
var doc = DocumentApp.openById(targetDoc.getId());
var body = doc.getBody();
var elements = body.getParagraphs();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var translatedText = LanguageApp.translate(element.getText(), sourceLang, targetLang);
if(translatedText != "") element.setText(translatedText);
// I had an error with an empty paragraph, so avoiding this by only updating when not empty
}
doc.saveAndClose();
return "https://docs.google.com/document/d/" + targetDoc.getId(); // returns URL of translated doc
}
function translateDocs () {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var rows = sheet.getDataRange().getValues();
var languages = ["de", "fr", "es", "it", "nl", "pl", "pt"];
var sourceFileURL, translationDoc;
rows.forEach(function(row, index){
if (index === 0) return; // table headers
if (row[0] == "") return; // no source file
if (row[1] != "") return; // column 2 not empty --> already translated this file
sourceFileURL = row[0];
for (var i = 0; i < languages.length; i++) {
translationDoc = translateDoc(sourceFileURL, "en", languages[i]);
sheet.getDataRange().getCell(parseInt(index) + 1,parseInt(i) + 2).setValue(translationDoc);
}
});
}

最新更新