How to make this docs code work on Android?

Viewed 78

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);
    });
  }
}
2 Answers

Tl;Dr it's not possible as the code depends on methods only available on desktop. It's not clear to me if an add-on for the Google Docs app for Android supports methods like DocumentApp.getActiveDocument().getSelection().


To create user interface elements using Google Apps Script for Android / iOS you might use the Card Service (Class CardService) instead of using the Document Service (Class DocumentApp) and the HTML Service (Class HtmlService)

From https://developers.google.com/apps-script/reference/card-service

This service allows scripts to configure and build card and widget components and behaviors for a UI. The UI structures you build with this service are automatically available in both desktop and mobile devices, so you don't need to develop separate UIs for both.

Please bear in mind that the CardService only works for developing Google Workspace Add-Ons, not for Editor Add ons. Based on https://developers.google.com/apps-script/add-ons/how-tos/starting-addons, on mobile, Workspace Add-ons can only be started from the Gmail app for Android / iOS.

The Docs app and Sheets app have an Add-ons menu with Get Addons and Manage Add-ons options. The Slides app hasn't this menu. Get Addons open the Play Store app, rather than the Google Workplace Marketplace App.

The link in the Google Apps Script Add-ons samples page to the sample for creating an Android app using Google Apps Script now returns page not found.

On 2016 Google made a call for developers to create Android Add-ons for Google Docs And Sheets, the links to the documentation and samples returns 404 Page not found error.

Notes:

  1. The Get Add-Ons from the Google Docs app for Android, opens the Play Store showing only three Android apps.
  2. The Test Deployment of a Google Workspace Add-on doesn't make the add-on to be available in the Google Docs app for Android.
  3. Few minutes ago I published a Google Workspace Add-on set for Internal use (I used the code from the Translate Workspace Add-on sample). So far it's not available in Google Docs app for Android. Apparently it will not be "soon".

Resources

References

In the meantime, I've found an acceptable solution for my problem.

I'm not able to remember where I found it here ... but we can open our document on an Android device as desktop website - afterwards, I was able to start my script.

The Google Chrome Browser showed some unexpected behaviour ... while opening the commentary prompt, the document window enlarges ... had no idea why.

So I tried the Samsung browser on my tablet ... and now the script works fine!

Of course: it is not the best solution, but it is the simplest way to use my code on an Android device.

Kind regards, Frank

Related