Created
November 19, 2022 20:17
-
-
Save nikhilbhatt/e90028edd38b127c82157394257c4c3e to your computer and use it in GitHub Desktop.
App script code to create 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 doGet(req) { | |
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
let personalDetails = spreadsheet.getSheetByName('personal-details'); //sheet name is personal-details | |
let personalDetailsValues = personalDetails.getDataRange().getValues(); //reading the sheet data(2D array of sheet data) | |
/* | |
personalDetailsValues output | |
[ [ 'First Name', 'Nikhil' ], | |
[ 'Last Name', 'Bhatt' ], | |
[ 'Email', '[email protected]' ] ] | |
*/ | |
let personalData = {}; // output hash | |
for (let i = 0; i < personalDetailsValues.length; i++) { | |
personalData[`${personalDetailsValues[i][0]}`] = personalDetailsValues[i][1]; //Getting the data in desired format | |
} | |
return ContentService | |
.createTextOutput(JSON.stringify({personalData: personalData})) | |
.setMimeType(ContentService.MimeType.json); //returning the data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment