An interesting Stack Overflow question popped up about auto-translating one document into another when edits are made. Most Google services have things called “triggers” which run functions after some kind of event – opening the doc, making a change, etc.
A Google Doc does not have access to the onEdit
trigger, so there is no way to automatically run the translation unless you put it on a timer trigger, and that’s a waste of resources, especially if you’re not constantly updating the document. But, you can link two documents together via the ID and push changes made using a custom menu.
Grab a copy of the template with instructions.
Source:
var ui = DocumentApp.getUi();
var docProps = PropertiesService.getDocumentProperties();
function getText() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var array = [];
array.push(body.getText());
sendTheText(array);
}
function sendTheText(e) {
if (docProps.getProperty("childId") == "") {
var prompt = ui.prompt("Child doc ID", ui.ButtonSet.OK);
var response = prompt.getResponseText()
docProps.setProperty("childId", response);
}
var childDoc = DocumentApp.openById(docProps.getProperty("childId"));
var es = LanguageApp.translate(e, 'en', 'es');
childDoc.getBody().clear();
childDoc.getBody().appendParagraph(es);
}
function clearProps() {
docProps.setProperty("childId", "");
}
function onOpen() {
ui.createMenu("Custom Translate").addItem("Run", "getText").addItem("Clear IDs","clearProps").addToUi();
}