【问题标题】:Google Script Run never executedGoogle Script Run 从未执行
【发布时间】:2020-06-29 15:05:08
【问题描述】:

我正在尝试在 Google Docs 中创建一个简单的插件,但在阅读了文档后,我无法弄清楚 google.script.run 是如何无法正常工作的。 这是我的基本代码:

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"]
}

如果我在编辑器中运行dummyFunc,我可以在 Log 中看到 Doc 的 ID。但是,如果我测试我的插件,我会不断从客户端函数 showError 收到“错误”。 我做错了什么?

【问题讨论】:

  • 您在哪个配置下测试插件?您使用的是什么 Apps 脚本运行时?
  • 它对我有用
  • 我可以通过禁用新的 V8 引擎来完成这项工作。
  • 一定和thisbug有关

标签: google-apps-script google-docs google-apps-script-addon


【解决方案1】:

我只需禁用新的 V8 运行时就可以使其正常工作。 非常奇怪的行为,最重要的是,完全没有记录。

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多