Best answer

How can zapier trigger a google sheet script to run?

  • 6 October 2020
  • 2 replies
  • 5064 views

Userlevel 1

Hello, I am new to google sheets and zapier.

I have a “Clear contents” script that needs to run before something else is written in that same sheet everyday.
In what ways can Zapier trigger that script to run by itself?
 

Any help is appreciated.

thank you!



 

icon

Best answer by Wemakefuture 7 October 2020, 05:19

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 6
Badge +7

Just create a webhook at the end of a zap and trigger a http endpoint on your sheet. 

 

So yes, you can trigger an Apps Script function with an HTTPS POST request. You'll need to add a doPost() function to a script file. The doPost() function can also receive data from the POST request. If you want to receive data into the doPost() function, add a variable name inside of the parenthesis:

doPost(e) {
// . . code here . .

};

The documentation typically shows a variable named e inside of the parenthesis, but you can use any name that you want.

If you need to send a POST request from an Apps Script function, you can use:

UrlFetchApp.fetch()

The default type of request is a GET request, so you will need to use advanced options, and explicitly designate a POST request.

Google Apps Script Documentation - UrlFetchApp.fetch()

In order for the Apps Script project to be available, it must be published. In the script editor, click the "Publish" menu, and choose "Deploy as Web App". There are two versions of a Web App, the development version, and the "production" version. The development version has "dev" on the end of the URL. The production version has "exec" on the end of the URL.

The "exec" version of the Apps Script URL is what you must use for the URL in your POST request to your Apps Script project.

If you want to return something from the Apps Script web app, then you must use Content Service.

You can get more information from the following Stack Overflow post:

Call a custom GAS function from an external URL

Just create a webhook at the end of a zap and trigger a http endpoint on your sheet. 

 

So yes, you can trigger an Apps Script function with an HTTPS POST request. You'll need to add a doPost() function to a script file. The doPost() function can also receive data from the POST request. If you want to receive data into the doPost() function, add a variable name inside of the parenthesis:

doPost(e) {
// . . code here . .

};

The documentation typically shows a variable named e inside of the parenthesis, but you can use any name that you want.

If you need to send a POST request from an Apps Script function, you can use:

UrlFetchApp.fetch()

The default type of request is a GET request, so you will need to use advanced options, and explicitly designate a POST request.

Google Apps Script Documentation - UrlFetchApp.fetch()

In order for the Apps Script project to be available, it must be published. In the script editor, click the "Publish" menu, and choose "Deploy as Web App". There are two versions of a Web App, the development version, and the "production" version. The development version has "dev" on the end of the URL. The production version has "exec" on the end of the URL.

The "exec" version of the Apps Script URL is what you must use for the URL in your POST request to your Apps Script project.

If you want to return something from the Apps Script web app, then you must use Content Service.

You can get more information from the following Stack Overflow post:

Call a custom GAS function from an external URL

 

Thank you for your advise, but I don’t get it… can you maybe make an example HOW to launch a specific Function in Google Sheets with Zapier webhooks?

 

Kind Regards,

Jan