我想在我的谷歌应用程序脚本文件中运行一个函数。但是我怎样才能在外部网站上通过 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.run
— calling 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/