Created
March 25, 2020 17:31
-
-
Save PatPeter/d283be1631e5a57cd5e0e45c38111d21 to your computer and use it in GitHub Desktop.
Zisian to English Translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A special function that inserts a custom menu when the spreadsheet opens. | |
*/ | |
function onOpen() { | |
//var menu1 = [{name: 'Translate from Zisian to English', functionName: 'translateZisianToEnglish_'}]; | |
//var menu2 = [{name: 'Translate from English to Zisian', functionName: 'translateEnglishToZisian_'}]; | |
//SpreadsheetApp.getActive().addMenu('Translate Zisian-English', menu1); | |
//SpreadsheetApp.getActive().addMenu('Translate English-Zisian', menu2); | |
// https://developers.google.com/apps-script/reference/spreadsheet/sheet#getdatarange | |
// https://developers.google.com/apps-script/guides/support/best-practices | |
var ss = SpreadsheetApp.getActive(); | |
var sheet = ss.getSheetByName('Sheet1'); | |
var range = sheet.getDataRange(); | |
var values = range.getValues(); | |
var lookupZisianToEnglish = {}; | |
var lookupEnglishToZisian = {}; | |
for (var i = 0; i < values.length; i++) { | |
lookupZisianToEnglish[values[i][1].toString().toLowerCase()] = values[i][0].toString().toLowerCase(); | |
lookupEnglishToZisian[values[i][0].toString().toLowerCase()] = values[i][1].toString().toLowerCase(); | |
/*for (var j = 0; j < values[i].length; j++) { | |
if (values[i][j]) { | |
row = row + values[i][j]; | |
} | |
row = row + ","; | |
}*/ | |
Logger.log("Translate Zisian to English " + values[i][1].toString().toLowerCase() + " to " + values[i][0].toString().toLowerCase()); | |
//Logger.log("Translate English to Zisian " + values[i][0].toString().toLowerCase() + " to " + values[i][1].toString().toLowerCase()); | |
} | |
var cache = CacheService.getScriptCache(); | |
cache.put("lookupZisianToEnglish", JSON.stringify(lookupZisianToEnglish), 300); | |
cache.put("lookupEnglishToZisian", JSON.stringify(lookupEnglishToZisian), 300); | |
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. | |
.createMenu('Translate') | |
.addItem('Translate from Zisian to English', 'translateZisianToEnglish_') | |
.addItem('Translate from English to Zisian', 'translateEnglishToZisian_') | |
.addToUi(); | |
} | |
/** | |
* A set-up function that uses the conference data in the spreadsheet to create | |
* Google Calendar events, a Google Form, and a trigger that allows the script | |
* to react to form responses. | |
*/ | |
function translateZisianToEnglish_() { | |
var html = HtmlService.createHtmlOutputFromFile('index') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
html.append("<script>(function() { var mode1 = document.getElementById('mode1'); mode1.checked = true; })();</script>"); | |
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. | |
.showModalDialog(html, 'Translate from Zisian to English'); | |
/*var ss = SpreadsheetApp.getActive(); | |
var sheet = ss.getSheetByName('Sheet1'); | |
var range = sheet.getDataRange(); | |
var values = range.getValues(); | |
setUpForm_(ss, values); | |
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit() | |
.create();*/ | |
} | |
function translate(direction, text) { | |
var cache = CacheService.getScriptCache(); | |
var lookupZisianToEnglish = cache.get("lookupZisianToEnglish"); | |
lookupZisianToEnglish = JSON.parse(lookupZisianToEnglish); | |
if (lookupZisianToEnglish == null) { | |
throw "Zisian To English lookup is not populated." | |
} | |
//for (var n in lookupZisianToEnglish) { | |
// Logger.log(n + ' => ' + lookupZisianToEnglish[n]); | |
//} | |
var lookupEnglishToZisian = cache.get("lookupEnglishToZisian"); | |
lookupEnglishToZisian = JSON.parse(lookupEnglishToZisian); | |
if (lookupEnglishToZisian == null) { | |
throw "English To Zisian lookup is not populated." | |
} | |
//for (var n in lookupEnglishToZisian) { | |
// Logger.log(n + ' => ' + lookupEnglishToZisian[n]); | |
//} | |
//Logger.log("Size of lookupZisianToEnglish: " + Object.keys(lookupZisianToEnglish).length); | |
//Logger.log("Size of lookupEnglishToZisian: " + Object.keys(lookupEnglishToZisian).length); | |
var output = ''; | |
var parts = text.split(/\s/); | |
if (direction == 1) { | |
for (var n in parts) { | |
if (output != '') output += ' '; | |
var word = parts[n].trim().toLowerCase(); | |
if (lookupZisianToEnglish[word]) { | |
if (lookupZisianToEnglish[word] == 'i') | |
lookupZisianToEnglish[word] = 'I'; | |
output += lookupZisianToEnglish[word]; | |
} else { | |
output += parts[n] + '!'; | |
} | |
} | |
} else if (direction == -1) { | |
for (var n in parts) { | |
if (output != '') output += ' '; | |
var word = parts[n].trim().toLowerCase(); | |
var plural = false; | |
if (word.charAt(word.length - 1) == 's') { | |
plural = true; | |
word = word.substr(-1); | |
} | |
if (lookupEnglishToZisian[word]) { | |
output += lookupEnglishToZisian[word]; | |
if (plural) { | |
output += 'n'; | |
} | |
} else { | |
output += parts[n] + '!'; | |
} | |
} | |
} else { | |
throw "Invalid direction: " + direction; | |
} | |
return output; | |
//return "Success! " + direction + " " + text; | |
} | |
/** | |
* A set-up function that uses the conference data in the spreadsheet to create | |
* Google Calendar events, a Google Form, and a trigger that allows the script | |
* to react to form responses. | |
*/ | |
function translateEnglishToZisian_() { | |
var html = HtmlService.createHtmlOutputFromFile('index') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
html.append("<script>(function() { var mode2 = document.getElementById('mode2'); mode2.checked = true; })();</script>"); | |
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. | |
.showModalDialog(html, 'Translate from English to Zisian'); | |
/*var ss = SpreadsheetApp.getActive(); | |
var sheet = ss.getSheetByName('Sheet1'); | |
var range = sheet.getDataRange(); | |
var values = range.getValues(); | |
setUpForm_(ss, values); | |
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit() | |
.create();*/ | |
} | |
/** | |
* Creates a Google Form that allows respondents to select which conference | |
* sessions they would like to attend, grouped by date and start time. | |
* | |
* @param {Spreadsheet} ss The spreadsheet that contains the conference data. | |
* @param {Array<String[]>} values Cell values for the spreadsheet range. | |
*/ | |
function setUpForm_(ss, values) { | |
// Create the form and add a multiple-choice question for each timeslot. | |
var form = FormApp.create('Conference Form'); | |
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId()); | |
form.addTextItem().setTitle('Text to Translate').setRequired(true); | |
} | |
/** | |
* A trigger-driven function that sends out calendar invitations and a | |
* personalized Google Docs itinerary after a user responds to the form. | |
* | |
* @param {Object} e The event parameter for form submission to a spreadsheet; | |
* see https://developers.google.com/apps-script/understanding_events | |
*/ | |
function onFormSubmit(e) { | |
//var user = {name: e.namedValues['Name'][0], email: e.namedValues['Email'][0]}; | |
// Grab the session data again so that we can match it to the user's choices. | |
var response = []; | |
var values = SpreadsheetApp.getActive().getSheetByName('Sheet1') | |
.getDataRange().getValues(); | |
ui.alert("Form Submitted"); | |
/*for (var i = 1; i < values.length; i++) { | |
var session = values[i]; | |
var title = session[0]; | |
var day = session[1].toLocaleDateString(); | |
var time = session[2].toLocaleTimeString(); | |
var timeslot = time + ' ' + day; | |
// For every selection in the response, find the matching timeslot and title | |
// in the spreadsheet and add the session data to the response array. | |
if (e.namedValues[timeslot] && e.namedValues[timeslot] == title) { | |
response.push(session); | |
} | |
}*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
function onSuccess(result) { | |
var input = document.getElementById('output'); | |
input.value = result; | |
} | |
function onFailure(error) { | |
var input = document.getElementById('output'); | |
input.value = "ERROR: " + error.message; | |
} | |
function translate() { | |
var mode1 = document.getElementById('mode1'); | |
var mode2 = document.getElementById('mode2'); | |
var direction = mode1.checked ? mode1.value : mode2.value; | |
var ttt = document.getElementById('ttt'); | |
var text = ttt.value; | |
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).translate(direction, text); | |
} | |
</script> | |
</head> | |
<body> | |
<label for="mode">Translation Mode:</label><br> | |
<input id="mode1" name="mode" value="1" type="radio"><label for="mode">Zisian to English</label><br> | |
<input id="mode2" name="mode" value="-1" type="radio"><label for="mode">English to Zisian</label><br> | |
<br> | |
<label for="ttt">Text to Translate:</label><br> | |
<input id="ttt" name="ttt" type="text" value="" /><br> | |
<br> | |
<label for="output">Output:</label><br> | |
<input id="output" name="output" type="text" value="" /><br> | |
<br> | |
<input type="button" value="Translate" | |
onclick="translate();" /> | |
| |
<input type="button" value="Close" | |
onclick="google.script.host.close()" /> | |
<!--<script> | |
/*(function() { | |
var mode1 = document.getElementById('mode1'); | |
var mode2 = document.getElementById('mode2'); | |
if (translationDirection) { | |
var input = document.getElementById('output'); | |
input.value = mode1.value; | |
mode1.checked = true; | |
} else { | |
var input = document.getElementById('output'); | |
input.value = mode2.value; | |
mode2.checked = true; | |
} | |
})();*/ | |
</script>--> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment