无法在Google App脚本中使用Logger记录值 [英] Cannot log values using Logger in google app script

查看:59
本文介绍了无法在Google App脚本中使用Logger记录值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML代码,单击加载"按钮后,我从此处调用函数 loadClient

Here is my HTML code, from where I call the function loadClient when load button is clicked,

//LOAD CLIENT BUTTON`
document.getElementById("load_button").addEventListener('click',function(){
google.script.run.loadClient();
});

这是code.gs文件中的相应功能

here is the respective function in the code.gs file

//function02-loadClient
function loadClient() {
    eval(UrlFetchApp.fetch('https://apis.google.com/js/api.js').getContentText());
    return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/webmasters/v3/rest")
       .then(function() { Logger.log("GAPI client loaded for API"); },
              function() { Logger.log("Error loading GAPI client for API" ); });
}

推荐答案

在客户端环境中调用 google.script.run 时,您正在请求执行服务器端Apps脚本功能.

When you call google.script.run in the client-side environment, you're requesting execution of a server side Apps Script function.

loadClient()的实现在服务器端Apps脚本执行的上下文中没有意义.

Your implementation of loadClient() doesn't make sense in the context of server-side Apps Script execution.

这是一个完整的简单示例,该示例通过单击客户端按钮来成功触发服务器端的 Logger.log()调用:

Here's a full, simple example of successfully triggering a Logger.log() call on the server-side by way of a client side button click:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <script>
    function registerListener() {
      document.getElementById('callServerFunction').addEventListener('click', function() {
        google.script.run
            .withSuccessHandler(function() {
              alert("Successfully called server function.");
            })
            .withFailureHandler(function() {
              alert("Failed to call server function.");
            })
            .serverFunction();
      });
    }
  </script>
  <body onload="registerListener()">
    <button id="callServerFunction">Call Server Side Function</button>
  </body>
</html>

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('client');
}

function serverFunction() {
  Logger.log("Server function called.");
}

单击按钮后的服务器端日志记录结果

[18-06-10 13:48:20:359 PDT] Server function called.


关于 JavaScript Google API客户端库(即https://apis.google.com/js/api.js),这并不是要在Apps Script服务器端上下文中使用.Apps脚本的全部要点是,有一个可立即使用而无需任何服务的服务清单设置.除了不兼容之外,尝试在服务器端Apps脚本上下文中加载客户端JS库完全是多余的.


Regarding the Javascript Google API Client Libraries (i.e. https://apis.google.com/js/api.js), that's not meant to be used in the Apps Script server side context. The whole point of Apps Script is that there is a laundry list of services ready to use immediately without any setup. In addition to not being compatible, trying to load the client-side JS libraries in the server side Apps Script context is simply redundant.

类似地,尝试在客户端应用程序脚本代码中使用客户端JS库也没有太大意义,因为您可以通过

Similarly, trying to use the client-side JS libraries in client-side Apps Script code doesn't make too much sense either, as you have the full set of server-side functionality available via google.script.run.

这篇关于无法在Google App脚本中使用Logger记录值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆