Gayngel

Basic URL storage to Google Sheet - Server Script

Nov 14th, 2023
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Instructions
  2. // Follow all steps in order beginning from the Google App Script
  3.  
  4. // Step 9: Copy/Paste this script into the server object
  5.  
  6.  
  7. // Step 10: Paste the URL of the Google App script into the Google_URL variable
  8.  
  9. // Step 11: Proceed to the client script
  10.  
  11. string Google_URL = ""; // The URL of the Google App Script
  12. string spreadsheet_name = "URL_Spreadsheet"; // Write the name of the spreadsheet here  the Google App script will create a spreadsheet in your Google Drive with this name.
  13. key reqURL;  // The id of the request for a new URL
  14. string server_url; // The URL of the server script
  15. key http_req; // The id of the http request to the Google app script. // May be redundant as the app script creates a new post to the server with its own id.
  16.  
  17. default
  18. {
  19.     state_entry()
  20.     {
  21.        llReleaseURL(server_url); // release all urls assigned to the server_url varaibale
  22.        reqURL = llRequestURL(); // request a new url which will be assigned to the server_url variable
  23.     }
  24.  
  25.     http_request(key id, string method, string body)
  26.     {
  27.      
  28.        
  29.         if(id == reqURL)
  30.         {
  31.         if (method == URL_REQUEST_DENIED)
  32.         {
  33.             llOwnerSay("No free URLs!"); // All URL slots have been used up in the parcel. The number of URLS avaialble in a parcel is the number of allowed prims in the parcel. For example if you have a maximum prim allowance of 100 prims you can only use up 100 URLs. You may never see this message if you dont request URLs in as many scripts as your prim allowance.
  34.         }
  35.         else if (method == URL_REQUEST_GRANTED)
  36.         {
  37.             server_url = body; // assign the url to the server_url variable
  38.            llOwnerSay("Creating spreadsheet.");
  39.           if(Google_URL != "") // if the google appp script url is not blank
  40.         {  
  41.        
  42.        http_req = llHTTPRequest(Google_URL+"?"+"spreadsheet="+llEscapeURL(spreadsheet_name)+"&"+"url="+server_url,[HTTP_METHOD,"POST"],"Create"); // make a request to the Google App Script to create a spreadsheet if one does not exist
  43.        
  44.      
  45.         }
  46.         }
  47.        
  48.      
  49.        
  50.      }
  51.      
  52.       list tmp = llParseString2List(body,["="],[""]); // Parse the body into a command and reference (key/value pair)
  53.        string cmd = llList2String(tmp,0); // command/key
  54.        string ref = llList2String(tmp,1); // reference/value
  55.        
  56.        if(cmd == "SpreadsheetReady") // If the spreadsheet was newly created
  57.        {
  58.            llOwnerSay("Spreadsheet created.");
  59.            http_req = llHTTPRequest(Google_URL+"?"+"url="+server_url,[HTTP_METHOD,"POST"],"AddURL"); // add the server url to the spreadsheet
  60.         }
  61.        
  62.         else if(cmd == "url_added") // If the spreadsheet already exists
  63.        {
  64.            
  65.            llOwnerSay("Server URL added to spreadsheet: " + llUnescapeURL(ref));
  66.         }
  67.      
  68.    
  69.      
  70.      
  71.     }
  72.    
  73.    
  74.    
  75.     changed(integer change)
  76.     {
  77.        if(change & CHANGED_REGION || change & CHANGED_REGION_START)
  78.         {
  79.          
  80.          llResetScript();  // URL will release on region change or region restart so reset the script to request a new URL
  81.            
  82.         }
  83.        
  84.        
  85.     }
  86. }
Add Comment
Please, Sign In to add comment