Skip to content

Instantly share code, notes, and snippets.

@nikhilbhatt
Created November 19, 2022 20:17
Show Gist options
  • Save nikhilbhatt/e90028edd38b127c82157394257c4c3e to your computer and use it in GitHub Desktop.
Save nikhilbhatt/e90028edd38b127c82157394257c4c3e to your computer and use it in GitHub Desktop.
App script code to create API from google sheets
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