Last active
November 16, 2020 17:16
-
-
Save rogergcc/071225ef95055476e4685f01e9697486 to your computer and use it in GitHub Desktop.
Sheet as Webservice Api
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
const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1') | |
function doGet(e){ | |
const action = e.parameter.action; | |
//URL_SCRIPT/exec?action=getItems | |
if(action == 'getItems'){ | |
return getItems(e); | |
} | |
} | |
function getItems(e){ | |
let records={}; | |
const rows = sheet.getRange(2, 1, sheet.getLastRow() - 1,sheet.getLastColumn()).getValues(); | |
const sizeRows = rows.length; | |
let data = []; | |
for (let i = 0; i < sizeRows; i++) { | |
let row = rows[i], | |
record = {}; | |
record['Title'] = row[0]; | |
record['Description']=row[1]; | |
record['Price']=row[2]; | |
data.push(record); | |
} | |
records.items = data; | |
let result=JSON.stringify(records); | |
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON); | |
} |
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
//If appscript created from main page, | |
// this way u can refer to other sheet by the url | |
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1q8tX6qEj5NbWKaGX3hWAzoSbrHqwM9IN-8bqOZmbIIo/edit#gid=0"); // sheet of account | |
var sheet = ss.getSheetByName("Sheet1"); //it is the sheet name .. so it should match | |
function doGet(e){ | |
var action = e.parameter.action; | |
//URL_SCRIPT/exec?action=getItems | |
if(action == 'getItems'){ | |
return getItems(e); | |
} | |
} | |
function getItems(e){ | |
var records={}; | |
var rows = sheet.getRange(2, 1, sheet.getLastRow() - 1,sheet.getLastColumn()).getValues(); | |
data = []; | |
for (var r = 0, l = rows.length; r < l; r++) { | |
var row = rows[r], | |
record = {}; | |
record['Title'] = row[0]; | |
record['Description']=row[1]; | |
record['Price']=row[2]; | |
data.push(record); | |
} | |
records.items = data; | |
var result=JSON.stringify(records); | |
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1')