Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Google App Script Code by Gayngel of The Script Yard.
- Join The Script Yard Group: secondlife:///app/group/4921b4f0-c21e-7866-c0c2-791d10bb5502/about
- Visit The Script Yard Marketplace: https://marketplace.secondlife.com/stores/149734
- Description:
- This Google App Script keeps track of an LSL script's URL when it changes. The object the script is in can then be used as a server to retrieve persistent data from for a drop box and redelivery terminal system or scoreboard or HUD or whatever object/system you create that requires persistent stored data. The URL and stored data is saved to a spreadsheet and the data can be retrieved by a client script.
- Instructions:
- 1. Open your Google Drive and select New > More > Google App Script.
- 2. Before doing anything in the editor you must switch to the legacy editor. This is a very important step as some features don't work yet in the new editor. On the top right corner under your account button select "Use Legacy Editor". You can ignore and close the opt out survey.
- 3. Copy and paste this script into the code editor.
- 4. Select the save icon to save the script and give it a name. N.B: It is very important you save the script before publishing it to the web.
- 5. Select "Publish" on the menu bar and select "Deploy as Web App"
- 6. Select the Project Version dropdown and select New.
- 7. Under "Execute App As:" select Me(youremail@gmail.com)
- 8. Under "Who has access to the app:" select "Anyone, even anonymous".
- 9. Select "Deploy" and "Review Permissions".
- 10. Sign in to your google account.
- 11. If you get the "Google hasn't verified this app" message select "Advanced" and then select "Go to app_name(unsafe)".
- 12. Review and Allow Permissions.
- 13. Copy the web app url and paste it both in the server and client scripts in the line string app = ""; paste the url in between the quotation marks.
- If you make any changes to the script you must save and publish again. You must also select a new project version.
- */
- function doGet(e) // The e parameter is the query data and arguments after the URL denoted by "?". For example if your http request is app_url+"?object_name="+obj_name+"&obj_key="+(string)llGetKey() then object_name and object_key are the e.parameters and can be called as e.parameter.object_name and e.parameter.object_key respectively.
- {
- var props = PropertiesService.getScriptProperties(); // Properties service can store properties/metadata. You can find properties by selecting File > Project Properties > Script Properties tab
- var url;
- if(e.parameter.server_url)
- {
- url = e.parameter.server_url; // gets the url of the server
- var obj_key = e.parameter.obj_key;
- var prop_obj = props.getProperty('prop_obj'); // get the prop_obj property from script properties
- if(prop_obj)
- {
- if(obj_key == prop_obj) // prevents two servers sharing the same app script/database
- {
- props.setProperty('URL',url);
- var payload =
- {
- "URL_OK":"URL_OK" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
- };
- var options =
- {
- "method" : "post", // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
- "payload" : payload // what variables we are sending back to the server/client.
- };
- var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
- }
- else // server uuid is different to stored server uuid. can not share same app script / database
- {
- var payload =
- {
- "URL_INVALID":"URL_INVALID" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
- };
- var options =
- {
- "method" : "post", // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
- "payload" : payload // what variables we are sending back to the server/client.
- };
- var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
- }
- }
- else
- {
- props.setProperty('prop_obj', obj_key); // sets a property prop_obj as object_key
- props.setProperty('URL',url);
- var payload =
- {
- "URL_OK":"URL_OK" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
- };
- var options =
- {
- "method" : "post", // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
- "payload" : payload // what variables we are sending back to the server/client.
- };
- var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
- }
- }
- else if(e.parameter.client_url)
- {
- url = e.parameter.client_url; // gets the url of the client
- var ssNew;
- var ssId = props.getProperty('ssId'); //gets the id of the spreadsheet by retrieving the stored id in script properties
- ssNew = SpreadsheetApp.openById(ssId); //open the spreadsheet
- var sheet = ssNew.getSheets()[0]; // get the first sheet in the spreadsheet
- var cell = sheet.getRange("A1"); // gets the range of cells.
- var payload =
- {
- "server_url":cell.getValue() // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[]) Sends the value of cell A1.
- };
- var options =
- {
- "method" : "post", // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
- "payload" : payload // what variables we are sending back to the server/client.
- };
- var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
- }
- }
- function doPost(e) // The e parameter is the query data and arguments after the URL denoted by "?". For example if your http request is app_url+"?object_name="+obj_name+"&obj_key="+(string)llGetKey() then object_name and object_key are the e.parameters and can be called as e.parameter.object_name and e.parameter.object_key respectively.
- {
- var url = e.parameter.url;
- var props = PropertiesService.getScriptProperties(); // Properties service can store properties/metadata. You can find properties by selecting File > Project Properties > Script Properties tab
- var ssNew;
- var ssId = props.getProperty('ssId'); // get the ssId property from script properties
- if(ssId) // if the spreadsheet is created. I.E If the property ssId exists
- {
- ssNew = SpreadsheetApp.openById(ssId); // open the spreadsheet
- var sheet = ssNew.getSheets()[0]; // get first sheet of spreadsheet
- var cell = sheet.getRange("A1"); // target the first cell in the spreadsheet - A1
- cell.clear(); // clear the data in A1
- cell.setValue(e.parameter.url); // set the value of the A1 cell to the url of the server
- props.setProperty('URL',url); // sets a property URL as the url of the server
- var payload =
- {
- "url_added":"url_added" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
- };
- var options =
- {
- "method" : "post", // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
- "payload" : payload // what variables we are sending back to the server/client.
- };
- var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
- } // if(ssId)
- else // if spreadsheet is not created i.e there is no property named ssId in script properties
- {
- var ssName = e.parameter.spreadsheet; // the name of the spreadsheet set in the server script
- ssNew = SpreadsheetApp.create(ssName); // create a new spread sheet
- props.setProperty('ssId', ssNew.getId()); // sets a property ssID and set the vaue of the property to the id of the spreadsheet
- var payload =
- {
- "SpreadsheetReady":ssNew.getId() // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
- };
- var options =
- {
- "method" : "post", // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
- "payload" : payload // what variables we are sending back to the server/client.
- };
- var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
- }
- }
Add Comment
Please, Sign In to add comment