I wrote this piece of code below - it works as desired on my desktop.
I want to use it on Android in the Google doc app.
How can I achieve this? What would be the simplest way to use this code on Android in the Google app?
function onEdit() {
var ui = DocumentApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Latein')
.addItem('Finite Verbform', 'highlightVerbform')
.addItem('Infinitiv', 'highlightInfinitiv' )
.addSeparator()
.addItem('Nominativ', 'highlightVerbform')
.addItem('Genitiv', 'highlightGenitiv' )
.addItem('Dativ', 'highlightDativ')
.addItem('Akkusativ', 'highlightAkkusativ' )
.addItem('Ablativ', 'highlightAblativ' )
.addSeparator()
.addItem('Einleitendes/verbindendes Wort', 'highlightBinde' )
.addSeparator()
.addItem('Kommentar', 'recurCommentaryPrompt' )
.addToUi();
}
function recurCommentaryPrompt() {
var html = HtmlService.createHtmlOutputFromFile('Dialog')
.setWidth(400)
.setHeight(100);
DocumentApp.getUi()
.showModalDialog(html, 'Kommentareingabe');
// DocumentApp.getUi().alert(val);
}
function insert(a){
//Logger.log(a);
if ( a !== '' )
{
insertSuperscript(a);
}
else
DocumentApp.getUi( ) . alert("Leer");
}
function insertSuperscript(text) {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var element = cursor.insertText(text).setForegroundColor('#ff0000').setBackgroundColor('#FFFFFF').setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
function highlightVerbform(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#ff0000')
.setBold(...range, true);
});
}
}
function highlightInfinitiv(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#ff7300')
.setBold(...range, true);
});
}
}
function highlightGenitiv(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#ffe600')
.setBold(...range, true);
});
}
}
function highlightDativ(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#62ff00')
.setBold(...range, true);
});
}
}
function highlightAkkusativ(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#4287f5')
.setForegroundColor(...range, '#FFFFFF')
.setBold(...range, true);
});
}
}
function highlightAblativ(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#000000')
.setForegroundColor(...range, '#FFFFFF')
.setBold(...range, true);
});
}
}
function highlightAblativusAbsolutus(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = "( "+ [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText() + " )"
.setBackgroundColor(...range, '#000000')
.setForegroundColor(...range, '#FFFFFF')
.setBold(...range, true);
});
}
}
function highlightBinde(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#666666')
.setForegroundColor(...range, '#EEEEEE')
.setBold(...range, true);
});
}
}
function menuItem1() {
var doc = DocumentApp.getActiveDocument();
var docText = doc.editAsText();
var text = docText.getSelection();
DocumentApp.getUi() // Or DocumentApp or FormApp.
.alert(text);
}
function menuItem2() {
DocumentApp.getUi() // Or DocumentApp or FormApp.
.alert('You clicked the second menu item!');
}
function highlightText(){
var selection = DocumentApp.getActiveDocument().getSelection();
if(selection){
selection.getRangeElements().forEach(e => {
var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
e.getElement().asText()
.setBackgroundColor(...range, '#FFFF00')
.setBold(...range, true);
});
}
}