Last active
July 1, 2020 10:33
-
-
Save JonathanMCarter/e40e98f4ef1bf4e732906b5db7d49968 to your computer and use it in GitHub Desktop.
A snippet of a client project which needed to save and load words into multiple columns of a google sheet. Please contact me to get full script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) Jonathan Carter | |
E: jonathan@carter.games | |
W: https://jonathan.carter.games/ | |
Twine harlowe google sheet saving and loading of single cells script | |
GET - returns the word or words up until the desired amount, or as many as there are if not enough | |
POST - adds the inputted word from twine to the sheet this script is linked to on the sheet defined below | |
*/ | |
// Make sure this is the sheet you wish to write to... (default is 'DATA') | |
var SHEET_NAME = "DATA"; | |
// Used to access the sheet to return data later on | |
var SCRIPT_PROP = PropertiesService.getScriptProperties(); | |
// Calls the Custom Get Function & returns its return value | |
function doGet(e) | |
{ | |
return GetValues(e); | |
} | |
function doPost(e) | |
{ | |
return AddWordToSheet(e); | |
} | |
// Gets the values in Column 2, Row 1 and returns them in a JSON string to be used in engine | |
function GetValues(e) | |
{ | |
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key")); | |
var sheet = doc.getSheetByName(SHEET_NAME); | |
// Needed to get data | |
var colToRead = 0; | |
var headersss = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]; | |
for (var i = 0; i < headersss.length; i++) | |
{ | |
if (headersss[i] == e.parameter.column) | |
{ | |
colToRead = i+1; | |
break; | |
} | |
} | |
var numRows = sheet.getLastRow(); | |
var readData = sheet.getRange(2, colToRead, numRows).getValues(); | |
var rowLimit = 0; | |
// finds the first free row, from the top as going from the bottom is wayyyyy to slow... | |
for (var i = 0; i < readData.length; i++) | |
{ | |
if (readData[i][0] == "") | |
{ | |
rowLimit = i; | |
} | |
} | |
// Remained of Get method not here..... please email to get full script | |
} | |
// Custom Alterations that allow the inputted data to be placed into any column defined by the user | |
// In Twine the following should be sent a word to the sheet - (word to input, column name) | |
// This makes e[0] = the word the user added & e[1] the column that it shoudl be inserted into | |
function AddWordToSheet(e) | |
{ | |
// Stops data overriding by accident | |
var lock = LockService.getPublicLock(); | |
lock.waitLock(30000); // wait 30 seconds before conceding defeat. | |
try | |
{ | |
// Sets where the input is written (this one just gets the sheet name) | |
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key")); | |
var sheet = doc.getSheetByName(SHEET_NAME); | |
//var postData = e.postData.contents; //my code uses postData instead | |
var eData = e.postData.contents; | |
var data = JSON.parse(eData); //parse the postData from JSON | |
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]; | |
var nextRow = sheet.getLastRow()+1; // get next row | |
var dataToInsert = []; | |
var colToAddTo = 2; | |
// Tries to find the column the user has inputted, this will fail if the user fails to define a column it should defalt to the next free column | |
for (i in headers) | |
{ | |
if (data[1].length > 0) | |
{ | |
if (headers[i] == data[1]) | |
{ | |
colToAddTo = i; | |
break; | |
} | |
} | |
else | |
{ | |
// should get the last column if the user failed to pass in a value for the column | |
colToAddTo = sheet.getLastColumn(); | |
break; | |
} | |
} | |
// Remained of Send method not here..... please email to get full script | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment