1

This is the follow-up to my previous question for which @Tanaike proposed a very good solution. Here is a bit explanation of workflow.

  1. Script runs using onEdit() trigger in Google sheets
  2. A dialog box pop-up in sheet where the user uploads an image from a computer/laptop
  3. This image will go to Google drive and the image link will be imported into a Google sheet cell.

Here is the image of the dialog box which pops up in google sheets on script run to upload images:

Dialog box

Here is the code in Code.gs

function addImage() { 

  var filename = 'Row';
  var htmlTemp = HtmlService.createTemplateFromFile('Index');
  htmlTemp.fName = filename;
  htmlTemp.position = 2;
  var html = htmlTemp.evaluate().setHeight(96).setWidth(415);
  var ui = SpreadsheetApp.getUi();
  ui.showModalDialog(html, 'Upload');
}

function upload(obj, rowNum) {
  var newFileName = obj[2];
  var blob = Utilities.newBlob(...obj);
  var upFile = DriveApp.getFolderById('[folderid]').createFile(blob).setName(newFileName);
  var fileUrl = upFile.getUrl();
  var urlCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(rowNum, 5);
  urlCell.setValue('=HYPERLINK("' + fileUrl + '","View image")');
  return "Done.";
}

Here is the code in HTML & Javascript side: Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_center">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
        <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
  </head>
  <body>
  <form id="myForm">
      Please upload image below.<br /><br />
    <input type="hidden" name="fname" id="fname" value="<?= fName ?>"/>
    <input type="hidden" name="position" id="position" value="<?= position ?>"/>
    <input type="file" name="file" id="file" accept="image/jpeg,.pdf" />
    <input type="button" value="Submit" class="action" onclick="formData(this.parentNode)" />
    <input type="button" value="Close" onclick="google.script.host.close()" />
  </form>
  <script>
  //Disable the default submit action  using “func1”
   window.onload=func1;
   function func1() {
      document.getElementById('myForm').addEventListener('submit', function(event) {
            event.preventDefault();
          });  
   }

   function formData(obj) {
  const file = obj.file.files[0];
  const fr = new FileReader();
  fr.readAsArrayBuffer(file);
  fr.onload = f =>
    google.script.run.withSuccessHandler(closeIt).upload([[...new Int8Array(f.target.result)], file.type, obj.fname.value], obj.position.value);
}  

  function closeIt(e){
      console.log(e);
      google.script.host.close();
  };   

    </script>
</body>
</html>

This script works perfectly when google sheets is opened in the web browser(chrome), but I want to run this in android based google sheet app, in the google sheet app, this script is fired using the onEdit() trigger but a dialog box does not show up where the user can upload the image from mobile as well.

is there any workaround to upload the image from the Google sheet app as we do with the usual google sheets in the chrome browser? I hope I was clear in explaining my issue. Thank you

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 1
    When you run a script with a trigger there is no user so no user interface is created and hence you no longer have any access to Ui. – Cooper Oct 23 '22 at 15:40
  • when I clicked on a checkbox, I am the editor in the sheet and edit the cell. I am the user there. when I do this same thing in google sheet (chrome browser), it pops up the dialog box –  Oct 23 '22 at 15:45
  • Related/Possible duplicate: https://stackoverflow.com/questions/33373826/executing-google-apps-script-functions-from-mobile-app/47207643#47207643 – TheMaster Oct 23 '22 at 16:01
  • Which function do you wish the trigger handlerFunction to be. – Cooper Oct 23 '22 at 20:15
  • If you chose add image then the Ui will not be available. If you chose upload then where will the parameters come from? Either way it's not going to work – Cooper Oct 23 '22 at 20:17

1 Answers1

1

The options to use Google Apps Script from a Google app in a mobile device are very limited because only the user interface elements of Google Workspace addons for Gmail work on mobile devices.

One option that you might explore is to create a web app using Google Apps Script. See https://developers.google.com/apps-script/guides/web.

Another option that you might explore is to create an app using Google AppSheet. It's already integrated with Google Sheets. There is a menu option in the Google Sheets Extensions menu, but before using it on an important spreadsheet, the best it that you spend some time learning about it.

Rubén
  • 34,714
  • 9
  • 70
  • 166