Google Sheet to Rest API with JSON Data using Macro Script

Picture of Amitpal Singh
Amitpal Singh
February 15, 2024
function doGet(req) {
  var service = req.parameter.service;
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = doc.getSheetByName('sheet_name');
  var values = sheet.getDataRange().getValues();

  var output = [];
  for(var i=0; i<values.length; i++)
  {
    var row = {};
    row['first_column'] = values[i][0];
    row['second_column'] = values[i][1];
    row['service'] = values[i][2]; //thirdcolumn
    output.push(row);
  }

  if(service !=null){
    var outputToReturn = output.filter(obj => obj.service.toLowerCase().includes(service.toLowerCase()));
    return ContentService.createTextOutput(JSON.stringify({data: outputToReturn})).setMimeType(ContentService.MimeType.JSON);
  }

return ContentService.createTextOutput(JSON.stringify({data: output})).setMimeType(ContentService.MimeType.JSON);

}
<script>
fetch('scripturl')
.then(res => res.json())
.then(data => console.log(data));
</script>

Share this post:

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.