Advertisement
Gayngel

Basic URL storage to Google Sheet - Google App Script

Nov 14th, 2023 (edited)
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Instructions:
  3. Follow all instructions in order.
  4.  
  5. Step 1: Go to code.google.com, create a new script and copy/paste this script to the editor.
  6.  
  7. Step 2: Select the save button next to Run (This is an important step, the script must be saved before deploying it)
  8.  
  9. Step 3: Select the big blue Deploy button on the top right corner and select New Deployment from the dropdown menu.
  10.  
  11. Step 4: Select the gear icon on the left corner and set type to Web App.
  12.  
  13. Step 5: Give the new deployment a description in the description field, select execute as Me (email address) and set who has access to Anyone (NOT ANYONE WITH A GOOGLE ACCOUNT, JUST ANYONE)
  14.  
  15. Step 6: Select Deploy and allow permissions. If it warns that the app is not secure just selet proceed.
  16.  
  17. Step 7: Copy the web app URL and paste them into the inworld scripts
  18.  
  19. Step 8: Proceed to the server script
  20. */
  21.  
  22. let ssNew; // To create and open a new spreadsheet
  23. let ssId; // The id of the spreadsheet
  24. let sheet; // the sheet number of the spreadsheet
  25. let cell; // the cell range we want
  26. let props; // properties of the Google App Script. Properties are like a memory bank for the script
  27. let payload; // the payload of the POST back to the inworld script.The will be the body in http_request
  28. let options; // list of options/methods for the POST  back to the inworld script
  29. let response; // the POST made back to the inworld script
  30.  
  31. function doGet(e)
  32. {
  33.  
  34. props = PropertiesService.getScriptProperties(); // Get current script properties. You can view properties in projetc settings, select the gear icon on the far left
  35. const client_url = e.parameter.client_url; // The value of the client_url parameter from llHttpRequest
  36.  
  37.  
  38.  if(e.parameter.client_url) // requests from the client will be picked up from this function
  39.   {
  40.    
  41.    
  42.     ssId = props.getProperty('ssId'); // get the spreadsheet id from properties
  43.     ssNew = SpreadsheetApp.openById(ssId); // open the spreadsheet
  44.     sheet = ssNew.getSheets()[0];  // Select the sheet number of the spreadsheet
  45.     cell = sheet.getRange("A2"); // Select the data in the cell range
  46.  
  47.      // the payload of the POST back to the inworld script.The will be the body in http_request
  48.     payload =
  49.    {
  50.      "server_url":cell.getValue() // send the value of the cell back to the script
  51.    };
  52.  
  53.   // list of options/methods for the POST  back to the inworld script
  54.       options =
  55.    {
  56.      "method" : "post",
  57.      "payload" : payload
  58.    };
  59.  
  60. // Make the POST made back to the inworld script. Similar method to llHttpRequest.
  61.    response = UrlFetchApp.fetch(client_url,options);
  62.    
  63.    
  64.   }
  65.  
  66. }
  67.  
  68. function doPost(e)  // requests from the server will be picked up from this function
  69. {
  70.   const server_url = e.parameter.url; // The URL of the server provided from llHTTPRequest
  71.   props = PropertiesService.getScriptProperties(); // Get current script properties. You can view properties in projetc settings, select the gear icon on the far left
  72.    
  73.   ssId = props.getProperty('ssId');   // Get the id of the spreadsheet saved in your Google drive
  74.  
  75.  
  76.   if(ssId) // if the spreadsheet is created
  77.   {
  78.    ssNew = SpreadsheetApp.openById(ssId); // Open the spreadsheet
  79.    sheet = ssNew.getSheets()[0];  // Select the sheet number of the spreadsheet
  80.    cell = sheet.getRange("A2"); // Select the cell range we want to use
  81.     cell.clear();// clear the cell range
  82.    cell.setValue(e.parameter.url); // set the data in the cell range
  83.    SpreadsheetApp.flush(); // Applies all pending Spreadsheet changes immediately. URL change in spreadsheet might be slow without this function so keep it in.
  84.    props.setProperty('Server URL',server_url); // Store the server url in properties
  85.    
  86.    
  87.     // the payload of the POST back to the inworld script.The will be the body in http_request
  88.     payload =
  89.    {
  90.      "url_added":server_url // send confirmation of server url added to the spreadsheet
  91.    };
  92.  
  93.   // list of options/methods for the POST  back to the inworld script
  94.       options =
  95.    {
  96.      "method" : "post",
  97.      "payload" : payload
  98.    };
  99.  
  100. // Make the POST made back to the inworld script. Similar method to llHttpRequest.
  101.    response = UrlFetchApp.fetch(server_url,options);
  102.  
  103.      
  104.  
  105.   } // if(ssId)
  106.  
  107.   else // if spreadsheet is not created
  108.   {
  109.    const ssName = e.parameter.spreadsheet; //The name of the spreadsheed sent from llHttpRequest
  110.    
  111.     ssNew = SpreadsheetApp.create(ssName); // Create a spreadsheet in Google Drive with the spreadsheet name provided from llHttpRequest
  112.    
  113.    props.setProperty('ssId', ssNew.getId()); // Add the spreadsheet id to script properties
  114.  
  115.   // Lets add headers to the spreadsheet
  116.  
  117.    ssId = props.getProperty('ssId'); // get the spreadsheet id from properties
  118.    ssNew = SpreadsheetApp.openById(ssId); // Open the spreadsheet
  119.    sheet = ssNew.getSheets()[0];  // Select the sheet number of the spreadsheet
  120.    cell = sheet.getRange("A1"); // Select the cell range we want to use
  121.     cell.clear();// clear the cell range
  122.    cell.setValue("Server URL"); // set the data in the cell range
  123.    SpreadsheetApp.flush(); // Applies all pending Spreadsheet changes immediately.
  124.  
  125.    // Lets respond back to the inworld script
  126.  
  127.     // the payload of the POST back to the inworld script.The will be the body in http_request
  128.     payload =
  129.    {
  130.      "SpreadsheetReady":"SpreadsheetReady" // send confirmation of spreadsheet creation back to the script
  131.    };
  132.  
  133.   // list of options/methods for the POST  back to the inworld script
  134.     options =
  135.    {
  136.      "method" : "post",
  137.      "payload" : payload
  138.    };
  139.  
  140. // Make the POST made back to the inworld script. Similar method to llHttpRequest.
  141.   response = UrlFetchApp.fetch(server_url,options);  // POST back to the script the payload
  142.    
  143.    
  144.   }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement