javascript - 如何使用javascript通过api在我的谷歌应用程序脚本中运行一个函数

标签 javascript html api google-apps-script

我想在我的谷歌应用程序脚本文件中运行一个函数。但是我怎样才能在外部网站上通过 api 运行它。请帮助任何人。

我试着像这样发送一个 api 请求 目标脚本代码.gs

 function addtext(sheetId) {
   var ss = SpreadsheetApp.openById(sheetId);
   ss.appendRow(['test', 'blabla'])
   });
 }

和javascript这样 var scriptId = "";

 // Initialize parameters for function call.
 var sheetId = "<ENTER_ID_OF_SPREADSHEET_TO_EXAMINE_HERE>";

 // Create execution request.
 var request = {
   'function': 'addtext',
   'parameters': [sheetId],
   'devMode': true  
 };

// Make the request.
 function apire() {
   gapi.client.request({
   'root': 'https://script.googleapis.com',
   'path': 'v1/scripts/' + scriptId + ':run',
   'method': 'POST',
   'body': request
   });
 }

最佳答案

可以关注JavaScript Quickstart :

HTML Service: Communicate with Server Functions

google.script.run is an asynchronous client-side JavaScript API that allows HTML-service pages to call server-side Apps Script functions. The following example shows the most basic functionality of google.script.runcalling a function on the server from client-side JavaScript.

HTML

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
google.script.run.doSomething();
</script>
</head>
</html>

code.gs

function doGet() {
return HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function doSomething() {
Logger.log('I was called!');
}

For an in-depth example, complete the steps described in the rest of this page, and in about five minutes you'll have a simple JavaScript application that makes requests to the Google Apps Script Execution API.

关于javascript - 如何使用javascript通过api在我的谷歌应用程序脚本中运行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740588/

相关文章:

php - 使用 Omnipay 的 PayPal 快速结帐未在沙盒帐户中显示订单

javascript - ng-options - 传递键而不是值

jquery - 使用jquery从odata wcf服务获取json

html - SVG 中 radialGradient 的停止不透明度行为

delphi - Delphi2009 的 PC-SC 包装器或 Omnikey Sync API 示例

java - 找不到 jumblr 的 .jar 文件(Tumblr android 包装器)

javascript - 当点向下钻取时获取 Highcharts 中点击点的名称

javascript - e.preventDefault() 不适用于 div onclick

javascript - 通过使用 JS 单击另一个按钮类型输入,将值从输入复制到剪贴板

带有CSS动画的javascript碰撞检测