Passing parameters from one script to another using PropertiesService

2,167 views
Skip to first unread message

Neville N

unread,
Jul 25, 2020, 3:19:15 AM7/25/20
to google-apps-sc...@googlegroups.com
Hello all. Let me begin by stating that i am pretty much a newbie which is still trying to understand the workings of the Apps Script. Unlike many, I like to take code and take them apart to understand how things actually work. 

What I am trying to achieve: I have a Woo-commerce/WP site  from which I am sending new customers data to Google sheet through webhook. It is working perfectly. All thanks to the post by Ben collins https://www.benlcollins.com/spreadsheets/marking-template/ , i have created a Apps script which does the job.

I have created a second script(this is a separate script) which can send data to a slack channel to notify me of the sale on the phone via slack messages. I tested it and as soon as I enter data into a row it sends it to slack.(please note I have tested it by manually adding a row which then shows up in slack) 

Now, I am trying to link the two together. 
Script 1 has the code for sending data from Woo-commerce to Google sheets.
Script 2 has the code for sending data from Google Sheets to Slack.

But I am not able to pass the values of variables from Script 1 to Script 2. 

I understand that variables  in script have a scope within the function only. But the values can be stored and passed through PropertiesService . But I have not understood how to do it. There is no example code that explains that lucidly. So, I am stuck with only half of the work done. 

This is equally about learning about Google App Script as well as trying to automate certain tasks. I hope that I will get some base ideas on how to proceed. 

The code :
Script1:

function doPost(e) {
   
var Orders = JSON.parse([e.postData.contents]);
 
var order_id = Orders.id;
 
var order_time = Orders.date_created;
 
var email = Orders.billing.email;
 
var phone = Orders.billing.phone;
 
 
var first_name = Orders.billing.first_name;
 
var last_name = Orders.billing.last_name;
 
var sheet = SpreadsheetApp.getActiveSheet();


 
//implementation code
 
 
var response = HtmlService.createHtmlOutput();
 
return response;
 
}
 

Script2: 
var url = "https://hooks.slack.com/services/ZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
 
 
var payload = {
   
"channel" : "#general",
   
"username" : "xxxxxxxxx",
   
"text" : "Awesome!!  You have got a new order" +
   
"\n Order number:" + order_id +
   
"\n Customer Name :" + first_name + last_name
   
"\n Email :" +email +
   
"\n Phone :" +phone +
     
"\n Order Received on :" +order_time
 
 
  sendToSlack
(url,payload)
 
};


function sendToSlack(url, payload){
 
var options = {
   
"method": "post",
   
"contentType": "application/json",
   
"payload": JSON.stringify(payload)
 
};
 
 
return UrlFetchApp.fetch(url, options)
}

Clark Lind

unread,
Jul 25, 2020, 8:54:28 AM7/25/20
to Google Apps Script Community
I don't know if this will work, but if I were trying it, I would set up a doGet() function in script 2, and publish as a webapp. Then from script1, attach all your parameters to the script2 url, not as an object, just individual parameters. And call it using urlFetch. The only issue I see with this approach would be any security concerns about sending the data openly, but Google uses https, so may or may not be an issue. At the end of the doGet() function, you could simply return a results message (success or error) or something. This is all conceptual of course.

Hope that helps!
Clark

On Saturday, July 25, 2020 at 3:19:15 AM UTC-4, Neville N wrote:
Hello all. Let me begin by stating that i am pretty much a newbie which is still trying to understand the workings of the Apps Script. Unlike many though, I like to take code and take them apart to understand who things actually work. 

Neville N

unread,
Jul 26, 2020, 1:00:38 AM7/26/20
to Google Apps Script Community
Thank you for your response. I will try your suggestion and see if it works. What I am confused is that how do I attach all the parameters to script2 url & call using urFetch? Could you please provide a code snippet for that?

Clark Lind

unread,
Jul 26, 2020, 12:27:16 PM7/26/20
to Google Apps Script Community
Attach your parameters on the end using a questionmark then parameterName1=parameter1&parameterName2=parameter2  etc

So your Script1 might look something like this:


Script1
function doPost(e) {
    var Orders = JSON.parse([e.postData.contents]);
 var order_id = Orders.id;
  var order_time = Orders.date_created;
  var email = Orders.billing.email;
  var phone = Orders.billing.phone;
 
  var first_name = Orders.billing.first_name;
  var last_name = Orders.billing.last_name;
 var sheet = SpreadsheetApp.getActiveSheet();


 //implementation code

//This should pass the parameters you are interested in over to Script2 using its published url. 
//Script2 has to have a doGet(e) (or doPost(e)).
 
//Build the Url using Script2's published url:
var Url = "https://script.google.com/a/macros/blahblahblahblah-bignumber-blahblah/edit?orderID=" + order_id + "email=" + email + "fname=" + first_name + " lname=" + last_name; //just keep adding the parameters

var response = UrlFetchApp.fetch(Url);
Logger.log(response.getContentText());

//In Script2, you simply grab the parameters in "e" (e.orderID, e.email, etc). Because the Urlfetch in Script1 is expecting something, just do a simple   return "Success!" at the end of the doGet/doPost in Script2.
 
 
// var response = HtmlService.createHtmlOutput();
 // return response;
  }

If this isn't working (since I'm just speculating), refer to this for trying a Post   https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl,-params

Riël Notermans

unread,
Jul 26, 2020, 1:17:50 PM7/26/20
to Google Apps Script Community
Another easy method is a shared library.

Check the examples in these docs:

Op zo 26 jul. 2020 18:27 schreef Clark Lind <cwl...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b0f803b3-d427-40c6-9007-548a444a294bo%40googlegroups.com.

Riël Notermans

unread,
Jul 26, 2020, 1:19:07 PM7/26/20
to Google Apps Script Community
This means no exposing your webapps. Just use user/scriptproperties from the library and make a function to pass them to one of the 2 scripts.

Op zo 26 jul. 2020 19:17 schreef Riël Notermans <ri...@zzapps.nl>:

Clark Lind

unread,
Jul 27, 2020, 9:23:13 AM7/27/20
to Google Apps Script Community
Riël to the rescue! :)  Thank you. Neville, if Riël suggests it, it is a much better way. 
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

con...@quiteoftenapp.com

unread,
Jul 27, 2020, 9:44:09 AM7/27/20
to Google Apps Script Community
Are those two scripts in separate projects? Do they need to be in separate projects?

Because if not, you can place both URL and payload variables inside the first script and call sendToSlack(url, payload) from the first function, before the return.

If they need to be on separate projects, I would also do it via shared library, as suggested by Riël.

Neville N

unread,
Jul 27, 2020, 9:45:02 AM7/27/20
to Google Apps Script Community
thank you Riël  and cwlind. I checked the properties service but the examples go way up my head. Hence the confusion and the question. Will try again to understand. 

Neville N

unread,
Jul 27, 2020, 9:58:40 AM7/27/20
to Google Apps Script Community
No the two scripts are in same project. 

I tried what you suggested earlier but it is not working. I will try again though

Riël Notermans

unread,
Jul 27, 2020, 12:51:30 PM7/27/20
to Google Apps Script Community
Essentially the properties are tied to the library. So if you have a function that reads properties and return Them, you can add the library to Both scripts.

If you call the functions you get the properties stores in the library. It is by far the easiest wat and also gives opportunity to tie settings to users.

Op ma 27 jul. 2020 15:58 schreef Neville N <nevil...@gmail.com>:
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/c4a2793e-9a21-4586-aee4-d5c897b7658bo%40googlegroups.com.

Riël Notermans

unread,
Jul 27, 2020, 12:51:50 PM7/27/20
to Google Apps Script Community
If you need working example let me know!

Op ma 27 jul. 2020 18:51 schreef Riël Notermans <ri...@zzapps.nl>:

Neville N

unread,
Aug 1, 2020, 8:00:47 AM8/1/20
to Google Apps Script Community
Yes please. It would really help me in understanding.
Reply all
Reply to author
Forward
0 new messages