1

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);
    });
  }
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85

2 Answers2

2

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Ever seen workspace addon in mobile app? Although docs say that, I don't think that's true at all. – TheMaster Sep 10 '22 at 14:15
  • 1
    @TheMaster Some time ago I created a Google Workspace Add-On for Gmail for testing purposes. I just confirmed that it still works. Also I have some add-ons available on the Google Workspace Marketplace, they also still work, but I don't know if they were created using Google Apps Script. – Rubén Sep 10 '22 at 14:22
  • By the way, I very, very rarely use the Google editors mobile apps, I can't tell if there is already a nice a Workspace add-on available. – Rubén Sep 10 '22 at 14:28
  • Thanks for the information. I still don't think this is true for editor apps: Google docs or Google sheets. Though I'll be glad to be proven wrong. – TheMaster Sep 10 '22 at 14:29
  • @TheMaster I spend some time digging into this "mystery"... The Google Docs app for Android has three "add-ons". They are Android apps that are someway integrated with the Google Docs app, they show a menu on the Add-ons app. I'm wondering if this integration is possible only to selected Google partners. P.S. Answer updated. – Rubén Sep 10 '22 at 18:49
  • 1
    I'm aware of those addons, but Google d̶e̶p̶r̶e̶c̶a̶t̶e̶d̶ [sunset](https://developers.google.com/apps-script/guides/support/sunset) them in 2019. Also see [this](https://stackoverflow.com/a/45986845/) – TheMaster Sep 10 '22 at 19:35
  • Thanks @TheMaster. Now I think that this question is a duplicate of your second link. If you agree feel free to mark it as duplicate. – Rubén Sep 10 '22 at 19:42
  • Thanks for confirming that workspace addons don't work on Google docs app though, even though docs say they do. I think this answer shows research and this question is specific to [tag:google-docs], where none of those triggers[in my answer there] work. I think this can be a separate post. Or if you want to merge, maybe you could move your answer there. – TheMaster Sep 10 '22 at 19:44
  • 1
    Thanks a lot for your efforts - apparently, it is a little bit more compilcated or even impossible to achieve this goal. – Metamorphosis Sep 10 '22 at 20:15
  • I hope it's ok if I go a litlle bit "off topic" ... maybe you've seen in the code above the names "highlightGenitiv" and so on ... my intention was to create this functionality in order to practise Latin translations ... via the menu entries, we can highlight the parts of the sentence. The advantage: I should be able to use these macros on the desktop and on my Android tablet ... do you have another proposal how this can be done? I thought of OneDrive, but macros are not supported there ... – Metamorphosis Sep 10 '22 at 20:22
  • @Metamorphosis Comments are not intended for having extended discussions and this is becoming into one. Please checkout https://stackoverflow.com/tags/google-apps-script/info. There you will find a link to the "community chat" ( I have not participated recently on it) as well other resources that might be helpful. – Rubén Sep 10 '22 at 20:34
  • Rubén OP lacks the rep(20) to participate in chat. @Metamorphosis I can't think of a solution on the top of my head. May be you can try [softwarerecs.se] (Do check their rules and see if the question would be on topic there) – TheMaster Sep 10 '22 at 20:48
  • You're right, my commentary went too far, sorry ... I'm going to contact another forum for a different proposal. Thanks again to all of you for helping! – Metamorphosis Sep 11 '22 at 08:05
1

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

  • would you provide more clear details about your solution? – David Leal Sep 22 '22 at 16:50
  • Maybe my term "solution" is a little bit presumptuous :) There was no (simple) way to make my script work on Android with the Goggle Chrome browser. So I searched on my tablet for a different browser - the first one I found was the browser from Samsung. I went to my Google drive and opened my document, the same way I did as on my desktop. Go to the menu "Extensions > Apps Script" - the script opens, and it can be started in the same manner as on the desktop. As mentioned: it is not the optimal solution for this, but it works for my purposes. Hope this helps. – Metamorphosis Sep 24 '22 at 10:58