简体   繁体   English

google.script.run独立使用

[英]google.script.run independent usage

Now that the client side of a Google Apps Script is standalone with HtmlService, I want the option to develop it independently of the client side (mainly to get the benefits of normal editor, js & css files and not be bothered by sandboxing). 现在,Google Apps脚本的客户端可以与HtmlService一起独立使用,我希望可以选择独立于客户端进行开发(主要是为了获得普通编辑器,js和css文件的好处,而不会被沙箱打扰)。 This can mean implementing my own doGet/doPost on the server side script, but then I loose the nicety of using google.script.run to directly call functions and accept results. 这可能意味着在服务器端脚本上实现自己的doGet / doPost,但随后,我失去了使用google.script.run直接调用函数并接受结果的好处。

So, is there a way to use google.script.run independently? 

You can do something like call a Google Apps Script Content Service from an AJAX request in a website and get a return value. 您可以执行类似的操作,例如从网站中的AJAX请求中调用Google Apps脚本内容服务,并获得返回值。

Google Documentation - Content Service Google文件-内容服务

That doesn't use the google.script.run API, but it's basically the same thing. 那没有使用google.script.run API,但是基本上是一样的。 If what you want is to run Apps Script server side code without displaying anything to the user, then you can do that with a JavaScript (jQuery, etc) AJAX call to the Apps Script Content Service apps URL. 如果您想要运行Apps Script服务器端代码而不向用户显示任何内容,则可以通过对Apps Script Content Service应用程序URL进行JavaScript(jQuery等)AJAX调用来实现。

Suppose you have a JavaScript function named "runAjaxToGetNames". 假设您有一个名为“ runAjaxToGetNames”的JavaScript函数。 And it's called with the following line: 并使用以下行调用它:

runAjaxToGetNames("https://script.google.com/macros/s/AKfycbyFyODk/exec");

An argument is passed that is the URL of the Apps Script Stand Alone Content Service app. 传递的参数是Apps脚本独立内容服务应用程序的URL。

The AJAX function looks like this: AJAX函数如下所示:

function runAjaxToGetNames(argURL_forAJAX) {

  console.log('runAjaxToGetNames ran: ' + argURL_forAJAX);

  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    //console.log'xmlhttp.readyState: ' + xmlhttp.readyState);
    if (xmlhttp.readyState===4 && xmlhttp.status===200) {
      //console.log'xmlhttp.responseText: ' + xmlhttp.responseText);

      glblStoreNames.names = xmlhttp.responseText;
      //console.log'return value into the global object?: ' + glblStoreNames.names);
      //Call a function to populate the store names
      populateStoreNames();

    };
  };

  xmlhttp.open("GET",argURL_forAJAX,true);
  xmlhttp.send();
};

The Apps Script content service is a file all to itself with only the following code: Apps脚本内容服务本身就是一个文件,仅包含以下代码:

//This Apps Script is to get a list of all the names out of a Google Doc.
function doGet() {
  var theGogDocReference = DocsList.getFileById('YWY4ZmM5OQ_File_ID_Here');
  var theContent = theGogDocReference.getContentAsString();
  return ContentService.createTextOutput(theContent);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM