Fidelis
Fidelis

Reputation: 339

Run Google Apps Script Function via API

I have a few Google documents; some are normal Docs, some are Google Scripts. When I'm logged in with my account, I can run one of my scripts manually, that interacts with my Docs (it opens an existing Doc and copies the content to a new one for example). Is there any way for a random user, who is not logged in, to run my functions via API? Everybody should be able to run my scripts easily, but not be able to see the code or what it actually does.

Upvotes: 0

Views: 2192

Answers (2)

James
James

Reputation: 79

Let me elaborate on MZimmerman's and Zig's suggestions.

  1. Create whatever function you want and encapsulate it in a Content Service doGet().
  2. Save a version and publish the web app for use by Anyone, even anonymous users. Be aware that publishing for only Anyone is insufficient authorization; it must be published for anonymous users.
  3. Execute the web app using UrlFetchApp.fetch(url) to invoke the doGet() script. You can pass request parameters (cf. documentation) and return textual error codes as you may require/wish.

This is very cool. I am using it to set up a multi-user ensemble of Gsheet workbooks that have protected ranges. The workbooks have a custom menu with code that invokes web app scripts to sort and process the tabular data. These web apps are run as the workbook owner, not the effective user; hence allowing the effective user to process the protected data ranges.

Upvotes: 1

mzimmerman
mzimmerman

Reputation: 930

Yes, publish it as a service and to anonymous users. Then setup some specific crafted URLs that run the functions you want it to run.

Upvotes: 1

Related Questions