Last active
February 7, 2020 02:52
-
-
Save Adrian-Samuel/d6d7fe098658a3872ca94956edb52014 to your computer and use it in GitHub Desktop.
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
// 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