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)
}