Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Instructions:
- Follow all instructions in order.
- Step 1: Go to code.google.com, create a new script and copy/paste this script to the editor.
- Step 2: Select the save button next to Run (This is an important step, the script must be saved before deploying it)
- Step 3: Select the big blue Deploy button on the top right corner and select New Deployment from the dropdown menu.
- Step 4: Select the gear icon on the left corner and set type to Web App.
- 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)
- Step 6: Select Deploy and allow permissions. If it warns that the app is not secure just selet proceed.
- Step 7: Copy the web app URL and paste them into the inworld scripts
- Step 8: Proceed to the server script
- */
- let ssNew; // To create and open a new spreadsheet
- let ssId; // The id of the spreadsheet
- let sheet; // the sheet number of the spreadsheet
- let cell; // the cell range we want
- let props; // properties of the Google App Script. Properties are like a memory bank for the script
- let payload; // the payload of the POST back to the inworld script.The will be the body in http_request
- let options; // list of options/methods for the POST back to the inworld script
- let response; // the POST made back to the inworld script
- function doGet(e)
- {
- props = PropertiesService.getScriptProperties(); // Get current script properties. You can view properties in projetc settings, select the gear icon on the far left
- const client_url = e.parameter.client_url; // The value of the client_url parameter from llHttpRequest
- if(e.parameter.client_url) // requests from the client will be picked up from this function
- {
- ssId = props.getProperty('ssId'); // get the spreadsheet id from properties
- ssNew = SpreadsheetApp.openById(ssId); // open the spreadsheet
- sheet = ssNew.getSheets()[0]; // Select the sheet number of the spreadsheet
- cell = sheet.getRange("A2"); // Select the data in the cell range
- // the payload of the POST back to the inworld script.The will be the body in http_request
- payload =
- {
- "server_url":cell.getValue() // send the value of the cell back to the script
- };
- // list of options/methods for the POST back to the inworld script
- options =
- {
- "method" : "post",
- "payload" : payload
- };
- // Make the POST made back to the inworld script. Similar method to llHttpRequest.
- response = UrlFetchApp.fetch(client_url,options);
- }
- }
- function doPost(e) // requests from the server will be picked up from this function
- {
- const server_url = e.parameter.url; // The URL of the server provided from llHTTPRequest
- props = PropertiesService.getScriptProperties(); // Get current script properties. You can view properties in projetc settings, select the gear icon on the far left
- ssId = props.getProperty('ssId'); // Get the id of the spreadsheet saved in your Google drive
- if(ssId) // if the spreadsheet is created
- {
- ssNew = SpreadsheetApp.openById(ssId); // Open the spreadsheet
- sheet = ssNew.getSheets()[0]; // Select the sheet number of the spreadsheet
- cell = sheet.getRange("A2"); // Select the cell range we want to use
- cell.clear();// clear the cell range
- cell.setValue(e.parameter.url); // set the data in the cell range
- SpreadsheetApp.flush(); // Applies all pending Spreadsheet changes immediately. URL change in spreadsheet might be slow without this function so keep it in.
- props.setProperty('Server URL',server_url); // Store the server url in properties
- // the payload of the POST back to the inworld script.The will be the body in http_request
- payload =
- {
- "url_added":server_url // send confirmation of server url added to the spreadsheet
- };
- // list of options/methods for the POST back to the inworld script
- options =
- {
- "method" : "post",
- "payload" : payload
- };
- // Make the POST made back to the inworld script. Similar method to llHttpRequest.
- response = UrlFetchApp.fetch(server_url,options);
- } // if(ssId)
- else // if spreadsheet is not created
- {
- const ssName = e.parameter.spreadsheet; //The name of the spreadsheed sent from llHttpRequest
- ssNew = SpreadsheetApp.create(ssName); // Create a spreadsheet in Google Drive with the spreadsheet name provided from llHttpRequest
- props.setProperty('ssId', ssNew.getId()); // Add the spreadsheet id to script properties
- // Lets add headers to the spreadsheet
- ssId = props.getProperty('ssId'); // get the spreadsheet id from properties
- ssNew = SpreadsheetApp.openById(ssId); // Open the spreadsheet
- sheet = ssNew.getSheets()[0]; // Select the sheet number of the spreadsheet
- cell = sheet.getRange("A1"); // Select the cell range we want to use
- cell.clear();// clear the cell range
- cell.setValue("Server URL"); // set the data in the cell range
- SpreadsheetApp.flush(); // Applies all pending Spreadsheet changes immediately.
- // Lets respond back to the inworld script
- // the payload of the POST back to the inworld script.The will be the body in http_request
- payload =
- {
- "SpreadsheetReady":"SpreadsheetReady" // send confirmation of spreadsheet creation back to the script
- };
- // list of options/methods for the POST back to the inworld script
- options =
- {
- "method" : "post",
- "payload" : payload
- };
- // Make the POST made back to the inworld script. Similar method to llHttpRequest.
- response = UrlFetchApp.fetch(server_url,options); // POST back to the script the payload
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement