繁体   English   中英

google.script.run独立使用

[英]google.script.run independent usage

现在,Google Apps脚本的客户端可以与HtmlService一起独立使用,我希望可以选择独立于客户端进行开发(主要是为了获得普通编辑器,js和css文件的好处,而不会被沙箱打扰)。 这可能意味着在服务器端脚本上实现自己的doGet / doPost,但随后,我失去了使用google.script.run直接调用函数并接受结果的好处。

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

您可以执行类似的操作,例如从网站中的AJAX请求中调用Google Apps脚本内容服务,并获得返回值。

Google文件-内容服务

那没有使用google.script.run API,但是基本上是一样的。 如果您想要运行Apps Script服务器端代码而不向用户显示任何内容,则可以通过对Apps Script Content Service应用程序URL进行JavaScript(jQuery等)AJAX调用来实现。

假设您有一个名为“ runAjaxToGetNames”的JavaScript函数。 并使用以下行调用它:

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

传递的参数是Apps脚本独立内容服务应用程序的URL。

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();
};

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