Created
April 5, 2024 01:22
-
-
Save itsabhinavjain/d11ededdec49eb10ecec7b4d7a004503 to your computer and use it in GitHub Desktop.
Rest API from Google Sheets
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 convertToJson(data) { | |
const headers = data[0] | |
const raw_data = data.slice(1,) | |
let json = [] | |
raw_data.forEach(d => { | |
let object = {} | |
for (let i = 0; i < headers.length; i++) { | |
object[headers[i]] = d[i] | |
} | |
json.push(object) | |
}); | |
return json | |
} | |
function json(sheetName) { | |
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet() | |
const sheet = spreadsheet.getSheetByName(sheetName) | |
const data = sheet.getDataRange().getValues() | |
const jsonData = convertToJson(data) | |
return ContentService | |
.createTextOutput(JSON.stringify(jsonData)) | |
.setMimeType(ContentService.MimeType.JSON) | |
} | |
function doGet(e) { | |
const path = e.parameter.path | |
return json(path) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment