Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Created November 24, 2016 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennettscience/03dddfc306e57a453b02d9d1eb409d7b to your computer and use it in GitHub Desktop.
Save bennettscience/03dddfc306e57a453b02d9d1eb409d7b to your computer and use it in GitHub Desktop.
Link two documents for translation updates
var ui = DocumentApp.getUi();
var docProps = PropertiesService.getDocumentProperties();
// Grab the document body.
// No formatting, tables, etc are kept.
function getText() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
Logger.log(body.getText());
var array = [];
array.push(body.getText());
sendTheText(array);
}
// Check for a Doc ID to push to. If none, ask for it.
// Push the doc and translate to Spanish.
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);
Logger.log(response);
}
var childDoc = DocumentApp.openById(docProps.getProperty("childId"));
var es = LanguageApp.translate(e, 'en', 'es');
childDoc.getBody().clear();
childDoc.getBody().appendParagraph(es);
}
// Reset the doc you're pushing updates to
function clearProps() {
docProps.setProperty("childId", "");
}
function onOpen() {
ui.createMenu("Custom Translate").addItem("Run", "getText").addItem("Clear IDs","clearProps").addToUi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment