1

I have setup an Installable trigger that launches a google picker function when my google sheet is opened [showPicker()]. The google sheet can be accessed by a url by anyone.

When I open the google sheet the script runs fine and the file picker shows up on screen. However, when I logged into a different account that i shared the google sheet with, the script appears to not run and the file picker doesn't show when the spreadsheet is opened.

.GS file

function showPicker() {
  var html = HtmlService.createHtmlOutputFromFile('Picker.html')
      .setWidth(600)
      .setHeight(425)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
 SpreadsheetApp.getUi().showModalDialog(html, 'Select CSV File');
}

function getOAuthToken() {
  DriveApp.getRootFolder();
  return ScriptApp.getOAuthToken();
}

Picker.html file

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
  <script type="text/javascript">
    var DIALOG_DIMENSIONS = {
        width: 600,
        height: 425
    };
    var pickerApiLoaded = false;

    function onApiLoad() {
        gapi.load('picker', {
            'callback': function() {
                pickerApiLoaded = true;
            }
        });
        google.script.run.withSuccessHandler(createPicker)
            .withFailureHandler(showError).getOAuthToken();
    }

    function createPicker(token) {

        if (pickerApiLoaded && token) {

            var docsView = new google.picker.DocsView()
                .setIncludeFolders(true)
                .setMimeTypes('application/vnd.google-apps.folder,text/csv')
                //.setSelectFolderEnabled(true);

            var picker = new google.picker.PickerBuilder()
                .addView(docsView)
                .enableFeature(google.picker.Feature.NAV_HIDDEN)
                .hideTitleBar()
                .setSize(DIALOG_DIMENSIONS.width - 2, DIALOG_DIMENSIONS.height - 2)
                .setOAuthToken(token)
                .setCallback(pickerCallback)
                .setOrigin('https://docs.google.com')
                .build();

            picker.setVisible(true);

        } else {
            showError('Unable to load the file picker.');
        }
    }

    /**
     * A callback function that extracts the chosen document's metadata from the
     * response object. For details on the response object, see
     * https://developers.google.com/picker/docs/result
     *
     * @param {object} data The response object.
     */
    function pickerCallback(data) {
        var action = data[google.picker.Response.ACTION];
        if (action == google.picker.Action.PICKED) {
            var doc = data[google.picker.Response.DOCUMENTS][0];
            var id = doc[google.picker.Document.ID];
            // Show the ID of the Google Drive folder
            //document.getElementById('result').innerHTML = id;
            //Run Import CSV
        google.script.run.withFailureHandler(onFailure)
          .importCSVFromGoogleDrive(id);

            //google.script.run.importCSVFromGoogleDrive(id);

            google.script.host.close();

        } else if (action == google.picker.Action.CANCEL) {
            google.script.host.close();
        }
    }

    function showError(message) {
        document.getElementById('result').innerHTML = 'Error: ' + message;
    }

    function onFailure(error) {
        var div = document.getElementById('output');
        div.innerHTML = "ERROR: " + error.message;
      }
  </script>
</head>

<body>
    <div>
        <p id='result'></p>
        <div id="output"></div>
    </div>
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>

I have tried looking through the documentation on authorization for shared users, but I'm a bit lost and have no idea why the script will not launch?? Any help would be greatly appreciated :)

STACKDRIVER LOGS (Using other google acct):

Error - Sep 2, 2019, 6:05:36 PM - You do not have permission to call showModalDialog at showPicker(Code:724) 

EXECECUTION TRANSCRIPT (Using other google acct):

[19-09-02 01:05:36:307 PDT] HtmlService.createHtmlOutputFromFile([Picker.html])[0 seconds] 
[19-09-02 01:05:36:308 PDT] HtmlOutput.setWidth([600]) [0 seconds] 
[19-09-02 01:05:36:308 PDT] HtmlOutput.setHeight([425]) [0 seconds] 
[19-09-02 01:05:36:309 PDT] HtmlOutput.setSandboxMode([IFRAME]) [0 seconds] 
[19-09-02 01:05:36:310 PDT] SpreadsheetApp.getUi() [0 seconds] 
[19-09-02 01:05:36:315 PDT] Execution failed: You do not have permission to call showModalDialog (line 724, file "Code") 
[0.004 seconds total runtime] 
  • Show browser console logs, stackdriver logs and execution transcripts for the other account. – TheMaster Sep 02 '19 at 07:38
  • @TheMaster I've attached the Logs in the question above, appears something to do with the showModalDialog??? – user3273216 Sep 02 '19 at 08:15
  • Did you try running the code from the editor from the second account or just through the trigger? – AMolina Sep 02 '19 at 10:15
  • @user I had a discussion with other users(I couldn't find it now) that You cannot show html automatically to other users. It was possible before 2016?. I think Google removed it for security purposes. The best you can do is add a menu item or something like [this](https://stackoverflow.com/questions/57480181/) to inform users to open picker. Also note that installable triggers run under your account-so your drive files will be shown to others. – TheMaster Sep 02 '19 at 10:20

1 Answers1

1

It is only possible for the owner of the spreadsheet to trigger html dialog boxes. When other users open your spreadsheet an error report will be created saying they do not have permission to call the modeless dialog. I'm guessing this is a security feature.

Other users can manually open the picker by selecting from a menu drop down.

However, you can trigger Browser.msgBox() for everyone who opens the spreadsheet.

  • Thanks, so just to clarify: Browser.msgBox() will just trigger a dialog display message for everyone who opens the spreadsheet but wont be able to run any other functions off it (i.e. prompt the file picker). – user3273216 Sep 03 '19 at 00:28
  • In addition to Browser.msgBox(), you can also use (probably a better option) SpreadsheetApp.getUi().alert(). – Efficient Small Business Sep 07 '19 at 18:08