Skip to content

Instantly share code, notes, and snippets.

@Adrian-Samuel
Last active February 7, 2020 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adrian-Samuel/d6d7fe098658a3872ca94956edb52014 to your computer and use it in GitHub Desktop.
Save Adrian-Samuel/d6d7fe098658a3872ca94956edb52014 to your computer and use it in GitHub Desktop.
// Function to insert into script for a google sheet to convert the rows into a JSON API
// Step 1: Add the records to the spreadsheet
// Step 2: Add the script, authorise script and then publish to web
// Step 3: Use generate URL to get an API response
// Documentation for this process can be found here: https://developers.google.com/apps-script/guides/web
function getSheetData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var sheetData = sheet.getDataRange().getValues()
var headers = sheet.getDataRange().getValues()[0]
var finalResults = [];
for(var i =1; i < sheetData.length;i++) {
var record = sheetData[i].reduce(function(dataObject, currentKey, idx){
dataObject[headers[idx]] = currentKey
return dataObject;
},{});
finalResults.push(record)
}
return finalResults
}
function doGet(e){
var content = getSheetData();
return ContentService.createTextOutput(JSON.stringify({results: content})).setMimeType(ContentService.MimeType.JSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment