Google Script Run never executed

659 views
Skip to first unread message

Alessio

unread,
Jun 29, 2020, 11:45:24 AM6/29/20
to Google Apps Script Community

I am trying to create a simple addon in Google Docs, but after reading the documentation I cannot figure out how it is possible that google.script.run doesn't work properly. Here is my base code:

code.gs

function onOpen(e) {
  DocumentApp.getUi().createAddonMenu()
      .addItem('Start', 'showSidebar')
      .addToUi();
}

function onInstall(e) {
  onOpen(e);
}

function showSidebar() {
  var ui = HtmlService.createHtmlOutputFromFile('sidebar')
      .setTitle('NeuralText');
  DocumentApp.getUi().showSidebar(ui);
}

function dummyFunc(){
  var doc_id = DocumentApp.getActiveDocument().getId();
  Logger.log(doc_id);
  return doc_id;
}

sidebar.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <!-- The CSS package above applies Google styling to buttons and other elements. -->

    <style>
    .branding-below {
      bottom: 56px;
      top: 0;
    }
    .branding-text {
      left: 7px;
      position: relative;
      top: 3px;
    }
    .col-contain {
      overflow: hidden;
    }
    .col-one {
      float: left;
      width: 50%;
    }
    .logo {
      vertical-align: middle;
    }
    .radio-spacer {
      height: 20px;
    }
    .width-100 {
      width: 100%;
    }
    </style>
  </head>
  <body>
    <div class="sidebar branding-below">
    <div class="output"></div>
      <form>
        <div class="block form-group">
          <label for="translated-text"><b>Insert URL</b></label>
          <input class="width-100" id="shared-brief"/>
        </div>
        <div class="block" id="button-bar">
          <button class="blue" id="test-btn">Test</button>
        </div>
      </form>
      
    </div>

    <div class="sidebar bottom">
      <img alt="Add-on logo" class="logo" src="https://www.gstatic.com/images/branding/product/1x/translate_48dp.png" width="27" height="27">
      <span class="gray branding-text">Translate sample by Google</span>
    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $(function() {
        $('#test-btn').click(printTest);
        google.script.run.withSuccessHandler(onSuccess)
            .withFailureHandler(showError).dummyFunc();
      });

      function printTest() {
        console.log('test');
      }

      function showError() {
       console.log('error')
      }

      function onSuccess() {
        console.log('success')
      }
    </script>
  </body>
</html>

appsscript.json

{
  "timeZone": "Europe/Paris",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
                  "https://www.googleapis.com/auth/script.container.ui",
                  "https://www.googleapis.com/auth/documents",
                  "https://www.googleapis.com/auth/drive"]
}

If I am in the editor and run dummyFunc, I can see in Log the ID of the Doc. But if I test my addon I keep getting 'error' from the client-side function showError. What am I doing wrong?

Alan Wells

unread,
Jun 29, 2020, 12:16:43 PM6/29/20
to Google Apps Script Community
After running the code as a add-on, from the code editor, choose "View" and then click "Executions."
The execution should have a red word "Failed"
Expand the row and look for an error message.
You can also look at View - Stackdriver Logs

Alessio Nittoli

unread,
Jun 29, 2020, 1:15:22 PM6/29/20
to google-apps-sc...@googlegroups.com
Thank you Alan!
Unfortunately in "Executions" I didn't found any error, but in View - Stackdriver Logs I can see a We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED. message How can I fix this? Thank you again!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/50d78be7-b843-4098-8033-45b7845e57d5o%40googlegroups.com.

Alan Wells

unread,
Jun 29, 2020, 1:33:19 PM6/29/20
to Google Apps Script Community
I'm not familiar with that error message.
But obviously, there is a permissions error, and some kind of storage is being attempted to be accessed.
Unless that error message is completely wrong, then you must have some other code?
When JavaScript runs, the code is first evaluated for being correct before anything actually runs.
Normally, when you save the code in the code editor, errors will be caught and you'll get an error message
so that you can fix it.
But sometimes, there is an "uncaught error."  In other words, the code gets saved even though there is a problem with it.
And then the code fails when it actually runs.
Even when you run the code from the editor, it works.  But then when you run it as a add-on it fails.
So, the code is being evaluated differently in each situation.
There have been issues with how the the new V8 version evaluates the code, but I'm not sure if that's the problem.
You could try reverting back to:
  "runtimeVersion": "DEPRECATED_ES5"
Is there some other code in the project, or have you provided 100% of the code?
Are you using Properties Service?

On Monday, June 29, 2020 at 1:15:22 PM UTC-4, Alessio wrote:
Thank you Alan!
Unfortunately in "Executions" I didn't found any error, but in View - Stackdriver Logs I can see a We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED. message How can I fix this? Thank you again!

On Mon, 29 Jun 2020 at 18:16, Alan Wells <aj.a...@gmail.com> wrote:
After running the code as a add-on, from the code editor, choose "View" and then click "Executions."
The execution should have a red word "Failed"
Expand the row and look for an error message.
You can also look at View - Stackdriver Logs

On Monday, June 29, 2020 at 11:45:24 AM UTC-4, Alessio wrote:

I am trying to create a simple addon in Google Docs, but after reading the documentation I cannot figure out how it is possible that google.script.run doesn't work properly. Here is my base code:


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Alessio

unread,
Jun 29, 2020, 2:13:54 PM6/29/20
to Google Apps Script Community
Hi Alan,
thank you again for your reply.
I have disabled the new V8 engine and everything works as excepted.
This is a really strange behavior and, above all, undocumented.
Thank you for your help, very precious!
Have a nice day.

Adam Morris

unread,
Jun 29, 2020, 6:10:54 PM6/29/20
to google-apps-sc...@googlegroups.com
That error indicates google script run is working correctly, but is running in a context in which it does not have permissions to perform the action.

Do you have multiple google accounts? Are you using Safari? I have seen that issue when I was using safari, and it didn’t open windows in the same account.

--
Reply all
Reply to author
Forward
0 new messages