2

I want to run a function in my google app script file. but how can i run it via api in an external website. Help anyone please.

I tried to send an api request as this target script code.gs

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

and javascript as this 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
   });
 }

1 Answer 1

-2

You can follow 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.